index.js 6.3 KB

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