@@ -21,6 +21,7 @@ export type useKeyboardControlType = {
2121 displayOptions : TdOptionProps [ ] ;
2222 onCheckAllChange : ( checkAll : boolean , e ?: React . KeyboardEvent < HTMLInputElement > ) => void ;
2323 selectInputRef : any ;
24+ toggleIsScrolling : ( isScrolling : boolean ) => void ;
2425} ;
2526
2627export default function useKeyboardControl ( {
@@ -33,6 +34,7 @@ export default function useKeyboardControl({
3334 displayOptions,
3435 onCheckAllChange,
3536 selectInputRef,
37+ toggleIsScrolling,
3638} : useKeyboardControlType ) {
3739 const [ hoverIndex , changeHoverIndex ] = useState ( - 1 ) ;
3840 const [ hoverOption , changeHoverOption ] = useState < TdOptionProps > ( undefined ) ;
@@ -53,6 +55,9 @@ export default function useKeyboardControl({
5355 const selector = `.${ classPrefix } -select-option__hover` ;
5456 const firstSelectedNode : HTMLDivElement = popupContent . querySelector ( selector ) ;
5557 if ( firstSelectedNode ) {
58+ // 避免与 updateScrollTop 冲突
59+ toggleIsScrolling ( true ) ;
60+
5661 // 小于0时不需要特殊处理,会被设为0
5762 const scrollHeight = popupContent . querySelector ( optionSelector ) . clientHeight * hoverIndex ;
5863
@@ -66,8 +71,12 @@ export default function useKeyboardControl({
6671 useEffect ( ( ) => {
6772 if ( ! innerPopupVisible ) {
6873 changeHoverIndex ( - 1 ) ;
74+ } else if ( ! multiple ) {
75+ // 单选时,hoverIndex 初始值为选中值的索引
76+ const index = displayOptions . findIndex ( ( option ) => option . value === value ) ;
77+ changeHoverIndex ( index >= 0 ? index : - 1 ) ;
6978 }
70- } , [ innerPopupVisible ] ) ;
79+ } , [ innerPopupVisible , multiple , value , displayOptions ] ) ;
7180
7281 useEffect ( ( ) => {
7382 changeHoverOption ( hoverIndex === - 1 ? undefined : displayOptions [ hoverIndex ] ) ;
0 commit comments