index.js 6.3 KB

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