1- import * as React from 'react' ;
2- import type { RefOptionListProps } from 'rc-select/lib/OptionList' ;
31import { useBaseProps } from 'rc-select' ;
2+ import type { RefOptionListProps } from 'rc-select/lib/OptionList' ;
43import KeyCode from 'rc-util/lib/KeyCode' ;
4+ import * as React from 'react' ;
55import type { DefaultOptionType , InternalFieldNames , SingleValueType } from '../Cascader' ;
66import { SEARCH_MARK } from '../hooks/useSearchOptions' ;
7+ import { getFullPathKeys , toPathKey } from '../utils/commonUtil' ;
78
89export default (
910 ref : React . Ref < RefOptionListProps > ,
@@ -16,41 +17,46 @@ export default (
1617 const { direction, searchValue, toggleOpen, open } = useBaseProps ( ) ;
1718 const rtl = direction === 'rtl' ;
1819
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 ;
2224
23- const mergedActiveIndexes : number [ ] = [ ] ;
24- const mergedActiveValueCells : React . Key [ ] = [ ] ;
25+ const mergedActiveIndexes : number [ ] = [ ] ;
26+ const mergedActiveValueCells : React . Key [ ] = [ ] ;
2527
26- const len = activeValueCells . length ;
28+ const len = activeValueCells . length ;
2729
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 ) ;
3431
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+ ) ;
3840
39- activeIndex = nextActiveIndex ;
40- mergedActiveIndexes . push ( activeIndex ) ;
41- mergedActiveValueCells . push ( activeValueCells [ i ] ) ;
41+ if ( nextActiveIndex === - 1 ) {
42+ break ;
43+ }
4244
43- currentOptions = currentOptions [ activeIndex ] [ fieldNames . children ] ;
44- }
45+ activeIndex = nextActiveIndex ;
46+ mergedActiveIndexes . push ( activeIndex ) ;
47+ mergedActiveValueCells . push ( activeValueCells [ i ] ) ;
4548
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+ }
5151
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 ] ) ;
5460
5561 // Update active value cells and scroll to target element
5662 const internalSetActiveValueCells = ( next : React . Key [ ] ) => {
@@ -69,10 +75,14 @@ export default (
6975 for ( let i = 0 ; i < len ; i += 1 ) {
7076 currentIndex = ( currentIndex + offset + len ) % len ;
7177 const option = lastActiveOptions [ currentIndex ] ;
72-
7378 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+ ) ;
7686 internalSetActiveValueCells ( nextActiveCells ) ;
7787 return ;
7888 }
0 commit comments