cascader.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. import { SuperComponent, wxComponent } from '../common/src/index';
  8. import config from '../common/config';
  9. import props from './props';
  10. const { prefix } = config;
  11. const name = `${prefix}-cascader`;
  12. const defaultOptionLabel = '选择选项';
  13. let Cascader = class Cascader extends SuperComponent {
  14. constructor() {
  15. super(...arguments);
  16. this.externalClasses = [`${prefix}-class`];
  17. this.properties = props;
  18. this.data = {
  19. prefix,
  20. name,
  21. stepIndex: 0,
  22. selectedIndexes: [],
  23. selectedValue: [],
  24. defaultOptionLabel,
  25. steps: [defaultOptionLabel],
  26. };
  27. this.observers = {
  28. visible(v) {
  29. if (v) {
  30. const $tabs = this.selectComponent('#tabs');
  31. $tabs === null || $tabs === void 0 ? void 0 : $tabs.setTrack();
  32. }
  33. },
  34. 'value, options'() {
  35. this.initWithValue();
  36. },
  37. 'selectedIndexes, options'() {
  38. var _a, _b, _c, _d;
  39. const { options, selectedIndexes, keys } = this.data;
  40. const selectedValue = [];
  41. const steps = [];
  42. const items = [options];
  43. if (options.length > 0) {
  44. for (let i = 0, size = selectedIndexes.length; i < size; i += 1) {
  45. const index = selectedIndexes[i];
  46. const next = items[i][index];
  47. selectedValue.push(next[(_a = keys === null || keys === void 0 ? void 0 : keys.value) !== null && _a !== void 0 ? _a : 'value']);
  48. steps.push(next[(_b = keys === null || keys === void 0 ? void 0 : keys.label) !== null && _b !== void 0 ? _b : 'label']);
  49. if (next[(_c = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _c !== void 0 ? _c : 'children']) {
  50. items.push(next[(_d = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _d !== void 0 ? _d : 'children']);
  51. }
  52. }
  53. }
  54. if (steps.length < items.length) {
  55. steps.push(defaultOptionLabel);
  56. }
  57. this.setData({
  58. steps,
  59. items,
  60. selectedValue,
  61. stepIndex: items.length - 1,
  62. });
  63. },
  64. };
  65. this.methods = {
  66. initWithValue() {
  67. if (this.data.value != null) {
  68. const selectedIndexes = this.getIndexesByValue(this.data.options, this.data.value);
  69. if (selectedIndexes) {
  70. this.setData({ selectedIndexes });
  71. }
  72. }
  73. },
  74. getIndexesByValue(options, value) {
  75. var _a, _b, _c;
  76. const { keys } = this.data;
  77. for (let i = 0, size = options.length; i < size; i += 1) {
  78. const opt = options[i];
  79. if (opt[(_a = keys === null || keys === void 0 ? void 0 : keys.value) !== null && _a !== void 0 ? _a : 'value'] === value) {
  80. return [i];
  81. }
  82. if (opt[(_b = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _b !== void 0 ? _b : 'children']) {
  83. const res = this.getIndexesByValue(opt[(_c = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _c !== void 0 ? _c : 'children'], value);
  84. if (res) {
  85. return [i, ...res];
  86. }
  87. }
  88. }
  89. },
  90. hide() {
  91. this.setData({ visible: false });
  92. },
  93. onStepClick(e) {
  94. const { index } = e.currentTarget.dataset;
  95. this.setData({ stepIndex: index });
  96. },
  97. onTabChange(e) {
  98. const { value } = e.detail;
  99. this.setData({
  100. stepIndex: value,
  101. });
  102. },
  103. handleSelect(e) {
  104. var _a, _b, _c;
  105. const { level } = e.target.dataset;
  106. const { value } = e.detail;
  107. const { selectedIndexes, items, keys } = this.data;
  108. 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; });
  109. const item = items[level][index];
  110. if (item.disabled) {
  111. return;
  112. }
  113. selectedIndexes[level] = index;
  114. selectedIndexes.length = level + 1;
  115. this.triggerEvent('pick', item[(_a = keys === null || keys === void 0 ? void 0 : keys.value) !== null && _a !== void 0 ? _a : 'value'], index);
  116. 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) {
  117. this.setData({ selectedIndexes });
  118. }
  119. else {
  120. this.setData({ selectedIndexes }, () => {
  121. var _a;
  122. const { items } = this.data;
  123. this.triggerEvent('change', {
  124. value: item[(_a = keys === null || keys === void 0 ? void 0 : keys.value) !== null && _a !== void 0 ? _a : 'value'],
  125. selectedOptions: items.map((item, index) => item[selectedIndexes[index]]),
  126. });
  127. });
  128. this.hide();
  129. }
  130. },
  131. };
  132. }
  133. };
  134. Cascader = __decorate([
  135. wxComponent()
  136. ], Cascader);
  137. export default Cascader;