index.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 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), { value: {
  16. type: String,
  17. observer(value) {
  18. this.code = value;
  19. this.setValues();
  20. },
  21. }, areaList: {
  22. type: Object,
  23. value: {},
  24. observer: 'setValues',
  25. }, columnsNum: {
  26. type: null,
  27. value: 3,
  28. }, columnsPlaceholder: {
  29. type: Array,
  30. observer(val) {
  31. this.setData({
  32. typeToColumnsPlaceholder: {
  33. province: val[0] || '',
  34. city: val[1] || '',
  35. county: val[2] || '',
  36. },
  37. });
  38. },
  39. } }),
  40. data: {
  41. columns: [{ values: [] }, { values: [] }, { values: [] }],
  42. typeToColumnsPlaceholder: {},
  43. },
  44. mounted() {
  45. requestAnimationFrame(() => {
  46. this.setValues();
  47. });
  48. },
  49. methods: {
  50. getPicker() {
  51. if (this.picker == null) {
  52. this.picker = this.selectComponent('.van-area__picker');
  53. }
  54. return this.picker;
  55. },
  56. onCancel(event) {
  57. this.emit('cancel', event.detail);
  58. },
  59. onConfirm(event) {
  60. const { index } = event.detail;
  61. let { value } = event.detail;
  62. value = this.parseValues(value);
  63. this.emit('confirm', { value, index });
  64. },
  65. emit(type, detail) {
  66. detail.values = detail.value;
  67. delete detail.value;
  68. this.$emit(type, detail);
  69. },
  70. parseValues(values) {
  71. const { columnsPlaceholder } = this.data;
  72. return values.map((value, index) => {
  73. if (value &&
  74. (!value.code || value.name === columnsPlaceholder[index])) {
  75. return Object.assign(Object.assign({}, value), { code: '', name: '' });
  76. }
  77. return value;
  78. });
  79. },
  80. onChange(event) {
  81. var _a;
  82. const { index, picker, value } = event.detail;
  83. this.code = value[index].code;
  84. (_a = this.setValues()) === null || _a === void 0 ? void 0 : _a.then(() => {
  85. this.$emit('change', {
  86. picker,
  87. values: this.parseValues(picker.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. if (type !== 'province' && !code) {
  98. return [];
  99. }
  100. const { typeToColumnsPlaceholder } = this.data;
  101. const list = this.getConfig(type);
  102. let result = Object.keys(list).map((code) => ({
  103. code,
  104. name: list[code],
  105. }));
  106. if (code != null) {
  107. // oversea code
  108. if (code[0] === '9' && type === 'city') {
  109. code = '9';
  110. }
  111. result = result.filter((item) => item.code.indexOf(code) === 0);
  112. }
  113. if (typeToColumnsPlaceholder[type] && result.length) {
  114. // set columns placeholder
  115. const codeFill = type === 'province'
  116. ? ''
  117. : type === 'city'
  118. ? EMPTY_CODE.slice(2, 4)
  119. : EMPTY_CODE.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 picker = this.getPicker();
  144. if (!picker) {
  145. return;
  146. }
  147. let code = this.code || this.getDefaultCode();
  148. const provinceList = this.getList('province');
  149. const cityList = this.getList('city', code.slice(0, 2));
  150. const stack = [];
  151. const indexes = [];
  152. const { columnsNum } = this.data;
  153. if (columnsNum >= 1) {
  154. stack.push(picker.setColumnValues(0, provinceList, false));
  155. indexes.push(this.getIndex('province', code));
  156. }
  157. if (columnsNum >= 2) {
  158. stack.push(picker.setColumnValues(1, cityList, false));
  159. indexes.push(this.getIndex('city', code));
  160. if (cityList.length && code.slice(2, 4) === '00') {
  161. [{ code }] = cityList;
  162. }
  163. }
  164. if (columnsNum === 3) {
  165. stack.push(picker.setColumnValues(2, this.getList('county', code.slice(0, 4)), false));
  166. indexes.push(this.getIndex('county', code));
  167. }
  168. return Promise.all(stack)
  169. .catch(() => { })
  170. .then(() => picker.setIndexes(indexes))
  171. .catch(() => { });
  172. },
  173. getDefaultCode() {
  174. const { columnsPlaceholder } = this.data;
  175. if (columnsPlaceholder.length) {
  176. return EMPTY_CODE;
  177. }
  178. const countyCodes = Object.keys(this.getConfig('county'));
  179. if (countyCodes[0]) {
  180. return countyCodes[0];
  181. }
  182. const cityCodes = Object.keys(this.getConfig('city'));
  183. if (cityCodes[0]) {
  184. return cityCodes[0];
  185. }
  186. return '';
  187. },
  188. getValues() {
  189. const picker = this.getPicker();
  190. if (!picker) {
  191. return [];
  192. }
  193. return this.parseValues(picker.getValues().filter((value) => !!value));
  194. },
  195. getDetail() {
  196. const values = this.getValues();
  197. const area = {
  198. code: '',
  199. country: '',
  200. province: '',
  201. city: '',
  202. county: '',
  203. };
  204. if (!values.length) {
  205. return area;
  206. }
  207. const names = values.map((item) => item.name);
  208. area.code = values[values.length - 1].code;
  209. if (area.code[0] === '9') {
  210. area.country = names[1] || '';
  211. area.province = names[2] || '';
  212. }
  213. else {
  214. area.province = names[0] || '';
  215. area.city = names[1] || '';
  216. area.county = names[2] || '';
  217. }
  218. return area;
  219. },
  220. reset(code) {
  221. this.code = code || '';
  222. return this.setValues();
  223. },
  224. },
  225. });
  226. export default global['__wxComponents']['vant/area/index']
  227. </script>
  228. <style platform="mp-weixin">
  229. @import '../common/index.css';
  230. </style>