tree-select.js 3.9 KB

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