储能智慧云小程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

tree-select.js 4.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 = [`${prefix}-class`];
  16. this.options = {
  17. multipleSlots: true,
  18. };
  19. this.data = {
  20. prefix,
  21. classPrefix: name,
  22. };
  23. this.properties = props;
  24. this.controlledProps = [
  25. {
  26. key: 'value',
  27. event: 'change',
  28. },
  29. ];
  30. this.observers = {
  31. 'value, options, keys, multiple'() {
  32. this.buildTreeOptions();
  33. },
  34. };
  35. this.methods = {
  36. buildTreeOptions() {
  37. const { options, value, multiple, keys } = this.data;
  38. const treeOptions = [];
  39. let level = -1;
  40. let node = { children: options };
  41. if (options.length === 0 || (Array.isArray(value) && value.length === 0))
  42. return;
  43. while (node && node.children) {
  44. level += 1;
  45. const list = node.children.map((item) => ({
  46. label: item[(keys === null || keys === void 0 ? void 0 : keys.label) || 'label'],
  47. value: item[(keys === null || keys === void 0 ? void 0 : keys.value) || 'value'],
  48. children: item.children,
  49. }));
  50. const thisValue = value === null || value === void 0 ? void 0 : value[level];
  51. treeOptions.push([...list]);
  52. if (thisValue == null) {
  53. const [firstChild] = list;
  54. node = firstChild;
  55. }
  56. else {
  57. const child = list.find((child) => child.value === thisValue);
  58. node = child !== null && child !== void 0 ? child : list[0];
  59. }
  60. }
  61. const leafLevel = Math.max(0, level);
  62. if (multiple) {
  63. const finalValue = this.data.value || this.data.defaultValue;
  64. if (finalValue[leafLevel] != null && !Array.isArray(finalValue[leafLevel])) {
  65. throw TypeError('应传入数组类型的 value');
  66. }
  67. }
  68. this.setData({
  69. leafLevel,
  70. treeOptions,
  71. });
  72. },
  73. onRootChange(e) {
  74. const { value } = this.data;
  75. const { value: itemValue } = e.detail;
  76. value[0] = itemValue;
  77. this._trigger('change', { value, level: 0 });
  78. },
  79. handleTreeClick(e) {
  80. const { level, value: itemValue } = e.currentTarget.dataset;
  81. const { value } = this.data;
  82. value[level] = itemValue;
  83. this._trigger('change', { value, level: 1 });
  84. },
  85. handleRadioChange(e) {
  86. const { value } = this.data;
  87. const { value: itemValue } = e.detail;
  88. const { level } = e.target.dataset;
  89. value[level] = itemValue;
  90. this._trigger('change', { value, level });
  91. },
  92. };
  93. }
  94. };
  95. TreeSelect = __decorate([
  96. wxComponent()
  97. ], TreeSelect);
  98. export default TreeSelect;