index.js 5.0 KB

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