index.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import { VantComponent } from '../common/component';
  2. import { pickerProps } from '../picker/shared';
  3. const COLUMNSPLACEHOLDERCODE = '000000';
  4. VantComponent({
  5. classes: ['active-class', 'toolbar-class', 'column-class'],
  6. props: Object.assign({}, pickerProps, { value: String, areaList: {
  7. type: Object,
  8. value: {}
  9. }, columnsNum: {
  10. type: [String, Number],
  11. value: 3
  12. }, columnsPlaceholder: {
  13. type: Array,
  14. observer(val) {
  15. this.setData({
  16. typeToColumnsPlaceholder: {
  17. province: val[0] || '',
  18. city: val[1] || '',
  19. county: val[2] || '',
  20. }
  21. });
  22. }
  23. } }),
  24. data: {
  25. columns: [{ values: [] }, { values: [] }, { values: [] }],
  26. displayColumns: [{ values: [] }, { values: [] }, { values: [] }],
  27. typeToColumnsPlaceholder: {}
  28. },
  29. watch: {
  30. value(value) {
  31. this.code = value;
  32. this.setValues();
  33. },
  34. areaList: 'setValues',
  35. columnsNum(value) {
  36. this.set({
  37. displayColumns: this.data.columns.slice(0, +value)
  38. });
  39. }
  40. },
  41. mounted() {
  42. setTimeout(() => {
  43. this.setValues();
  44. }, 0);
  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.parseOutputValues(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. // parse output columns data
  68. parseOutputValues(values) {
  69. const { columnsPlaceholder } = this.data;
  70. return values.map((value, index) => {
  71. // save undefined value
  72. if (!value)
  73. return value;
  74. value = JSON.parse(JSON.stringify(value));
  75. if (!value.code || value.name === columnsPlaceholder[index]) {
  76. value.code = '';
  77. value.name = '';
  78. }
  79. return value;
  80. });
  81. },
  82. onChange(event) {
  83. const { index, picker, value } = event.detail;
  84. this.code = value[index].code;
  85. let getValues = picker.getValues();
  86. getValues = this.parseOutputValues(getValues);
  87. this.setValues().then(() => {
  88. this.$emit('change', {
  89. picker,
  90. values: getValues,
  91. index
  92. });
  93. });
  94. },
  95. getConfig(type) {
  96. const { areaList } = this.data;
  97. return (areaList && areaList[`${type}_list`]) || {};
  98. },
  99. getList(type, code) {
  100. const { typeToColumnsPlaceholder } = this.data;
  101. let result = [];
  102. if (type !== 'province' && !code) {
  103. return result;
  104. }
  105. const list = this.getConfig(type);
  106. result = Object.keys(list).map(code => ({
  107. code,
  108. name: list[code]
  109. }));
  110. if (code) {
  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 = type === 'province' ? '' : type === 'city' ? COLUMNSPLACEHOLDERCODE.slice(2, 4) : COLUMNSPLACEHOLDERCODE.slice(4, 6);
  120. result.unshift({
  121. code: `${code}${codeFill}`,
  122. name: typeToColumnsPlaceholder[type]
  123. });
  124. }
  125. return result;
  126. },
  127. getIndex(type, code) {
  128. let compareNum = type === 'province' ? 2 : type === 'city' ? 4 : 6;
  129. const list = this.getList(type, code.slice(0, compareNum - 2));
  130. // oversea code
  131. if (code[0] === '9' && type === 'province') {
  132. compareNum = 1;
  133. }
  134. code = code.slice(0, compareNum);
  135. for (let i = 0; i < list.length; i++) {
  136. if (list[i].code.slice(0, compareNum) === code) {
  137. return i;
  138. }
  139. }
  140. return 0;
  141. },
  142. setValues() {
  143. const county = this.getConfig('county');
  144. let { code } = this;
  145. if (!code) {
  146. if (this.data.columnsPlaceholder.length) {
  147. code = COLUMNSPLACEHOLDERCODE;
  148. }
  149. else if (Object.keys(county)[0]) {
  150. code = Object.keys(county)[0];
  151. }
  152. else {
  153. code = '';
  154. }
  155. }
  156. const province = this.getList('province');
  157. const city = this.getList('city', code.slice(0, 2));
  158. const picker = this.getPicker();
  159. if (!picker) {
  160. return;
  161. }
  162. const stack = [];
  163. stack.push(picker.setColumnValues(0, province, false));
  164. stack.push(picker.setColumnValues(1, city, false));
  165. if (city.length && code.slice(2, 4) === '00') {
  166. [{ code }] = city;
  167. }
  168. stack.push(picker.setColumnValues(2, this.getList('county', code.slice(0, 4)), false));
  169. return Promise.all(stack)
  170. .catch(() => { })
  171. .then(() => picker.setIndexes([
  172. this.getIndex('province', code),
  173. this.getIndex('city', code),
  174. this.getIndex('county', code)
  175. ]))
  176. .catch(() => { });
  177. },
  178. getValues() {
  179. const picker = this.getPicker();
  180. return picker ? picker.getValues().filter(value => !!value) : [];
  181. },
  182. getDetail() {
  183. const values = this.getValues();
  184. const area = {
  185. code: '',
  186. country: '',
  187. province: '',
  188. city: '',
  189. county: ''
  190. };
  191. if (!values.length) {
  192. return area;
  193. }
  194. const names = values.map((item) => item.name);
  195. area.code = values[values.length - 1].code;
  196. if (area.code[0] === '9') {
  197. area.country = names[1] || '';
  198. area.province = names[2] || '';
  199. }
  200. else {
  201. area.province = names[0] || '';
  202. area.city = names[1] || '';
  203. area.county = names[2] || '';
  204. }
  205. return area;
  206. },
  207. reset() {
  208. this.code = '';
  209. return this.setValues();
  210. }
  211. }
  212. });