Skip to content

Commit 5d29cc0

Browse files
committed
revert: restore recursive node search
1 parent 134f4f4 commit 5d29cc0

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

src/OptionList.tsx

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -160,27 +160,32 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
160160
// eslint-disable-next-line react-hooks/exhaustive-deps
161161
}, [searchValue]);
162162

163-
// ========================== Flatten Tree Data ==========================
164-
const flattenedTreeData = React.useMemo(() => {
165-
return flattenTreeData(memoTreeData, mergedExpandedKeys, fieldNames);
166-
}, [memoTreeData, mergedExpandedKeys]);
167-
168163
// ========================== Get First Selectable Node ==========================
169-
const getFirstMatchingNode = (searchVal?: string): EventDataNode<any> | null => {
170-
const matchedNode = flattenedTreeData.find(node => {
171-
const rawNode = node.data as EventDataNode<any>;
172-
if (rawNode.disabled || rawNode.selectable === false) {
173-
return false;
164+
const getFirstMatchingNode = (
165+
nodes: EventDataNode<any>[],
166+
searchVal?: string,
167+
): EventDataNode<any> | null => {
168+
for (const node of nodes) {
169+
if (node.disabled || node.selectable === false) {
170+
continue;
174171
}
175172

176173
if (searchVal) {
177-
return filterTreeNode(rawNode);
174+
if (filterTreeNode(node)) {
175+
return node;
176+
}
177+
} else {
178+
return node;
178179
}
179180

180-
return true;
181-
});
182-
183-
return matchedNode ? (matchedNode.data as EventDataNode<any>) : null;
181+
if (node[fieldNames.children]) {
182+
const matchInChildren = getFirstMatchingNode(node[fieldNames.children], searchVal);
183+
if (matchInChildren) {
184+
return matchInChildren;
185+
}
186+
}
187+
}
188+
return null;
184189
};
185190

186191
// ========================== Active Key Effect ==========================
@@ -192,7 +197,7 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
192197

193198
// Prioritize activating the searched node
194199
if (searchValue) {
195-
const firstNode = getFirstMatchingNode(searchValue);
200+
const firstNode = getFirstMatchingNode(treeData, searchValue);
196201
setActiveKey(firstNode ? firstNode[fieldNames.value] : null);
197202
return;
198203
}
@@ -204,7 +209,7 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
204209
}
205210

206211
// If no search value and no checked nodes, activate the first node
207-
const firstNode = getFirstMatchingNode();
212+
const firstNode = getFirstMatchingNode(treeData);
208213
setActiveKey(firstNode ? firstNode[fieldNames.value] : null);
209214
}, [open, searchValue]);
210215

0 commit comments

Comments
 (0)