index.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import { VantComponent } from '../common/component';
  2. VantComponent({
  3. classes: ['active-class', 'toolbar-class', 'column-class'],
  4. props: {
  5. title: String,
  6. value: String,
  7. loading: Boolean,
  8. cancelButtonText: String,
  9. confirmButtonText: String,
  10. itemHeight: {
  11. type: Number,
  12. value: 44
  13. },
  14. visibleItemCount: {
  15. type: Number,
  16. value: 5
  17. },
  18. columnsNum: {
  19. type: [String, Number],
  20. value: 3
  21. },
  22. areaList: {
  23. type: Object,
  24. value: {}
  25. }
  26. },
  27. data: {
  28. columns: [{ values: [] }, { values: [] }, { values: [] }],
  29. displayColumns: [{ values: [] }, { values: [] }, { values: [] }]
  30. },
  31. watch: {
  32. value(value) {
  33. this.code = value;
  34. this.setValues();
  35. },
  36. areaList: 'setValues',
  37. columnsNum(value) {
  38. this.set({
  39. displayColumns: this.data.columns.slice(0, +value)
  40. });
  41. }
  42. },
  43. methods: {
  44. getPicker() {
  45. if (this.picker == null) {
  46. this.picker = this.selectComponent('.van-area__picker');
  47. }
  48. return this.picker;
  49. },
  50. onCancel(event) {
  51. this.emit('cancel', event.detail);
  52. },
  53. onConfirm(event) {
  54. this.emit('confirm', event.detail);
  55. },
  56. emit(type, detail) {
  57. detail.values = detail.value;
  58. delete detail.value;
  59. this.$emit(type, detail);
  60. },
  61. onChange(event) {
  62. const { index, picker, value } = event.detail;
  63. this.code = value[index].code;
  64. this.setValues().then(() => {
  65. this.$emit('change', {
  66. picker,
  67. values: picker.getValues(),
  68. index
  69. });
  70. });
  71. },
  72. getConfig(type) {
  73. const { areaList } = this.data;
  74. return (areaList && areaList[`${type}_list`]) || {};
  75. },
  76. getList(type, code) {
  77. let result = [];
  78. if (type !== 'province' && !code) {
  79. return result;
  80. }
  81. const list = this.getConfig(type);
  82. result = Object.keys(list).map(code => ({
  83. code,
  84. name: list[code]
  85. }));
  86. if (code) {
  87. // oversea code
  88. if (code[0] === '9' && type === 'city') {
  89. code = '9';
  90. }
  91. result = result.filter(item => item.code.indexOf(code) === 0);
  92. }
  93. return result;
  94. },
  95. getIndex(type, code) {
  96. let compareNum = type === 'province' ? 2 : type === 'city' ? 4 : 6;
  97. const list = this.getList(type, code.slice(0, compareNum - 2));
  98. // oversea code
  99. if (code[0] === '9' && type === 'province') {
  100. compareNum = 1;
  101. }
  102. code = code.slice(0, compareNum);
  103. for (let i = 0; i < list.length; i++) {
  104. if (list[i].code.slice(0, compareNum) === code) {
  105. return i;
  106. }
  107. }
  108. return 0;
  109. },
  110. setValues() {
  111. const county = this.getConfig('county');
  112. let code = this.code || Object.keys(county)[0] || '';
  113. const province = this.getList('province');
  114. const city = this.getList('city', code.slice(0, 2));
  115. const picker = this.getPicker();
  116. if (!picker) {
  117. return;
  118. }
  119. const stack = [];
  120. stack.push(picker.setColumnValues(0, province, false));
  121. stack.push(picker.setColumnValues(1, city, false));
  122. if (city.length && code.slice(2, 4) === '00') {
  123. ;
  124. [{ code }] = city;
  125. }
  126. stack.push(picker.setColumnValues(2, this.getList('county', code.slice(0, 4)), false));
  127. return Promise.all(stack)
  128. .catch(() => { })
  129. .then(() => picker.setIndexes([
  130. this.getIndex('province', code),
  131. this.getIndex('city', code),
  132. this.getIndex('county', code)
  133. ]))
  134. .catch(() => { });
  135. },
  136. getValues() {
  137. const picker = this.getPicker();
  138. return picker ? picker.getValues().filter(value => !!value) : [];
  139. },
  140. getDetail() {
  141. const values = this.getValues();
  142. const area = {
  143. code: '',
  144. country: '',
  145. province: '',
  146. city: '',
  147. county: ''
  148. };
  149. if (!values.length) {
  150. return area;
  151. }
  152. const names = values.map((item) => item.name);
  153. area.code = values[values.length - 1].code;
  154. if (area.code[0] === '9') {
  155. area.country = names[1] || '';
  156. area.province = names[2] || '';
  157. }
  158. else {
  159. area.province = names[0] || '';
  160. area.city = names[1] || '';
  161. area.county = names[2] || '';
  162. }
  163. return area;
  164. },
  165. reset() {
  166. this.code = '';
  167. return this.setValues();
  168. }
  169. }
  170. });