Skip to content

Commit 0ce3627

Browse files
committed
fix: scroll failed
1 parent 674b154 commit 0ce3627

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

packages/components/select/base/Select.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import { useLocaleReceiver } from '../../locale/LocalReceiver';
2424
import SelectInput, { type SelectInputValue, type SelectInputValueChangeContext } from '../../select-input';
2525
import Tag from '../../tag';
2626
import { selectDefaultProps } from '../defaultProps';
27-
import useOptions, { isSelectOptionGroup } from '../hooks/useOptions';
2827
import useKeyboardControl from '../hooks/useKeyboardControl';
28+
import useOptions, { isSelectOptionGroup } from '../hooks/useOptions';
2929
import { getKeyMapping, getSelectValueArr, getSelectedOptions } from '../util/helper';
3030
import Option from './Option';
3131
import OptionGroup from './OptionGroup';
@@ -300,6 +300,7 @@ const Select = forwardRefWithStatics(
300300
value,
301301
onCheckAllChange,
302302
selectInputRef,
303+
toggleIsScrolling,
303304
});
304305

305306
// 处理filter逻辑

packages/components/select/hooks/useKeyboardControl.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

2627
export 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

Comments
 (0)