1
- import React from 'react' ;
1
+ import * as React from 'react' ;
2
+ import { useMemo } from 'react' ;
2
3
import generateSelector , { SelectProps , RefSelectProps } from 'rc-select/lib/generate' ;
3
4
import { getLabeledValue } from 'rc-select/lib/utils/valueUtil' ;
4
5
import { convertDataToEntities } from 'rc-tree/lib/utils/treeUtil' ;
5
6
import { conductCheck } from 'rc-tree/lib/utils/conductUtil' ;
6
7
import { IconType } from 'rc-tree/lib/interface' ;
7
8
import { FilterFunc , INTERNAL_PROPS_MARK } from 'rc-select/lib/interface/generator' ;
9
+ import useMergedState from 'rc-util/lib/hooks/useMergedState' ;
8
10
import warning from 'rc-util/lib/warning' ;
9
11
import OptionList from './OptionList' ;
10
12
import TreeNode from './TreeNode' ;
@@ -206,37 +208,42 @@ const RefTreeSelect = React.forwardRef<RefSelectProps, TreeSelectProps>((props,
206
208
// ======================= Tree Data =======================
207
209
// Legacy both support `label` or `title` if not set.
208
210
// We have to fallback to function to handle this
211
+ const getTreeNodeTitle = ( node : DataNode ) : React . ReactNode => {
212
+ if ( ! treeData ) {
213
+ return node . title ;
214
+ }
215
+ return node . label || node . title ;
216
+ } ;
217
+
209
218
const getTreeNodeLabelProp = ( node : DataNode ) : React . ReactNode => {
210
219
if ( treeNodeLabelProp ) {
211
220
return node [ treeNodeLabelProp ] ;
212
221
}
213
222
214
- if ( ! treeData ) {
215
- return node . title ;
216
- }
217
- return node . label || node . title ;
223
+ return getTreeNodeTitle ( node ) ;
218
224
} ;
219
225
220
226
const mergedTreeData = useTreeData ( treeData , children , {
221
- getLabelProp : getTreeNodeLabelProp ,
227
+ getLabelProp : getTreeNodeTitle ,
222
228
simpleMode : treeDataSimpleMode ,
223
229
} ) ;
224
230
225
- const flattedOptions = React . useMemo ( ( ) => flattenOptions ( mergedTreeData ) , [ mergedTreeData ] ) ;
231
+ const flattedOptions = useMemo ( ( ) => flattenOptions ( mergedTreeData ) , [ mergedTreeData ] ) ;
226
232
const [ cacheKeyMap , cacheValueMap ] = useKeyValueMap ( flattedOptions ) ;
227
233
const [ getEntityByKey , getEntityByValue ] = useKeyValueMapping ( cacheKeyMap , cacheValueMap ) ;
228
234
229
235
// Only generate keyEntities for check conduction when is `treeCheckable`
230
- const { keyEntities : conductKeyEntities } = React . useMemo ( ( ) => {
236
+ const { keyEntities : conductKeyEntities } = useMemo ( ( ) => {
231
237
if ( treeConduction ) {
232
238
return convertDataToEntities ( mergedTreeData as any ) ;
233
239
}
234
240
return { keyEntities : null } ;
235
241
} , [ mergedTreeData , treeCheckable , treeCheckStrictly ] ) ;
236
242
237
243
// ========================= Value =========================
238
- const [ value , setValue ] = React . useState < DefaultValueType > ( props . defaultValue ) ;
239
- const mergedValue = 'value' in props ? props . value : value ;
244
+ const [ value , setValue ] = useMergedState < DefaultValueType > ( props . defaultValue , {
245
+ value : props . value ,
246
+ } ) ;
240
247
241
248
/** Get `missingRawValues` which not exist in the tree yet */
242
249
const splitRawValues = ( newRawValues : RawValueType [ ] ) => {
@@ -255,11 +262,11 @@ const RefTreeSelect = React.forwardRef<RefSelectProps, TreeSelectProps>((props,
255
262
return { missingRawValues, existRawValues } ;
256
263
} ;
257
264
258
- const [ rawValues , rawHalfCheckedKeys ] : [ RawValueType [ ] , RawValueType [ ] ] = React . useMemo ( ( ) => {
265
+ const [ rawValues , rawHalfCheckedKeys ] : [ RawValueType [ ] , RawValueType [ ] ] = useMemo ( ( ) => {
259
266
const valueHalfCheckedKeys : RawValueType [ ] = [ ] ;
260
267
const newRawValues : RawValueType [ ] = [ ] ;
261
268
262
- toArray ( mergedValue ) . forEach ( item => {
269
+ toArray ( value ) . forEach ( item => {
263
270
if ( item && typeof item === 'object' && 'value' in item ) {
264
271
if ( item . halfChecked && treeCheckStrictly ) {
265
272
const entity = getEntityByValue ( item . value ) ;
@@ -284,10 +291,10 @@ const RefTreeSelect = React.forwardRef<RefSelectProps, TreeSelectProps>((props,
284
291
] ;
285
292
}
286
293
return [ newRawValues , valueHalfCheckedKeys ] ;
287
- } , [ mergedValue , mergedMultiple , mergedLabelInValue , treeCheckable , treeCheckStrictly ] ) ;
294
+ } , [ value , mergedMultiple , mergedLabelInValue , treeCheckable , treeCheckStrictly ] ) ;
288
295
const selectValues = useSelectValues ( rawValues , {
289
296
treeConduction,
290
- value : mergedValue ,
297
+ value,
291
298
showCheckedStrategy,
292
299
conductKeyEntities,
293
300
getEntityByValue,
@@ -326,7 +333,7 @@ const RefTreeSelect = React.forwardRef<RefSelectProps, TreeSelectProps>((props,
326
333
} ;
327
334
328
335
let returnValues = mergedLabelInValue
329
- ? getRawValueLabeled ( eventValues , mergedValue , getEntityByValue , getTreeNodeLabelProp )
336
+ ? getRawValueLabeled ( eventValues , value , getEntityByValue , getTreeNodeLabelProp )
330
337
: eventValues ;
331
338
332
339
// We need fill half check back
@@ -340,7 +347,7 @@ const RefTreeSelect = React.forwardRef<RefSelectProps, TreeSelectProps>((props,
340
347
341
348
returnValues = [
342
349
...( returnValues as LabelValueType [ ] ) ,
343
- ...getRawValueLabeled ( halfValues , mergedValue , getEntityByValue , getTreeNodeLabelProp ) ,
350
+ ...getRawValueLabeled ( halfValues , value , getEntityByValue , getTreeNodeLabelProp ) ,
344
351
] ;
345
352
}
346
353
0 commit comments