diff --git a/src/components/Tree/src/hooks/useTree.ts b/src/components/Tree/src/hooks/useTree.ts index 17345a260..dd04ec17e 100644 --- a/src/components/Tree/src/hooks/useTree.ts +++ b/src/components/Tree/src/hooks/useTree.ts @@ -181,14 +181,16 @@ export function useTree(treeDataRef: Ref, getFieldNames: Compute function getSelectedNode(key: KeyType, list?: TreeItem[], selectedNode?: TreeItem | null) { if (!key && key !== 0) return null; const treeData = list || unref(treeDataRef); + const { key: keyField, children: childrenField } = unref(getFieldNames); + if (!keyField) return; treeData.forEach((item) => { if (selectedNode?.key || selectedNode?.key === 0) return selectedNode; - if (item.key === key) { + if (item[keyField] === key) { selectedNode = item; return; } - if (item.children && item.children.length) { - selectedNode = getSelectedNode(key, item.children, selectedNode); + if (item[childrenField!] && item[childrenField!].length) { + selectedNode = getSelectedNode(key, item[childrenField!], selectedNode); } }); return selectedNode || null;