1
- import * as React from 'react' ;
2
- import type { RefOptionListProps } from 'rc-select/lib/OptionList' ;
3
1
import { useBaseProps } from 'rc-select' ;
2
+ import type { RefOptionListProps } from 'rc-select/lib/OptionList' ;
4
3
import KeyCode from 'rc-util/lib/KeyCode' ;
4
+ import * as React from 'react' ;
5
5
import type { DefaultOptionType , InternalFieldNames , SingleValueType } from '../Cascader' ;
6
6
import { SEARCH_MARK } from '../hooks/useSearchOptions' ;
7
+ import { getFullPathKeys , toPathKey } from '../utils/commonUtil' ;
7
8
8
9
export default (
9
10
ref : React . Ref < RefOptionListProps > ,
@@ -16,41 +17,46 @@ export default (
16
17
const { direction, searchValue, toggleOpen, open } = useBaseProps ( ) ;
17
18
const rtl = direction === 'rtl' ;
18
19
19
- const [ validActiveValueCells , lastActiveIndex , lastActiveOptions ] = React . useMemo ( ( ) => {
20
- let activeIndex = - 1 ;
21
- let currentOptions = options ;
20
+ const [ validActiveValueCells , lastActiveIndex , lastActiveOptions , fullPathKeys ] =
21
+ React . useMemo ( ( ) => {
22
+ let activeIndex = - 1 ;
23
+ let currentOptions = options ;
22
24
23
- const mergedActiveIndexes : number [ ] = [ ] ;
24
- const mergedActiveValueCells : React . Key [ ] = [ ] ;
25
+ const mergedActiveIndexes : number [ ] = [ ] ;
26
+ const mergedActiveValueCells : React . Key [ ] = [ ] ;
25
27
26
- const len = activeValueCells . length ;
28
+ const len = activeValueCells . length ;
27
29
28
- // Fill validate active value cells and index
29
- for ( let i = 0 ; i < len && currentOptions ; i += 1 ) {
30
- // Mark the active index for current options
31
- const nextActiveIndex = currentOptions . findIndex (
32
- option => option [ fieldNames . value ] === activeValueCells [ i ] ,
33
- ) ;
30
+ const pathKeys = getFullPathKeys ( options , fieldNames ) ;
34
31
35
- if ( nextActiveIndex === - 1 ) {
36
- break ;
37
- }
32
+ // Fill validate active value cells and index
33
+ for ( let i = 0 ; i < len && currentOptions ; i += 1 ) {
34
+ // Mark the active index for current options
35
+ const nextActiveIndex = currentOptions . findIndex (
36
+ ( option , index ) =>
37
+ ( pathKeys [ index ] ? toPathKey ( pathKeys [ index ] ) : option [ fieldNames . value ] ) ===
38
+ activeValueCells [ i ] ,
39
+ ) ;
38
40
39
- activeIndex = nextActiveIndex ;
40
- mergedActiveIndexes . push ( activeIndex ) ;
41
- mergedActiveValueCells . push ( activeValueCells [ i ] ) ;
41
+ if ( nextActiveIndex === - 1 ) {
42
+ break ;
43
+ }
42
44
43
- currentOptions = currentOptions [ activeIndex ] [ fieldNames . children ] ;
44
- }
45
+ activeIndex = nextActiveIndex ;
46
+ mergedActiveIndexes . push ( activeIndex ) ;
47
+ mergedActiveValueCells . push ( activeValueCells [ i ] ) ;
45
48
46
- // Fill last active options
47
- let activeOptions = options ;
48
- for ( let i = 0 ; i < mergedActiveIndexes . length - 1 ; i += 1 ) {
49
- activeOptions = activeOptions [ mergedActiveIndexes [ i ] ] [ fieldNames . children ] ;
50
- }
49
+ currentOptions = currentOptions [ activeIndex ] [ fieldNames . children ] ;
50
+ }
51
51
52
- return [ mergedActiveValueCells , activeIndex , activeOptions ] ;
53
- } , [ activeValueCells , fieldNames , options ] ) ;
52
+ // Fill last active options
53
+ let activeOptions = options ;
54
+ for ( let i = 0 ; i < mergedActiveIndexes . length - 1 ; i += 1 ) {
55
+ activeOptions = activeOptions [ mergedActiveIndexes [ i ] ] [ fieldNames . children ] ;
56
+ }
57
+
58
+ return [ mergedActiveValueCells , activeIndex , activeOptions , pathKeys ] ;
59
+ } , [ activeValueCells , fieldNames , options ] ) ;
54
60
55
61
// Update active value cells and scroll to target element
56
62
const internalSetActiveValueCells = ( next : React . Key [ ] ) => {
@@ -69,10 +75,14 @@ export default (
69
75
for ( let i = 0 ; i < len ; i += 1 ) {
70
76
currentIndex = ( currentIndex + offset + len ) % len ;
71
77
const option = lastActiveOptions [ currentIndex ] ;
72
-
73
78
if ( option && ! option . disabled ) {
74
- const value = option [ fieldNames . value ] ;
75
- const nextActiveCells = validActiveValueCells . slice ( 0 , - 1 ) . concat ( value ) ;
79
+ const nextActiveCells = validActiveValueCells
80
+ . slice ( 0 , - 1 )
81
+ . concat (
82
+ fullPathKeys [ currentIndex ]
83
+ ? toPathKey ( fullPathKeys [ currentIndex ] )
84
+ : option [ fieldNames . value ] ,
85
+ ) ;
76
86
internalSetActiveValueCells ( nextActiveCells ) ;
77
87
return ;
78
88
}
0 commit comments