Skip to content

Commit 1ee1eb2

Browse files
authored
fix: wrappig index param (#739)
* fix: wrappig index param * test: add unit test * fix: check args passed to scrollIntoView
1 parent 1d77385 commit 1ee1eb2

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/OptionList.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, OptionListP
7777

7878
const scrollIntoView = (args: number | ScrollConfig) => {
7979
if (listRef.current) {
80-
listRef.current.scrollTo(args);
80+
listRef.current.scrollTo(typeof args === 'number' ? { index: args } : args);
8181
}
8282
};
8383

tests/OptionList.test.tsx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ import { mount } from 'enzyme';
22
import KeyCode from 'rc-util/lib/KeyCode';
33
import { act } from 'react-dom/test-utils';
44
import React from 'react';
5-
import type { OptionListProps, RefOptionListProps } from '../src/OptionList';
5+
import type { RefOptionListProps } from '../src/OptionList';
66
import OptionList from '../src/OptionList';
77
import { injectRunAllTimers } from './utils/common';
8-
import type { OptionsType } from '../src/interface';
98
import { fillFieldNames, flattenOptions } from '../src/utils/valueUtil';
109
import SelectContext from '../src/SelectContext';
1110
import { BaseSelectContext } from '../src/hooks/useBaseProps';
@@ -299,4 +298,23 @@ describe('OptionList', () => {
299298
);
300299
expect(wrapper.find('.rc-select-item-option').first().prop('title')).toBe(undefined);
301300
});
301+
302+
it('params to scrollIntoView should be object when key is pressed', () => {
303+
const listRef = React.createRef<RefOptionListProps>();
304+
const options = Array.from({ length: 20 }).map((v, i) => ({ value: i, label: i }));
305+
const mockScroll = jest.fn();
306+
const wrapper = mount(
307+
generateList({
308+
options,
309+
ref: listRef,
310+
}),
311+
);
312+
const vList = wrapper.find('List').getElement() as any;
313+
// override the ref to test the parameters passed to the scrollIntoView method
314+
vList.ref.current = { scrollTo: mockScroll };
315+
act(() => {
316+
listRef.current.onKeyDown({ which: KeyCode.DOWN } as any);
317+
});
318+
expect(mockScroll.mock.calls[0][0]).toEqual({ index: 1 });
319+
});
302320
});

0 commit comments

Comments
 (0)