index.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <template>
  2. <uni-shadow-root class="vant-area-index"><van-picker class="van-area__picker" active-class="active-class" toolbar-class="toolbar-class" column-class="column-class" :show-toolbar="showToolbar" value-key="name" :title="title" :loading="loading" :columns="computed.displayColumns(columns, columnsNum)" :item-height="itemHeight" :visible-item-count="visibleItemCount" :cancel-button-text="cancelButtonText" :confirm-button-text="confirmButtonText" @change="onChange" @confirm="onConfirm" @cancel="onCancel"></van-picker></uni-shadow-root>
  3. </template>
  4. <wxs src="./index.wxs" module="computed"></wxs>
  5. <script>
  6. import VanPicker from '../picker/index.vue'
  7. global['__wxVueOptions'] = {components:{'van-picker': VanPicker}}
  8. global['__wxRoute'] = 'vant/area/index'
  9. import { VantComponent } from '../common/component';
  10. import { pickerProps } from '../picker/shared';
  11. import { requestAnimationFrame } from '../common/utils';
  12. const EMPTY_CODE = '000000';
  13. VantComponent({
  14. classes: ['active-class', 'toolbar-class', 'column-class'],
  15. props: Object.assign(Object.assign({}, pickerProps), { showToolbar: {
  16. type: Boolean,
  17. value: true,
  18. }, value: {
  19. type: String,
  20. observer(value) {
  21. this.code = value;
  22. this.setValues();
  23. },
  24. }, areaList: {
  25. type: Object,
  26. value: {},
  27. observer: 'setValues',
  28. }, columnsNum: {
  29. type: null,
  30. value: 3,
  31. }, columnsPlaceholder: {
  32. type: Array,
  33. observer(val) {
  34. this.setData({
  35. typeToColumnsPlaceholder: {
  36. province: val[0] || '',
  37. city: val[1] || '',
  38. county: val[2] || '',
  39. },
  40. });
  41. },
  42. } }),
  43. data: {
  44. columns: [{ 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.parseValues(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. parseValues(values) {
  74. const { columnsPlaceholder } = this.data;
  75. return values.map((value, index) => {
  76. if (value &&
  77. (!value.code || value.name === columnsPlaceholder[index])) {
  78. return Object.assign(Object.assign({}, value), { code: '', name: '' });
  79. }
  80. return value;
  81. });
  82. },
  83. onChange(event) {
  84. var _a;
  85. const { index, picker, value } = event.detail;
  86. this.code = value[index].code;
  87. (_a = this.setValues()) === null || _a === void 0 ? void 0 : _a.then(() => {
  88. this.$emit('change', {
  89. picker,
  90. values: this.parseValues(picker.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. if (type !== 'province' && !code) {
  101. return [];
  102. }
  103. const { typeToColumnsPlaceholder } = this.data;
  104. const list = this.getConfig(type);
  105. let result = Object.keys(list).map((code) => ({
  106. code,
  107. name: list[code],
  108. }));
  109. if (code != null) {
  110. // oversea code
  111. if (code[0] === '9' && type === 'city') {
  112. code = '9';
  113. }
  114. result = result.filter((item) => item.code.indexOf(code) === 0);
  115. }
  116. if (typeToColumnsPlaceholder[type] && result.length) {
  117. // set columns placeholder
  118. const codeFill = type === 'province'
  119. ? ''
  120. : type === 'city'
  121. ? EMPTY_CODE.slice(2, 4)
  122. : EMPTY_CODE.slice(4, 6);
  123. result.unshift({
  124. code: `${code}${codeFill}`,
  125. name: typeToColumnsPlaceholder[type],
  126. });
  127. }
  128. return result;
  129. },
  130. getIndex(type, code) {
  131. let compareNum = type === 'province' ? 2 : type === 'city' ? 4 : 6;
  132. const list = this.getList(type, code.slice(0, compareNum - 2));
  133. // oversea code
  134. if (code[0] === '9' && type === 'province') {
  135. compareNum = 1;
  136. }
  137. code = code.slice(0, compareNum);
  138. for (let i = 0; i < list.length; i++) {
  139. if (list[i].code.slice(0, compareNum) === code) {
  140. return i;
  141. }
  142. }
  143. return 0;
  144. },
  145. setValues() {
  146. const picker = this.getPicker();
  147. if (!picker) {
  148. return;
  149. }
  150. let code = this.code || this.getDefaultCode();
  151. const provinceList = this.getList('province');
  152. const cityList = this.getList('city', code.slice(0, 2));
  153. const stack = [];
  154. const indexes = [];
  155. const { columnsNum } = this.data;
  156. if (columnsNum >= 1) {
  157. stack.push(picker.setColumnValues(0, provinceList, false));
  158. indexes.push(this.getIndex('province', code));
  159. }
  160. if (columnsNum >= 2) {
  161. stack.push(picker.setColumnValues(1, cityList, false));
  162. indexes.push(this.getIndex('city', code));
  163. if (cityList.length && code.slice(2, 4) === '00') {
  164. [{ code }] = cityList;
  165. }
  166. }
  167. if (columnsNum === 3) {
  168. stack.push(picker.setColumnValues(2, this.getList('county', code.slice(0, 4)), false));
  169. indexes.push(this.getIndex('county', code));
  170. }
  171. return Promise.all(stack)
  172. .catch(() => { })
  173. .then(() => picker.setIndexes(indexes))
  174. .catch(() => { });
  175. },
  176. getDefaultCode() {
  177. const { columnsPlaceholder } = this.data;
  178. if (columnsPlaceholder.length) {
  179. return EMPTY_CODE;
  180. }
  181. const countyCodes = Object.keys(this.getConfig('county'));
  182. if (countyCodes[0]) {
  183. return countyCodes[0];
  184. }
  185. const cityCodes = Object.keys(this.getConfig('city'));
  186. if (cityCodes[0]) {
  187. return cityCodes[0];
  188. }
  189. return '';
  190. },
  191. getValues() {
  192. const picker = this.getPicker();
  193. if (!picker) {
  194. return [];
  195. }
  196. return this.parseValues(picker.getValues().filter((value) => !!value));
  197. },
  198. getDetail() {
  199. const values = this.getValues();
  200. const area = {
  201. code: '',
  202. country: '',
  203. province: '',
  204. city: '',
  205. county: '',
  206. };
  207. if (!values.length) {
  208. return area;
  209. }
  210. const names = values.map((item) => item.name);
  211. area.code = values[values.length - 1].code;
  212. if (area.code[0] === '9') {
  213. area.country = names[1] || '';
  214. area.province = names[2] || '';
  215. }
  216. else {
  217. area.province = names[0] || '';
  218. area.city = names[1] || '';
  219. area.county = names[2] || '';
  220. }
  221. return area;
  222. },
  223. reset(code) {
  224. this.code = code || '';
  225. return this.setValues();
  226. },
  227. },
  228. });
  229. export default global['__wxComponents']['vant/area/index']
  230. </script>
  231. <style platform="mp-weixin">
  232. @import '../common/index.css';
  233. </style>