tree-select.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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}-tree-select`;
  12. let TreeSelect = class TreeSelect extends SuperComponent {
  13. constructor() {
  14. super(...arguments);
  15. this.externalClasses = [
  16. `${prefix}-class`,
  17. `${prefix}-class-left-column`,
  18. `${prefix}-class-left-item`,
  19. `${prefix}-class-middle-item`,
  20. `${prefix}-class-right-column`,
  21. `${prefix}-class-right-item`,
  22. `${prefix}-class-right-item-label`,
  23. ];
  24. this.options = {
  25. multipleSlots: true,
  26. };
  27. this.data = {
  28. prefix,
  29. classPrefix: name,
  30. scrollIntoView: null,
  31. };
  32. this.properties = Object.assign(Object.assign({}, props), { customValue: {
  33. type: null,
  34. value: null,
  35. } });
  36. this.controlledProps = [
  37. {
  38. key: 'value',
  39. event: 'change',
  40. },
  41. ];
  42. this.observers = {
  43. 'value, customValue, options, keys, multiple'() {
  44. this.buildTreeOptions();
  45. },
  46. };
  47. this.lifetimes = {
  48. ready() {
  49. this.getScrollIntoView('init');
  50. },
  51. };
  52. this.methods = {
  53. buildTreeOptions() {
  54. const { options, value, defaultValue, customValue, multiple, keys } = this.data;
  55. const treeOptions = [];
  56. let level = -1;
  57. let node = { children: options };
  58. if (options.length === 0)
  59. return;
  60. while (node && node.children) {
  61. level += 1;
  62. const list = node.children.map((item) => ({
  63. label: item[(keys === null || keys === void 0 ? void 0 : keys.label) || 'label'],
  64. value: item[(keys === null || keys === void 0 ? void 0 : keys.value) || 'value'],
  65. children: item.children,
  66. }));
  67. const thisValue = (customValue === null || customValue === void 0 ? void 0 : customValue[level]) || (value === null || value === void 0 ? void 0 : value[level]);
  68. treeOptions.push([...list]);
  69. if (thisValue == null) {
  70. const [firstChild] = list;
  71. node = firstChild;
  72. }
  73. else {
  74. const child = list.find((child) => child.value === thisValue);
  75. node = child !== null && child !== void 0 ? child : list[0];
  76. }
  77. }
  78. const leafLevel = Math.max(0, level);
  79. if (multiple) {
  80. const finalValue = customValue || value || defaultValue;
  81. if (finalValue[leafLevel] != null && !Array.isArray(finalValue[leafLevel])) {
  82. throw TypeError('应传入数组类型的 value');
  83. }
  84. }
  85. this.setData({
  86. innerValue: customValue ||
  87. (treeOptions === null || treeOptions === void 0 ? void 0 : treeOptions.map((ele, idx) => {
  88. const v = idx === treeOptions.length - 1 && multiple ? [ele[0].value] : ele[0].value;
  89. return (value === null || value === void 0 ? void 0 : value[idx]) || v;
  90. })),
  91. leafLevel,
  92. treeOptions,
  93. });
  94. },
  95. getScrollIntoView(status) {
  96. const { value, customValue, scrollIntoView } = this.data;
  97. if (status === 'init') {
  98. const _value = customValue || value;
  99. const scrollIntoView = Array.isArray(_value)
  100. ? _value.map((item) => {
  101. return Array.isArray(item) ? item[0] : item;
  102. })
  103. : [_value];
  104. this.setData({
  105. scrollIntoView,
  106. });
  107. }
  108. else {
  109. if (scrollIntoView === null)
  110. return;
  111. this.setData({
  112. scrollIntoView: null,
  113. });
  114. }
  115. },
  116. onRootChange(e) {
  117. const { innerValue } = this.data;
  118. const { value: itemValue } = e.detail;
  119. this.getScrollIntoView('none');
  120. innerValue[0] = itemValue;
  121. this._trigger('change', { value: innerValue, level: 0 });
  122. },
  123. handleTreeClick(e) {
  124. const { level, value: itemValue } = e.currentTarget.dataset;
  125. const { innerValue } = this.data;
  126. innerValue[level] = itemValue;
  127. this.getScrollIntoView('none');
  128. this._trigger('change', { value: innerValue, level: 1 });
  129. },
  130. handleRadioChange(e) {
  131. const { innerValue } = this.data;
  132. const { value: itemValue } = e.detail;
  133. const { level } = e.target.dataset;
  134. innerValue[level] = itemValue;
  135. this.getScrollIntoView('none');
  136. this._trigger('change', { value: innerValue, level });
  137. },
  138. };
  139. }
  140. };
  141. TreeSelect = __decorate([
  142. wxComponent()
  143. ], TreeSelect);
  144. export default TreeSelect;