From be9d1855e4c5348f85bd582c87c9fcd4dbcc7eac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Th=C3=A9o=20LAURET?= Date: Fri, 13 Nov 2026 12:53:39 +0100 Subject: [PATCH] Fix Cmd+H typeahead infinite loop, guard missing controlled IDs, and export ITreeNode --- src/TreeView/index.tsx | 26 ++++++++++++++++++++++++-- src/TreeView/utils.ts | 2 +- src/index.ts | 4 +++- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/TreeView/index.tsx b/src/TreeView/index.tsx index 17f565ba..621324db 100644 --- a/src/TreeView/index.tsx +++ b/src/TreeView/index.tsx @@ -117,6 +117,7 @@ const useTree = ({ useEffect(() => { if (onSelect != null && onSelect !== noop) { for (const toggledId of toggledIds) { + if (!data.find((node) => node.id === toggledId)) continue; const isBranch = isBranchNode(data, toggledId) || !!getTreeNode(data, tabbableId)?.isBranch; @@ -144,7 +145,7 @@ const useTree = ({ useEffect(() => { if (onNodeSelect != null && onNodeSelect !== noop) { - if (lastManuallyToggled != null) { + if (lastManuallyToggled != null && data.find((node) => node.id === lastManuallyToggled)) { if (toggledIds.size) { onNodeSelect({ element: getTreeNode(data, lastManuallyToggled), @@ -163,6 +164,7 @@ const useTree = ({ const toggledExpandIds = symmetricDifference(expandedIds, prevExpandedIds); if (onExpand != null && onExpand !== noop) { for (const id of toggledExpandIds) { + if (!data.find((node) => node.id === id)) continue; onExpand({ element: getTreeNode(data, id), isExpanded: expandedIds.has(id), @@ -189,6 +191,7 @@ const useTree = ({ const toggledExpandIds = symmetricDifference(expandedIds, prevExpandedIds); if (onLoadData) { for (const id of toggledExpandIds) { + if (!data.find((node) => node.id === id)) continue; onLoadData({ element: getTreeNode(data, id), isExpanded: expandedIds.has(id), @@ -200,6 +203,7 @@ const useTree = ({ } if (prevData !== data && togglableSelect && propagateSelect) { for (const id of expandedIds) { + if (!data.find((node) => node.id === id)) continue; selectedIds.has(id) && dispatch({ type: treeTypes.changeSelectMany, @@ -265,13 +269,16 @@ const useTree = ({ toggledControlledIds.size && dispatch({ type: treeTypes.controlledSelectMany, - ids: controlledSelectedIds, + ids: controlledSelectedIds.filter((id) => + data.some((node) => node.id === id) + ), multiSelect, }); } if (selectedIdsToUse) { for (const id of selectedIdsToUse) { + if (!data.find((node) => node.id === id)) continue; propagateSelect && !disabledIds.has(id) && dispatch({ @@ -300,6 +307,7 @@ const useTree = ({ //controlled collapsing if (diffCollapseIds.size) { for (const id of diffCollapseIds) { + if (!data.find((node) => node.id === id)) continue; if (isBranchNode(data, id) || getTreeNode(data, id).isBranch) { const ids = [id, ...getDescendants(data, id, new Set())]; dispatch({ @@ -313,6 +321,7 @@ const useTree = ({ //controlled expanding if (diffExpandedIds.size) { for (const id of diffExpandedIds) { + if (!data.find((node) => node.id === id)) continue; if (isBranchNode(data, id) || getTreeNode(data, id).isBranch) { const parentId = getParent(data, id); if (parentId) { @@ -571,6 +580,8 @@ export interface ITreeViewProps { treeState: ITreeViewState; dispatch: React.Dispatch; }) => void; + /** Custom onKeyDown event handler */ + onKeyDown?: React.KeyboardEventHandler; /** Id of the node to focus */ focusedId?: NodeId; } @@ -725,6 +736,9 @@ const handleKeyDown = ({ }) => (event: React.KeyboardEvent) => { const element = getTreeNode(data, tabbableId); const id = element.id; + if (event.metaKey || event.altKey) { + return; + } if (event.ctrlKey) { if (event.key === "a" && clickAction !== clickActions.focus) { event.preventDefault(); @@ -973,12 +987,20 @@ const handleKeyDown = ({ return; default: if (event.key.length === 1) { + const visited = new Set([id]); let currentId = getNextAccessible(data, id, expandedIds); while (currentId !== id) { if (currentId == null) { currentId = getTreeParent(data).children[0]; + if (currentId == null || visited.has(currentId)) { + break; + } continue; } + if (visited.has(currentId)) { + break; + } + visited.add(currentId); if ( getTreeNode(data, currentId).name[0].toLowerCase() === event.key.toLowerCase() diff --git a/src/TreeView/utils.ts b/src/TreeView/utils.ts index a7b53c22..c67aed8b 100644 --- a/src/TreeView/utils.ts +++ b/src/TreeView/utils.ts @@ -307,7 +307,7 @@ export type IFlatMetadata = Record< string | number | boolean | undefined | null >; -interface ITreeNode { +export interface ITreeNode { id?: NodeId; name: string; isBranch?: boolean; diff --git a/src/index.ts b/src/index.ts index d02f1444..c238c8b9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,10 +16,12 @@ import { IBranchProps, LeafProps, } from "./TreeView/types"; -import { flattenTree } from "./TreeView/utils"; +import { flattenTree, ITreeNode, IFlatMetadata } from "./TreeView/utils"; export { flattenTree, + ITreeNode, + IFlatMetadata, ITreeViewProps, INode, NodeId,