Skip to content

Commit 60cdccd

Browse files
authored
Fix: fix scroll params (#728)
* Fix: fix scroll params [issue-link](ant-design/ant-design#34468) * Chore: add unit test for scroll config type params
1 parent 4b2f72c commit 60cdccd

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

src/BaseSelect.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { AlignType } from 'rc-trigger/lib/interface';
44
import KeyCode from 'rc-util/lib/KeyCode';
55
import isMobile from 'rc-util/lib/isMobile';
66
import { useComposeRef } from 'rc-util/lib/ref';
7-
import type { ScrollTo } from 'rc-virtual-list/lib/List';
7+
import type { ScrollTo, ScrollConfig } from 'rc-virtual-list/lib/List';
88
import useMergedState from 'rc-util/lib/hooks/useMergedState';
99
import useLayoutEffect from 'rc-util/lib/hooks/useLayoutEffect';
1010
import { getSeparatedContent } from './utils/valueUtil';
@@ -46,7 +46,7 @@ export type RawValueType = string | number;
4646
export interface RefOptionListProps {
4747
onKeyDown: React.KeyboardEventHandler;
4848
onKeyUp: React.KeyboardEventHandler;
49-
scrollTo?: (index: number) => void;
49+
scrollTo?: (args: number | ScrollConfig) => void;
5050
}
5151

5252
export type CustomTagProps = {
@@ -316,7 +316,7 @@ const BaseSelect = React.forwardRef((props: BaseSelectProps, ref: React.Ref<Base
316316
React.useImperativeHandle(ref, () => ({
317317
focus: selectorRef.current?.focus,
318318
blur: selectorRef.current?.blur,
319-
scrollTo: (arg) => listRef.current?.scrollTo(arg as any),
319+
scrollTo: (arg) => listRef.current?.scrollTo(arg),
320320
}));
321321

322322
// ========================== Search Value ==========================

src/OptionList.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as React from 'react';
22
import { useEffect } from 'react';
3+
import type { ScrollConfig } from 'rc-virtual-list/lib/List';
34
import KeyCode from 'rc-util/lib/KeyCode';
45
import omit from 'rc-util/lib/omit';
56
import pickAttrs from 'rc-util/lib/pickAttrs';
@@ -20,7 +21,7 @@ export type OptionListProps = Record<string, never>;
2021
export interface RefOptionListProps {
2122
onKeyDown: React.KeyboardEventHandler;
2223
onKeyUp: React.KeyboardEventHandler;
23-
scrollTo?: (index: number) => void;
24+
scrollTo?: (args: number | ScrollConfig) => void;
2425
}
2526

2627
function isTitleType(content: any) {
@@ -65,9 +66,9 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, OptionListP
6566
event.preventDefault();
6667
};
6768

68-
const scrollIntoView = (index: number) => {
69+
const scrollIntoView = (args: number | ScrollConfig) => {
6970
if (listRef.current) {
70-
listRef.current.scrollTo({ index });
71+
listRef.current.scrollTo(args);
7172
}
7273
};
7374

tests/Select.test.tsx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import KeyCode from 'rc-util/lib/KeyCode';
33
import React from 'react';
44
import { act } from 'react-dom/test-utils';
55
import { resetWarned } from 'rc-util/lib/warning';
6+
import type { ScrollConfig } from 'rc-virtual-list/lib/List';
67
import { spyElementPrototype } from 'rc-util/lib/test/domHook';
78
import VirtualList from 'rc-virtual-list';
89
import type { SelectProps } from '../src';
@@ -1776,13 +1777,12 @@ describe('Select.Basic', () => {
17761777
});
17771778
});
17781779

1779-
it('scrollTo should work', () => {
1780+
it('scrollTo should work with number', () => {
17801781
const ref = React.createRef<BaseSelectRef>();
17811782
const wrapper = mount(<Select ref={ref} />);
17821783

17831784
// Not crash
17841785
ref.current.scrollTo(100);
1785-
17861786
// Open to call again
17871787
wrapper.setProps({
17881788
open: true,
@@ -1791,6 +1791,24 @@ describe('Select.Basic', () => {
17911791
ref.current.scrollTo(100);
17921792
});
17931793

1794+
it('scrollTo should work with scrollConfig object', () => {
1795+
const ref = React.createRef<BaseSelectRef>();
1796+
const wrapper = mount(<Select ref={ref} />);
1797+
const scrollParams: ScrollConfig = {
1798+
index: 30,
1799+
align: 'top',
1800+
};
1801+
1802+
// Not crash
1803+
ref.current.scrollTo(scrollParams);
1804+
// Open to call again
1805+
wrapper.setProps({
1806+
open: true,
1807+
});
1808+
wrapper.update();
1809+
ref.current.scrollTo(scrollParams);
1810+
});
1811+
17941812
it('pass props', () => {
17951813
// `count` is not a valid dom prop. Just compatible with origin logic.
17961814
const wrapper = mount(

0 commit comments

Comments
 (0)