cascader.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  8. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  9. return new (P || (P = Promise))(function (resolve, reject) {
  10. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  11. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  12. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  13. step((generator = generator.apply(thisArg, _arguments || [])).next());
  14. });
  15. };
  16. import { SuperComponent, wxComponent } from '../common/src/index';
  17. import config from '../common/config';
  18. import props from './props';
  19. import { getRect } from '../common/utils';
  20. const { prefix } = config;
  21. const name = `${prefix}-cascader`;
  22. const defaultOptionLabel = '选择选项';
  23. let Cascader = class Cascader extends SuperComponent {
  24. constructor() {
  25. super(...arguments);
  26. this.externalClasses = [`${prefix}-class`];
  27. this.options = {
  28. multipleSlots: true,
  29. };
  30. this.properties = props;
  31. this.data = {
  32. prefix,
  33. name,
  34. stepIndex: 0,
  35. selectedIndexes: [],
  36. selectedValue: [],
  37. defaultOptionLabel,
  38. scrollTopList: [],
  39. steps: [defaultOptionLabel],
  40. };
  41. this.observers = {
  42. visible(v) {
  43. if (v) {
  44. const $tabs = this.selectComponent('#tabs');
  45. $tabs === null || $tabs === void 0 ? void 0 : $tabs.setTrack();
  46. this.updateScrollTop();
  47. }
  48. },
  49. 'value, options'() {
  50. this.initWithValue();
  51. },
  52. 'selectedIndexes, options'() {
  53. var _a, _b, _c, _d;
  54. const { options, selectedIndexes, keys } = this.data;
  55. const selectedValue = [];
  56. const steps = [];
  57. const items = [options];
  58. if (options.length > 0) {
  59. for (let i = 0, size = selectedIndexes.length; i < size; i += 1) {
  60. const index = selectedIndexes[i];
  61. const next = items[i][index];
  62. selectedValue.push(next[(_a = keys === null || keys === void 0 ? void 0 : keys.value) !== null && _a !== void 0 ? _a : 'value']);
  63. steps.push(next[(_b = keys === null || keys === void 0 ? void 0 : keys.label) !== null && _b !== void 0 ? _b : 'label']);
  64. if (next[(_c = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _c !== void 0 ? _c : 'children']) {
  65. items.push(next[(_d = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _d !== void 0 ? _d : 'children']);
  66. }
  67. }
  68. }
  69. if (steps.length < items.length) {
  70. steps.push(defaultOptionLabel);
  71. }
  72. this.setData({
  73. steps,
  74. items,
  75. selectedValue,
  76. stepIndex: items.length - 1,
  77. });
  78. },
  79. stepIndex() {
  80. return __awaiter(this, void 0, void 0, function* () {
  81. const { visible } = this.data;
  82. if (visible) {
  83. this.updateScrollTop();
  84. }
  85. });
  86. },
  87. };
  88. this.methods = {
  89. initWithValue() {
  90. if (this.data.value != null) {
  91. const selectedIndexes = this.getIndexesByValue(this.data.options, this.data.value);
  92. if (selectedIndexes) {
  93. this.setData({ selectedIndexes });
  94. }
  95. }
  96. },
  97. getIndexesByValue(options, value) {
  98. var _a, _b, _c;
  99. const { keys } = this.data;
  100. for (let i = 0, size = options.length; i < size; i += 1) {
  101. const opt = options[i];
  102. if (opt[(_a = keys === null || keys === void 0 ? void 0 : keys.value) !== null && _a !== void 0 ? _a : 'value'] === value) {
  103. return [i];
  104. }
  105. if (opt[(_b = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _b !== void 0 ? _b : 'children']) {
  106. const res = this.getIndexesByValue(opt[(_c = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _c !== void 0 ? _c : 'children'], value);
  107. if (res) {
  108. return [i, ...res];
  109. }
  110. }
  111. }
  112. },
  113. updateScrollTop() {
  114. const { visible, items, selectedIndexes, stepIndex } = this.data;
  115. if (visible) {
  116. getRect(this, '.cascader-radio-group-0').then((rect) => {
  117. var _a;
  118. const eachRadioHeight = rect.height / ((_a = items[0]) === null || _a === void 0 ? void 0 : _a.length);
  119. this.setData({
  120. [`scrollTopList[${stepIndex}]`]: eachRadioHeight * selectedIndexes[stepIndex],
  121. });
  122. });
  123. }
  124. },
  125. hide() {
  126. this.setData({ visible: false });
  127. },
  128. onStepClick(e) {
  129. const { index } = e.currentTarget.dataset;
  130. this.setData({ stepIndex: index });
  131. },
  132. onTabChange(e) {
  133. const { value } = e.detail;
  134. this.setData({
  135. stepIndex: value,
  136. });
  137. },
  138. handleSelect(e) {
  139. var _a, _b, _c;
  140. const { level } = e.target.dataset;
  141. const { value } = e.detail;
  142. const { selectedIndexes, items, keys } = this.data;
  143. const index = items[level].findIndex((item) => { var _a; return item[(_a = keys === null || keys === void 0 ? void 0 : keys.value) !== null && _a !== void 0 ? _a : 'value'] === value; });
  144. const item = items[level][index];
  145. if (item.disabled) {
  146. return;
  147. }
  148. selectedIndexes[level] = index;
  149. selectedIndexes.length = level + 1;
  150. this.triggerEvent('pick', item[(_a = keys === null || keys === void 0 ? void 0 : keys.value) !== null && _a !== void 0 ? _a : 'value'], index);
  151. if ((_c = item === null || item === void 0 ? void 0 : item[(_b = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _b !== void 0 ? _b : 'children']) === null || _c === void 0 ? void 0 : _c.length) {
  152. this.setData({ selectedIndexes });
  153. }
  154. else {
  155. this.setData({ selectedIndexes }, () => {
  156. var _a;
  157. const { items } = this.data;
  158. this.triggerEvent('change', {
  159. value: item[(_a = keys === null || keys === void 0 ? void 0 : keys.value) !== null && _a !== void 0 ? _a : 'value'],
  160. selectedOptions: items.map((item, index) => item[selectedIndexes[index]]),
  161. });
  162. });
  163. this.hide();
  164. }
  165. },
  166. };
  167. }
  168. };
  169. Cascader = __decorate([
  170. wxComponent()
  171. ], Cascader);
  172. export default Cascader;