@@ -102,42 +102,21 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
102
102
// eslint-disable-next-line react-hooks/exhaustive-deps
103
103
} , [ open ] ) ;
104
104
105
- // ========================== Search ==========================
106
- const lowerSearchValue = String ( searchValue ) . toLowerCase ( ) ;
107
- const filterTreeNode = ( treeNode : EventDataNode < any > ) => {
108
- if ( ! lowerSearchValue ) {
109
- return false ;
110
- }
111
- return String ( treeNode [ treeNodeFilterProp ] ) . toLowerCase ( ) . includes ( lowerSearchValue ) ;
112
- } ;
113
-
114
- // =========================== Keys ===========================
115
- const [ expandedKeys , setExpandedKeys ] = React . useState < Key [ ] > ( treeDefaultExpandedKeys ) ;
116
- const [ searchExpandedKeys , setSearchExpandedKeys ] = React . useState < Key [ ] > ( null ) ;
117
-
118
- const mergedExpandedKeys = React . useMemo ( ( ) => {
119
- if ( treeExpandedKeys ) {
120
- return [ ...treeExpandedKeys ] ;
121
- }
122
- return searchValue ? searchExpandedKeys : expandedKeys ;
123
- } , [ expandedKeys , searchExpandedKeys , treeExpandedKeys , searchValue ] ) ;
124
-
105
+ // =========================== Search Effect ===========================
125
106
React . useEffect ( ( ) => {
126
107
if ( searchValue ) {
127
108
setSearchExpandedKeys ( getAllKeys ( treeData , fieldNames ) ) ;
109
+
110
+ const firstMatchNode = getFirstMatchNode ( memoTreeData ) ;
111
+ if ( firstMatchNode ) {
112
+ setActiveKey ( firstMatchNode [ fieldNames . value ] ) ;
113
+ } else {
114
+ setActiveKey ( null ) ;
115
+ }
128
116
}
129
117
// eslint-disable-next-line react-hooks/exhaustive-deps
130
118
} , [ searchValue ] ) ;
131
119
132
- const onInternalExpand = ( keys : Key [ ] ) => {
133
- setExpandedKeys ( keys ) ;
134
- setSearchExpandedKeys ( keys ) ;
135
-
136
- if ( onTreeExpand ) {
137
- onTreeExpand ( keys ) ;
138
- }
139
- } ;
140
-
141
120
// ========================== Events ==========================
142
121
const onListMouseDown : React . MouseEventHandler < HTMLDivElement > = event => {
143
122
event . preventDefault ( ) ;
@@ -159,7 +138,7 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
159
138
}
160
139
} ;
161
140
162
- const getFirstMatchNode = ( nodes : EventDataNode < any > [ ] ) : EventDataNode < any > | null => {
141
+ const getFirstMatchNode = ( nodes : EventDataNode < any > ) : EventDataNode < any > | null => {
163
142
for ( const node of nodes ) {
164
143
if ( filterTreeNode ( node ) && ! node . disabled ) {
165
144
return node ;
@@ -174,6 +153,35 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
174
153
return null ;
175
154
} ;
176
155
156
+ // ========================== Search ==========================
157
+ const lowerSearchValue = String ( searchValue ) . toLowerCase ( ) ;
158
+ const filterTreeNode = ( treeNode : EventDataNode < any > ) => {
159
+ if ( ! lowerSearchValue ) {
160
+ return false ;
161
+ }
162
+ return String ( treeNode [ treeNodeFilterProp ] ) . toLowerCase ( ) . includes ( lowerSearchValue ) ;
163
+ } ;
164
+
165
+ // =========================== Keys ===========================
166
+ const [ expandedKeys , setExpandedKeys ] = React . useState < Key [ ] > ( treeDefaultExpandedKeys ) ;
167
+ const [ searchExpandedKeys , setSearchExpandedKeys ] = React . useState < Key [ ] > ( null ) ;
168
+
169
+ const mergedExpandedKeys = React . useMemo ( ( ) => {
170
+ if ( treeExpandedKeys ) {
171
+ return [ ...treeExpandedKeys ] ;
172
+ }
173
+ return searchValue ? searchExpandedKeys : expandedKeys ;
174
+ } , [ expandedKeys , searchExpandedKeys , treeExpandedKeys , searchValue ] ) ;
175
+
176
+ const onInternalExpand = ( keys : Key [ ] ) => {
177
+ setExpandedKeys ( keys ) ;
178
+ setSearchExpandedKeys ( keys ) ;
179
+
180
+ if ( onTreeExpand ) {
181
+ onTreeExpand ( keys ) ;
182
+ }
183
+ } ;
184
+
177
185
// ========================= Keyboard =========================
178
186
React . useImperativeHandle ( ref , ( ) => ( {
179
187
scrollTo : treeRef . current ?. scrollTo ,
@@ -190,18 +198,7 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
190
198
191
199
// >>> Select item
192
200
case KeyCode . ENTER : {
193
- if ( searchValue ) {
194
- const firstMatchNode = getFirstMatchNode ( memoTreeData ) ;
195
-
196
- if ( firstMatchNode ) {
197
- if ( firstMatchNode . selectable !== false ) {
198
- onInternalSelect ( null , {
199
- node : { key : firstMatchNode [ fieldNames . value ] } ,
200
- selected : ! checkedKeys . includes ( firstMatchNode [ fieldNames . value ] ) ,
201
- } ) ;
202
- }
203
- }
204
- } else if ( activeEntity ) {
201
+ if ( activeEntity ) {
205
202
const { selectable, value } = activeEntity ?. node || { } ;
206
203
if ( selectable !== false ) {
207
204
onInternalSelect ( null , {
0 commit comments