Skip to content

Commit 516d0b8

Browse files
committed
feat: flatten tree to match first node
1 parent e3ef3e5 commit 516d0b8

File tree

2 files changed

+18
-23
lines changed

2 files changed

+18
-23
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

src/OptionList.tsx

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import LegacyContext from './LegacyContext';
1010
import TreeSelectContext from './TreeSelectContext';
1111
import type { Key, SafeKey } from './interface';
1212
import { getAllKeys, isCheckDisabled } from './utils/valueUtil';
13+
import { flattenTreeData } from 'rc-tree/lib/utils/treeUtil';
1314

1415
const HIDDEN_STYLE = {
1516
width: 0,
@@ -76,7 +77,7 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
7677
(prev, next) => next[0] && prev[1] !== next[1],
7778
);
7879

79-
// ========================== Active Key Effect ==========================
80+
// ========================== Active Key ==========================
8081
const [activeKey, setActiveKey] = React.useState<Key>(null);
8182
const activeEntity = keyEntities[activeKey as SafeKey];
8283

@@ -161,31 +162,24 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
161162
nodes: EventDataNode<any>[],
162163
searchVal?: string,
163164
): EventDataNode<any> | null => {
164-
const findNode = (nodeList: EventDataNode<any>[]): EventDataNode<any> | null => {
165-
for (const node of nodeList) {
166-
if (node.disabled || node.selectable === false) {
167-
continue;
168-
}
169-
170-
if (searchVal) {
171-
if (filterTreeNode(node)) {
172-
return node;
173-
}
174-
} else if (!node.disabled && node.selectable !== false) {
175-
return node;
176-
}
165+
// Flatten the tree structure
166+
const flattenedNodes = flattenTreeData(nodes, true, fieldNames);
167+
168+
// Iterate through the flattened array to find the first matching node
169+
const matchedNode = flattenedNodes.find(node => {
170+
const rawNode = node.data as EventDataNode<any>;
171+
if (rawNode.disabled || rawNode.selectable === false) {
172+
return false;
173+
}
177174

178-
if (node[fieldNames.children]) {
179-
const matchInChildren = findNode(node[fieldNames.children]);
180-
if (matchInChildren) {
181-
return matchInChildren;
182-
}
183-
}
175+
if (searchVal) {
176+
return filterTreeNode(rawNode);
184177
}
185-
return null;
186-
};
187178

188-
return findNode(nodes);
179+
return true;
180+
});
181+
182+
return matchedNode ? (matchedNode.data as EventDataNode<any>) : null;
189183
};
190184

191185
// ========================== Active Key Effect ==========================

0 commit comments

Comments
 (0)