Skip to content

Commit 4640c4d

Browse files
committed
chore: remove unnecessary logic
1 parent 5d29cc0 commit 4640c4d

File tree

2 files changed

+28
-35
lines changed

2 files changed

+28
-35
lines changed

examples/basic.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ class Demo extends React.Component {
216216
console.log('onPopupScroll:', evt.target);
217217
}}
218218
/>
219+
219220
<h2>single select (just select children)</h2>
220221
<TreeSelect
221222
style={{ width: 300 }}
@@ -232,6 +233,7 @@ class Demo extends React.Component {
232233
filterTreeNode={false}
233234
onChange={this.onChangeChildren}
234235
/>
236+
235237
<h2>multiple select</h2>
236238
<TreeSelect
237239
style={{ width: 300 }}
@@ -247,6 +249,7 @@ class Demo extends React.Component {
247249
onSelect={this.onSelect}
248250
allowClear
249251
/>
252+
250253
<h2>check select</h2>
251254
<TreeSelect
252255
open
@@ -278,6 +281,7 @@ class Demo extends React.Component {
278281
return `${valueList.length} rest...`;
279282
}}
280283
/>
284+
281285
<h2>labelInValue & show path</h2>
282286
<TreeSelect
283287
style={{ width: 500 }}
@@ -295,6 +299,7 @@ class Demo extends React.Component {
295299
filterTreeNode={false}
296300
onChange={this.onChangeLV}
297301
/>
302+
298303
<h2>use treeDataSimpleMode</h2>
299304
<TreeSelect
300305
style={{ width: 300 }}
@@ -318,6 +323,7 @@ class Demo extends React.Component {
318323
this.onSelect(...args);
319324
}}
320325
/>
326+
321327
<h2>Testing in extreme conditions (Boundary conditions test) </h2>
322328
<TreeSelect
323329
style={{ width: 200 }}
@@ -341,6 +347,7 @@ class Demo extends React.Component {
341347
]}
342348
onChange={(val, ...args) => console.log(val, ...args)}
343349
/>
350+
344351
<h2>use TreeNode Component (not recommend)</h2>
345352
<TreeSelect
346353
style={{ width: 200 }}
@@ -377,6 +384,7 @@ class Demo extends React.Component {
377384
</TreeNode>
378385
<TreeNode value="same value3" title="same title" key="0-3" />
379386
</TreeSelect>
387+
380388
<h2>title render</h2>
381389
<TreeSelect<{ label: string }>
382390
open

src/OptionList.tsx

+20-35
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ 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';
1413

1514
const HIDDEN_STYLE = {
1615
width: 0,
@@ -77,10 +76,6 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
7776
(prev, next) => next[0] && prev[1] !== next[1],
7877
);
7978

80-
// ========================== Active Key ==========================
81-
const [activeKey, setActiveKey] = React.useState<Key>(null);
82-
const activeEntity = keyEntities[activeKey as SafeKey];
83-
8479
// ========================== Values ==========================
8580
const mergedCheckedKeys = React.useMemo(() => {
8681
if (!checkable) {
@@ -93,7 +88,7 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
9388
};
9489
}, [checkable, checkedKeys, halfCheckedKeys]);
9590

96-
// ========================== Scroll Effect ==========================
91+
// ========================== Scroll ==========================
9792
React.useEffect(() => {
9893
// Single mode should scroll to current key
9994
if (open && !multiple && checkedKeys.length) {
@@ -123,15 +118,6 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
123118
}
124119
};
125120

126-
// ========================== Search ==========================
127-
const lowerSearchValue = String(searchValue).toLowerCase();
128-
const filterTreeNode = (treeNode: EventDataNode<any>) => {
129-
if (!lowerSearchValue) {
130-
return false;
131-
}
132-
return String(treeNode[treeNodeFilterProp]).toLowerCase().includes(lowerSearchValue);
133-
};
134-
135121
// =========================== Keys ===========================
136122
const [expandedKeys, setExpandedKeys] = React.useState<Key[]>(treeDefaultExpandedKeys);
137123
const [searchExpandedKeys, setSearchExpandedKeys] = React.useState<Key[]>(null);
@@ -152,7 +138,15 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
152138
}
153139
};
154140

155-
// ========================== Search Effect ==========================
141+
// ========================== Search ==========================
142+
const lowerSearchValue = String(searchValue).toLowerCase();
143+
const filterTreeNode = (treeNode: EventDataNode<any>) => {
144+
if (!lowerSearchValue) {
145+
return false;
146+
}
147+
return String(treeNode[treeNodeFilterProp]).toLowerCase().includes(lowerSearchValue);
148+
};
149+
156150
React.useEffect(() => {
157151
if (searchValue) {
158152
setSearchExpandedKeys(getAllKeys(treeData, fieldNames));
@@ -161,25 +155,14 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
161155
}, [searchValue]);
162156

163157
// ========================== Get First Selectable Node ==========================
164-
const getFirstMatchingNode = (
165-
nodes: EventDataNode<any>[],
166-
searchVal?: string,
167-
): EventDataNode<any> | null => {
158+
const getFirstMatchingNode = (nodes: EventDataNode<any>[]): EventDataNode<any> | null => {
168159
for (const node of nodes) {
169-
if (node.disabled || node.selectable === false) {
170-
continue;
171-
}
172-
173-
if (searchVal) {
174-
if (filterTreeNode(node)) {
175-
return node;
176-
}
177-
} else {
160+
if (!node.disabled && node.selectable !== false) {
178161
return node;
179162
}
180163

181164
if (node[fieldNames.children]) {
182-
const matchInChildren = getFirstMatchingNode(node[fieldNames.children], searchVal);
165+
const matchInChildren = getFirstMatchingNode(node[fieldNames.children]);
183166
if (matchInChildren) {
184167
return matchInChildren;
185168
}
@@ -188,16 +171,18 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
188171
return null;
189172
};
190173

191-
// ========================== Active Key Effect ==========================
174+
// ========================== Active ==========================
175+
const [activeKey, setActiveKey] = React.useState<Key>(null);
176+
const activeEntity = keyEntities[activeKey as SafeKey];
177+
192178
React.useEffect(() => {
193179
if (!open) {
194-
setActiveKey(null);
195180
return;
196181
}
197182

198-
// Prioritize activating the searched node
183+
// // Prioritize activating the searched node
199184
if (searchValue) {
200-
const firstNode = getFirstMatchingNode(treeData, searchValue);
185+
const firstNode = getFirstMatchingNode(memoTreeData);
201186
setActiveKey(firstNode ? firstNode[fieldNames.value] : null);
202187
return;
203188
}
@@ -209,7 +194,7 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
209194
}
210195

211196
// If no search value and no checked nodes, activate the first node
212-
const firstNode = getFirstMatchingNode(treeData);
197+
const firstNode = getFirstMatchingNode(memoTreeData);
213198
setActiveKey(firstNode ? firstNode[fieldNames.value] : null);
214199
}, [open, searchValue]);
215200

0 commit comments

Comments
 (0)