@@ -10,7 +10,6 @@ import LegacyContext from './LegacyContext';
10
10
import TreeSelectContext from './TreeSelectContext' ;
11
11
import type { Key , SafeKey } from './interface' ;
12
12
import { getAllKeys , isCheckDisabled } from './utils/valueUtil' ;
13
- import { flattenTreeData } from 'rc-tree/lib/utils/treeUtil' ;
14
13
15
14
const HIDDEN_STYLE = {
16
15
width : 0 ,
@@ -77,10 +76,6 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
77
76
( prev , next ) => next [ 0 ] && prev [ 1 ] !== next [ 1 ] ,
78
77
) ;
79
78
80
- // ========================== Active Key ==========================
81
- const [ activeKey , setActiveKey ] = React . useState < Key > ( null ) ;
82
- const activeEntity = keyEntities [ activeKey as SafeKey ] ;
83
-
84
79
// ========================== Values ==========================
85
80
const mergedCheckedKeys = React . useMemo ( ( ) => {
86
81
if ( ! checkable ) {
@@ -93,7 +88,7 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
93
88
} ;
94
89
} , [ checkable , checkedKeys , halfCheckedKeys ] ) ;
95
90
96
- // ========================== Scroll Effect ==========================
91
+ // ========================== Scroll ==========================
97
92
React . useEffect ( ( ) => {
98
93
// Single mode should scroll to current key
99
94
if ( open && ! multiple && checkedKeys . length ) {
@@ -123,15 +118,6 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
123
118
}
124
119
} ;
125
120
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
-
135
121
// =========================== Keys ===========================
136
122
const [ expandedKeys , setExpandedKeys ] = React . useState < Key [ ] > ( treeDefaultExpandedKeys ) ;
137
123
const [ searchExpandedKeys , setSearchExpandedKeys ] = React . useState < Key [ ] > ( null ) ;
@@ -152,7 +138,15 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
152
138
}
153
139
} ;
154
140
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
+
156
150
React . useEffect ( ( ) => {
157
151
if ( searchValue ) {
158
152
setSearchExpandedKeys ( getAllKeys ( treeData , fieldNames ) ) ;
@@ -161,25 +155,14 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
161
155
} , [ searchValue ] ) ;
162
156
163
157
// ========================== 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 => {
168
159
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 ) {
178
161
return node ;
179
162
}
180
163
181
164
if ( node [ fieldNames . children ] ) {
182
- const matchInChildren = getFirstMatchingNode ( node [ fieldNames . children ] , searchVal ) ;
165
+ const matchInChildren = getFirstMatchingNode ( node [ fieldNames . children ] ) ;
183
166
if ( matchInChildren ) {
184
167
return matchInChildren ;
185
168
}
@@ -188,16 +171,18 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
188
171
return null ;
189
172
} ;
190
173
191
- // ========================== Active Key Effect ==========================
174
+ // ========================== Active ==========================
175
+ const [ activeKey , setActiveKey ] = React . useState < Key > ( null ) ;
176
+ const activeEntity = keyEntities [ activeKey as SafeKey ] ;
177
+
192
178
React . useEffect ( ( ) => {
193
179
if ( ! open ) {
194
- setActiveKey ( null ) ;
195
180
return ;
196
181
}
197
182
198
- // Prioritize activating the searched node
183
+ // // Prioritize activating the searched node
199
184
if ( searchValue ) {
200
- const firstNode = getFirstMatchingNode ( treeData , searchValue ) ;
185
+ const firstNode = getFirstMatchingNode ( memoTreeData ) ;
201
186
setActiveKey ( firstNode ? firstNode [ fieldNames . value ] : null ) ;
202
187
return ;
203
188
}
@@ -209,7 +194,7 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
209
194
}
210
195
211
196
// If no search value and no checked nodes, activate the first node
212
- const firstNode = getFirstMatchingNode ( treeData ) ;
197
+ const firstNode = getFirstMatchingNode ( memoTreeData ) ;
213
198
setActiveKey ( firstNode ? firstNode [ fieldNames . value ] : null ) ;
214
199
} , [ open , searchValue ] ) ;
215
200
0 commit comments