1- import { useEffect , useRef , useState } from 'react' ;
1+ import { useEffect , useMemo , useRef , useState } from 'react' ;
22
33import useConfig from '../../hooks/useConfig' ;
4- import { getSelectValueArr } from '../util/helper' ;
4+ import { getKeyMapping , getSelectValueArr } from '../util/helper' ;
55
6- import type { SelectOption , SelectValue , SelectValueChangeTrigger , TdOptionProps } from '../type' ;
6+ import type { SelectOption , SelectValue , SelectValueChangeTrigger , TdOptionProps , TdSelectProps } from '../type' ;
77
88export type useKeyboardControlType = {
9+ keys : TdSelectProps [ 'keys' ] ;
910 max : number ;
1011 multiple : boolean ;
1112 value : SelectValue < SelectOption > ;
@@ -25,6 +26,7 @@ export type useKeyboardControlType = {
2526} ;
2627
2728export default function useKeyboardControl ( {
29+ keys,
2830 max,
2931 multiple,
3032 value,
@@ -36,11 +38,26 @@ export default function useKeyboardControl({
3638 selectInputRef,
3739 toggleIsScrolling,
3840} : useKeyboardControlType ) {
39- const [ hoverIndex , changeHoverIndex ] = useState ( - 1 ) ;
40-
4141 const { classPrefix } = useConfig ( ) ;
42- // 全选判断
42+
4343 const isCheckAll = useRef ( false ) ;
44+ const [ hoverIndex , changeHoverIndex ] = useState ( - 1 ) ;
45+
46+ const { valueKey, disabledKey } = useMemo ( ( ) => getKeyMapping ( keys ) , [ keys ] ) ;
47+
48+ useEffect ( ( ) => {
49+ if ( ! innerPopupVisible ) {
50+ changeHoverIndex ( - 1 ) ;
51+ } else if ( ! multiple ) {
52+ // 单选时,hoverIndex 初始值为选中值的索引
53+ const index = displayOptions . findIndex ( ( option ) => option . value === value ) ;
54+ changeHoverIndex ( index >= 0 ? index : - 1 ) ;
55+ } else {
56+ changeHoverIndex ( - 1 ) ;
57+ }
58+ // eslint-disable-next-line react-hooks/exhaustive-deps
59+ } , [ innerPopupVisible ] ) ;
60+
4461 useEffect ( ( ) => {
4562 if ( ! Array . isArray ( value ) ) return ;
4663 isCheckAll . current =
@@ -67,19 +84,6 @@ export default function useKeyboardControl({
6784 }
6885 } ;
6986
70- useEffect ( ( ) => {
71- if ( ! innerPopupVisible ) {
72- changeHoverIndex ( - 1 ) ;
73- } else if ( ! multiple ) {
74- // 单选时,hoverIndex 初始值为选中值的索引
75- const index = displayOptions . findIndex ( ( option ) => option . value === value ) ;
76- changeHoverIndex ( index >= 0 ? index : - 1 ) ;
77- } else {
78- changeHoverIndex ( - 1 ) ;
79- }
80- // eslint-disable-next-line react-hooks/exhaustive-deps
81- } , [ innerPopupVisible ] ) ;
82-
8387 const handleKeyDown = ( _value : string , { e } : { e : React . KeyboardEvent < HTMLInputElement > } ) => {
8488 const optionsListLength = displayOptions . length ;
8589
@@ -92,7 +96,7 @@ export default function useKeyboardControl({
9296 else if ( hoverIndex === 0 || hoverIndex > optionsListLength - 1 ) newIndex = optionsListLength - 1 ;
9397 else newIndex -= 1 ;
9498
95- if ( displayOptions [ newIndex ] ?. disabled ) newIndex -= 1 ;
99+ if ( displayOptions [ newIndex ] ?. [ disabledKey ] ) newIndex -= 1 ;
96100
97101 changeHoverIndex ( newIndex ) ;
98102 handleKeyboardScroll ( newIndex ) ;
@@ -119,7 +123,7 @@ export default function useKeyboardControl({
119123 const selectedOptions = displayOptions [ hoverIndex ] ;
120124
121125 if ( selectedOptions )
122- handleChange ( selectedOptions . value , {
126+ handleChange ( selectedOptions [ valueKey ] , {
123127 trigger : 'check' ,
124128 e,
125129 } ) ;
@@ -128,17 +132,16 @@ export default function useKeyboardControl({
128132 handleKeyboardScroll ( 0 ) ;
129133 } else {
130134 if ( displayOptions [ hoverIndex ] . checkAll ) {
131- onCheckAllChange ( ! isCheckAll . current ) ;
135+ onCheckAllChange ( ! isCheckAll . current , e ) ;
132136 return ;
133137 }
134138
135- const optionValue = displayOptions [ hoverIndex ] ?. value ;
136-
139+ const optionValue = displayOptions [ hoverIndex ] ?. [ valueKey ] ;
137140 if ( ! optionValue ) return ;
141+
138142 const newValue = value as Array < SelectValue > ;
139143 const valueIndex = newValue . indexOf ( optionValue ) ;
140144 const isSelected = valueIndex > - 1 ;
141-
142145 const values = getSelectValueArr ( value , optionValue , isSelected ) ;
143146
144147 if ( max > 0 && values . length > max ) return ; // 如果已选达到最大值 则不处理
0 commit comments