优化BasicLeftTree
parent
8cb25fdbe3
commit
926e80c103
|
|
@ -1,5 +1,6 @@
|
||||||
import { Input, Tree } from "antd";
|
import { Input, Tree } from "antd";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
|
import { getTreeNodePaths } from "../../../utils";
|
||||||
|
|
||||||
const { Search } = Input;
|
const { Search } = Input;
|
||||||
|
|
||||||
|
|
@ -71,35 +72,16 @@ const BasicLeftTree = (props) => {
|
||||||
setAutoExpandParent(false);
|
setAutoExpandParent(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
const getNodePaths = (data, targetId, idKey, childrenKey, path = [], isIncludeOneself) => {
|
|
||||||
for (let i = 0; i < data.length; i++) {
|
|
||||||
const node = data[i];
|
|
||||||
const newPath = [...path, node];
|
|
||||||
|
|
||||||
// 找到目标节点,根据isIncludeOneself决定是否包含自身
|
|
||||||
if (node[idKey] === targetId) {
|
|
||||||
if (isIncludeOneself)
|
|
||||||
return newPath; // 包含自身
|
|
||||||
else
|
|
||||||
return path; // 不包含自身,只返回父节点路径
|
|
||||||
}
|
|
||||||
|
|
||||||
// 递归查找子节点
|
|
||||||
if (node[childrenKey] && node[childrenKey].length > 0) {
|
|
||||||
const result = getNodePaths(node[childrenKey], targetId, idKey, childrenKey, newPath, isIncludeOneself);
|
|
||||||
if (result) {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleSelect = (selectedKeys, event) => {
|
const handleSelect = (selectedKeys, event) => {
|
||||||
if (selectedKeys.length > 0) {
|
if (selectedKeys.length > 0) {
|
||||||
const selectedNodeId = selectedKeys[0];
|
const selectedNodeId = selectedKeys[0];
|
||||||
const parentNodes = getNodePaths(treeData, selectedNodeId, idKey, childrenKey, onGetNodePathsIsIncludeOneself);
|
const parentNodes = getTreeNodePaths({
|
||||||
|
data: treeData,
|
||||||
|
targetId: selectedNodeId,
|
||||||
|
idKey,
|
||||||
|
childrenKey,
|
||||||
|
isIncludeOneself: onGetNodePathsIsIncludeOneself,
|
||||||
|
});
|
||||||
onGetNodePaths?.(parentNodes);
|
onGetNodePaths?.(parentNodes);
|
||||||
}
|
}
|
||||||
onSelect?.(selectedKeys, event);
|
onSelect?.(selectedKeys, event);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue