UNPKG

tdesign-react

Version:
194 lines (188 loc) 7.65 kB
/** * tdesign v1.15.1 * (c) 2025 tdesign * @license MIT */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var toConsumableArray = require('../../_chunks/dep-e4e1901e.js'); var slicedToArray = require('../../_chunks/dep-8e4d656d.js'); var createClass = require('../../_chunks/dep-69792df2.js'); var defineProperty = require('../../_chunks/dep-0006fcfa.js'); var React = require('react'); require('../../_chunks/dep-00b49251.js'); require('../../_chunks/dep-667ac7af.js'); function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } var MenuTree = /*#__PURE__*/function () { function MenuTree(children) { var expandMutex = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; createClass._classCallCheck(this, MenuTree); defineProperty._defineProperty(this, "menuMap", /* @__PURE__ */new Map()); defineProperty._defineProperty(this, "expandedSet", /* @__PURE__ */new Set()); this.buildTree(children); this.expandMutex = expandMutex; } return createClass._createClass(MenuTree, [{ key: "buildTree", value: function buildTree(children) { this.menuMap.clear(); this.traverseChildren(children); } }, { key: "traverseChildren", value: function traverseChildren(node, parentValue) { var _this = this; if (Array.isArray(node)) { node.forEach(function (child) { return _this.traverseChildren(child, parentValue); }); return; } if (! /*#__PURE__*/React.isValidElement(node)) return; var _node$props = node.props, value = _node$props.value, children = _node$props.children; if (value !== void 0) { if (!this.menuMap.has(value)) { this.menuMap.set(value, { parent: parentValue, children: [] }); } if (parentValue !== void 0) { var parentNode = this.menuMap.get(parentValue); if (parentNode && !parentNode.children.includes(value)) { parentNode.children.push(value); } } if (children) { this.traverseChildren(children, value); } } else if (children) { this.traverseChildren(children, parentValue); } } }, { key: "setExpanded", value: function setExpanded(expandedList) { this.expandedSet = new Set(expandedList); } }, { key: "getExpanded", value: function getExpanded() { return Array.from(this.expandedSet); } }, { key: "isExpanded", value: function isExpanded(value) { return this.expandedSet.has(value); } }, { key: "getAncestors", value: function getAncestors(value) { var _this$menuMap$get; var ancestors = []; var current = (_this$menuMap$get = this.menuMap.get(value)) === null || _this$menuMap$get === void 0 ? void 0 : _this$menuMap$get.parent; while (current !== void 0) { var _this$menuMap$get2; ancestors.unshift(current); current = (_this$menuMap$get2 = this.menuMap.get(current)) === null || _this$menuMap$get2 === void 0 ? void 0 : _this$menuMap$get2.parent; } return ancestors; } }, { key: "getDescendants", value: function getDescendants(value) { var _this2 = this; var descendants = []; if (!this.menuMap.get(value)) return descendants; var _dfs = function dfs(nodeValue) { var node = _this2.menuMap.get(nodeValue); if (!node) return; node.children.forEach(function (child) { descendants.push(child); _dfs(child); }); }; _dfs(value); return descendants; } }, { key: "getSiblings", value: function getSiblings(value) { var node = this.menuMap.get(value); if (!node) return []; var parentValue = node.parent; if (parentValue === void 0) { var rootNodes = []; var _iterator = _createForOfIteratorHelper(this.menuMap.entries()), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var _step$value = slicedToArray._slicedToArray(_step.value, 2), nodeValue = _step$value[0], info = _step$value[1]; if (info.parent === void 0 && nodeValue !== value) { rootNodes.push(nodeValue); } } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } return rootNodes; } var parent = this.menuMap.get(parentValue); return parent ? parent.children.filter(function (child) { return child !== value; }) : []; } }, { key: "expandNode", value: function expandNode(value) { var _this3 = this; var isCurrExpanded = this.isExpanded(value); var nextExpanded = this.getExpanded(); if (isCurrExpanded) { var descendants = this.getDescendants(value); var toClose = /* @__PURE__ */new Set([value].concat(toConsumableArray._toConsumableArray(descendants))); nextExpanded = nextExpanded.filter(function (item) { return !toClose.has(item); }); } else { var expandedSet = new Set(nextExpanded); if (this.expandMutex) { var siblings = this.getSiblings(value); var siblingsToClose = /* @__PURE__ */new Set(); siblings.forEach(function (sibling) { if (expandedSet.has(sibling)) { siblingsToClose.add(sibling); var siblingDescendants = _this3.getDescendants(sibling); siblingDescendants.forEach(function (desc) { return siblingsToClose.add(desc); }); } }); nextExpanded = nextExpanded.filter(function (item) { return !siblingsToClose.has(item); }); } var ancestors = this.getAncestors(value); ancestors.forEach(function (ancestor) { if (!nextExpanded.includes(ancestor)) { nextExpanded.push(ancestor); } }); if (!nextExpanded.includes(value)) { nextExpanded.push(value); } } this.setExpanded(nextExpanded); } }]); }(); exports.MenuTree = MenuTree; //# sourceMappingURL=getMenuTree.js.map