Update useTree.ts (#2602)

修复获取子节点出错
This commit is contained in:
JokeHe 2023-03-06 11:24:24 +08:00 committed by GitHub
parent 72dbe574d3
commit 8d218ec8d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -181,14 +181,16 @@ export function useTree(treeDataRef: Ref<TreeDataItem[]>, getFieldNames: Compute
function getSelectedNode(key: KeyType, list?: TreeItem[], selectedNode?: TreeItem | null) { function getSelectedNode(key: KeyType, list?: TreeItem[], selectedNode?: TreeItem | null) {
if (!key && key !== 0) return null; if (!key && key !== 0) return null;
const treeData = list || unref(treeDataRef); const treeData = list || unref(treeDataRef);
const { key: keyField, children: childrenField } = unref(getFieldNames);
if (!keyField) return;
treeData.forEach((item) => { treeData.forEach((item) => {
if (selectedNode?.key || selectedNode?.key === 0) return selectedNode; if (selectedNode?.key || selectedNode?.key === 0) return selectedNode;
if (item.key === key) { if (item[keyField] === key) {
selectedNode = item; selectedNode = item;
return; return;
} }
if (item.children && item.children.length) { if (item[childrenField!] && item[childrenField!].length) {
selectedNode = getSelectedNode(key, item.children, selectedNode); selectedNode = getSelectedNode(key, item[childrenField!], selectedNode);
} }
}); });
return selectedNode || null; return selectedNode || null;