index.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import { VantComponent } from '../common/component';
  2. import { pickerProps } from '../picker/shared';
  3. import { requestAnimationFrame } from '../common/utils';
  4. const EMPTY_CODE = '000000';
  5. VantComponent({
  6. classes: ['active-class', 'toolbar-class', 'column-class'],
  7. props: Object.assign(Object.assign({}, pickerProps), { showToolbar: {
  8. type: Boolean,
  9. value: true,
  10. }, value: {
  11. type: String,
  12. observer(value) {
  13. this.code = value;
  14. this.setValues();
  15. },
  16. }, areaList: {
  17. type: Object,
  18. value: {},
  19. observer: 'setValues',
  20. }, columnsNum: {
  21. type: null,
  22. value: 3,
  23. }, columnsPlaceholder: {
  24. type: Array,
  25. observer(val) {
  26. this.setData({
  27. typeToColumnsPlaceholder: {
  28. province: val[0] || '',
  29. city: val[1] || '',
  30. county: val[2] || '',
  31. },
  32. });
  33. },
  34. } }),
  35. data: {
  36. columns: [{ values: [] }, { values: [] }, { values: [] }],
  37. typeToColumnsPlaceholder: {},
  38. },
  39. mounted() {
  40. requestAnimationFrame(() => {
  41. this.setValues();
  42. });
  43. },
  44. methods: {
  45. getPicker() {
  46. if (this.picker == null) {
  47. this.picker = this.selectComponent('.van-area__picker');
  48. }
  49. return this.picker;
  50. },
  51. onCancel(event) {
  52. this.emit('cancel', event.detail);
  53. },
  54. onConfirm(event) {
  55. const { index } = event.detail;
  56. let { value } = event.detail;
  57. value = this.parseValues(value);
  58. this.emit('confirm', { value, index });
  59. },
  60. emit(type, detail) {
  61. detail.values = detail.value;
  62. delete detail.value;
  63. this.$emit(type, detail);
  64. },
  65. parseValues(values) {
  66. const { columnsPlaceholder } = this.data;
  67. return values.map((value, index) => {
  68. if (value &&
  69. (!value.code || value.name === columnsPlaceholder[index])) {
  70. return Object.assign(Object.assign({}, value), { code: '', name: '' });
  71. }
  72. return value;
  73. });
  74. },
  75. onChange(event) {
  76. var _a;
  77. const { index, picker, value } = event.detail;
  78. this.code = value[index].code;
  79. (_a = this.setValues()) === null || _a === void 0 ? void 0 : _a.then(() => {
  80. this.$emit('change', {
  81. picker,
  82. values: this.parseValues(picker.getValues()),
  83. index,
  84. });
  85. });
  86. },
  87. getConfig(type) {
  88. const { areaList } = this.data;
  89. return (areaList && areaList[`${type}_list`]) || {};
  90. },
  91. getList(type, code) {
  92. if (type !== 'province' && !code) {
  93. return [];
  94. }
  95. const { typeToColumnsPlaceholder } = this.data;
  96. const list = this.getConfig(type);
  97. let result = Object.keys(list).map((code) => ({
  98. code,
  99. name: list[code],
  100. }));
  101. if (code != null) {
  102. // oversea code
  103. if (code[0] === '9' && type === 'city') {
  104. code = '9';
  105. }
  106. result = result.filter((item) => item.code.indexOf(code) === 0);
  107. }
  108. if (typeToColumnsPlaceholder[type] && result.length) {
  109. // set columns placeholder
  110. const codeFill = type === 'province'
  111. ? ''
  112. : type === 'city'
  113. ? EMPTY_CODE.slice(2, 4)
  114. : EMPTY_CODE.slice(4, 6);
  115. result.unshift({
  116. code: `${code}${codeFill}`,
  117. name: typeToColumnsPlaceholder[type],
  118. });
  119. }
  120. return result;
  121. },
  122. getIndex(type, code) {
  123. let compareNum = type === 'province' ? 2 : type === 'city' ? 4 : 6;
  124. const list = this.getList(type, code.slice(0, compareNum - 2));
  125. // oversea code
  126. if (code[0] === '9' && type === 'province') {
  127. compareNum = 1;
  128. }
  129. code = code.slice(0, compareNum);
  130. for (let i = 0; i < list.length; i++) {
  131. if (list[i].code.slice(0, compareNum) === code) {
  132. return i;
  133. }
  134. }
  135. return 0;
  136. },
  137. setValues() {
  138. const picker = this.getPicker();
  139. if (!picker) {
  140. return;
  141. }
  142. let code = this.code || this.getDefaultCode();
  143. const provinceList = this.getList('province');
  144. const cityList = this.getList('city', code.slice(0, 2));
  145. const stack = [];
  146. const indexes = [];
  147. const { columnsNum } = this.data;
  148. if (columnsNum >= 1) {
  149. stack.push(picker.setColumnValues(0, provinceList, false));
  150. indexes.push(this.getIndex('province', code));
  151. }
  152. if (columnsNum >= 2) {
  153. stack.push(picker.setColumnValues(1, cityList, false));
  154. indexes.push(this.getIndex('city', code));
  155. if (cityList.length && code.slice(2, 4) === '00') {
  156. [{ code }] = cityList;
  157. }
  158. }
  159. if (columnsNum === 3) {
  160. stack.push(picker.setColumnValues(2, this.getList('county', code.slice(0, 4)), false));
  161. indexes.push(this.getIndex('county', code));
  162. }
  163. return Promise.all(stack)
  164. .catch(() => { })
  165. .then(() => picker.setIndexes(indexes))
  166. .catch(() => { });
  167. },
  168. getDefaultCode() {
  169. const { columnsPlaceholder } = this.data;
  170. if (columnsPlaceholder.length) {
  171. return EMPTY_CODE;
  172. }
  173. const countyCodes = Object.keys(this.getConfig('county'));
  174. if (countyCodes[0]) {
  175. return countyCodes[0];
  176. }
  177. const cityCodes = Object.keys(this.getConfig('city'));
  178. if (cityCodes[0]) {
  179. return cityCodes[0];
  180. }
  181. return '';
  182. },
  183. getValues() {
  184. const picker = this.getPicker();
  185. if (!picker) {
  186. return [];
  187. }
  188. return this.parseValues(picker.getValues().filter((value) => !!value));
  189. },
  190. getDetail() {
  191. const values = this.getValues();
  192. const area = {
  193. code: '',
  194. country: '',
  195. province: '',
  196. city: '',
  197. county: '',
  198. };
  199. if (!values.length) {
  200. return area;
  201. }
  202. const names = values.map((item) => item.name);
  203. area.code = values[values.length - 1].code;
  204. if (area.code[0] === '9') {
  205. area.country = names[1] || '';
  206. area.province = names[2] || '';
  207. }
  208. else {
  209. area.province = names[0] || '';
  210. area.city = names[1] || '';
  211. area.county = names[2] || '';
  212. }
  213. return area;
  214. },
  215. reset(code) {
  216. this.code = code || '';
  217. return this.setValues();
  218. },
  219. },
  220. });