index.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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(Object.assign({}, pickerProps), { value: String, areaList: {
  7. type: Object,
  8. value: {}
  9. }, columnsNum: {
  10. type: null,
  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.setData({
  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. value = JSON.parse(JSON.stringify(value));
  72. if (!value.code || value.name === columnsPlaceholder[index]) {
  73. value.code = '';
  74. value.name = '';
  75. }
  76. return value;
  77. });
  78. },
  79. onChange(event) {
  80. const { index, picker, value } = event.detail;
  81. this.code = value[index].code;
  82. let getValues = picker.getValues();
  83. getValues = this.parseOutputValues(getValues);
  84. this.setValues().then(() => {
  85. this.$emit('change', {
  86. picker,
  87. values: getValues,
  88. index
  89. });
  90. });
  91. },
  92. getConfig(type) {
  93. const { areaList } = this.data;
  94. return (areaList && areaList[`${type}_list`]) || {};
  95. },
  96. getList(type, code) {
  97. const { typeToColumnsPlaceholder } = this.data;
  98. let result = [];
  99. if (type !== 'province' && !code) {
  100. return result;
  101. }
  102. const list = this.getConfig(type);
  103. result = Object.keys(list).map(code => ({
  104. code,
  105. name: list[code]
  106. }));
  107. if (code) {
  108. // oversea code
  109. if (code[0] === '9' && type === 'city') {
  110. code = '9';
  111. }
  112. result = result.filter(item => item.code.indexOf(code) === 0);
  113. }
  114. if (typeToColumnsPlaceholder[type] && result.length) {
  115. // set columns placeholder
  116. const codeFill = type === 'province' ? '' : type === 'city' ? COLUMNSPLACEHOLDERCODE.slice(2, 4) : COLUMNSPLACEHOLDERCODE.slice(4, 6);
  117. result.unshift({
  118. code: `${code}${codeFill}`,
  119. name: typeToColumnsPlaceholder[type]
  120. });
  121. }
  122. return result;
  123. },
  124. getIndex(type, code) {
  125. let compareNum = type === 'province' ? 2 : type === 'city' ? 4 : 6;
  126. const list = this.getList(type, code.slice(0, compareNum - 2));
  127. // oversea code
  128. if (code[0] === '9' && type === 'province') {
  129. compareNum = 1;
  130. }
  131. code = code.slice(0, compareNum);
  132. for (let i = 0; i < list.length; i++) {
  133. if (list[i].code.slice(0, compareNum) === code) {
  134. return i;
  135. }
  136. }
  137. return 0;
  138. },
  139. setValues() {
  140. const county = this.getConfig('county');
  141. let { code } = this;
  142. if (!code) {
  143. if (this.data.columnsPlaceholder.length) {
  144. code = COLUMNSPLACEHOLDERCODE;
  145. }
  146. else if (Object.keys(county)[0]) {
  147. code = Object.keys(county)[0];
  148. }
  149. else {
  150. code = '';
  151. }
  152. }
  153. const province = this.getList('province');
  154. const city = this.getList('city', code.slice(0, 2));
  155. const picker = this.getPicker();
  156. if (!picker) {
  157. return;
  158. }
  159. const stack = [];
  160. stack.push(picker.setColumnValues(0, province, false));
  161. stack.push(picker.setColumnValues(1, city, false));
  162. if (city.length && code.slice(2, 4) === '00') {
  163. [{ code }] = city;
  164. }
  165. stack.push(picker.setColumnValues(2, this.getList('county', code.slice(0, 4)), false));
  166. return Promise.all(stack)
  167. .catch(() => { })
  168. .then(() => picker.setIndexes([
  169. this.getIndex('province', code),
  170. this.getIndex('city', code),
  171. this.getIndex('county', code)
  172. ]))
  173. .catch(() => { });
  174. },
  175. getValues() {
  176. const picker = this.getPicker();
  177. return picker ? picker.getValues().filter(value => !!value) : [];
  178. },
  179. getDetail() {
  180. const values = this.getValues();
  181. const area = {
  182. code: '',
  183. country: '',
  184. province: '',
  185. city: '',
  186. county: ''
  187. };
  188. if (!values.length) {
  189. return area;
  190. }
  191. const names = values.map((item) => item.name);
  192. area.code = values[values.length - 1].code;
  193. if (area.code[0] === '9') {
  194. area.country = names[1] || '';
  195. area.province = names[2] || '';
  196. }
  197. else {
  198. area.province = names[0] || '';
  199. area.city = names[1] || '';
  200. area.county = names[2] || '';
  201. }
  202. return area;
  203. },
  204. reset(code) {
  205. this.code = code || '';
  206. return this.setValues();
  207. }
  208. }
  209. });