index.js 7.5 KB

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