Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: fix scroll params #728

Merged
merged 2 commits into from Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/BaseSelect.tsx
Expand Up @@ -3,7 +3,7 @@ import classNames from 'classnames';
import KeyCode from 'rc-util/lib/KeyCode';
import isMobile from 'rc-util/lib/isMobile';
import { useComposeRef } from 'rc-util/lib/ref';
import type { ScrollTo } from 'rc-virtual-list/lib/List';
import type { ScrollTo, ScrollConfig } from 'rc-virtual-list/lib/List';
import useMergedState from 'rc-util/lib/hooks/useMergedState';
import useLayoutEffect from 'rc-util/lib/hooks/useLayoutEffect';
import { getSeparatedContent } from './utils/valueUtil';
Expand Down Expand Up @@ -45,7 +45,7 @@ export type RawValueType = string | number;
export interface RefOptionListProps {
onKeyDown: React.KeyboardEventHandler;
onKeyUp: React.KeyboardEventHandler;
scrollTo?: (index: number) => void;
scrollTo?: (args: number | ScrollConfig) => void;
}

export type CustomTagProps = {
Expand Down Expand Up @@ -315,7 +315,7 @@ const BaseSelect = React.forwardRef((props: BaseSelectProps, ref: React.Ref<Base
React.useImperativeHandle(ref, () => ({
focus: selectorRef.current?.focus,
blur: selectorRef.current?.blur,
scrollTo: (arg) => listRef.current?.scrollTo(arg as any),
scrollTo: (arg) => listRef.current?.scrollTo(arg),
}));

// ========================== Search Value ==========================
Expand Down
7 changes: 4 additions & 3 deletions src/OptionList.tsx
@@ -1,5 +1,6 @@
import * as React from 'react';
import { useEffect } from 'react';
import type { ScrollConfig } from 'rc-virtual-list/lib/List';
import KeyCode from 'rc-util/lib/KeyCode';
import omit from 'rc-util/lib/omit';
import pickAttrs from 'rc-util/lib/pickAttrs';
Expand All @@ -20,7 +21,7 @@ export type OptionListProps = Record<string, never>;
export interface RefOptionListProps {
onKeyDown: React.KeyboardEventHandler;
onKeyUp: React.KeyboardEventHandler;
scrollTo?: (index: number) => void;
scrollTo?: (args: number | ScrollConfig) => void;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the virtual list hereused (arg: number | ScrollConfig) as its params type, so I think maybe we should allow here to accept type ScrollConfig

}

function isTitleType(content: any) {
Expand Down Expand Up @@ -65,9 +66,9 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, OptionListP
event.preventDefault();
};

const scrollIntoView = (index: number) => {
const scrollIntoView = (args: number | ScrollConfig) => {
if (listRef.current) {
listRef.current.scrollTo({ index });
listRef.current.scrollTo(args);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
listRef.current.scrollTo(args);
listRef.current.scrollTo(typeof args === 'number' ? { index: args } : args);

}
};

Expand Down
22 changes: 20 additions & 2 deletions tests/Select.test.tsx
Expand Up @@ -3,6 +3,7 @@ import KeyCode from 'rc-util/lib/KeyCode';
import React from 'react';
import { act } from 'react-dom/test-utils';
import { resetWarned } from 'rc-util/lib/warning';
import type { ScrollConfig } from 'rc-virtual-list/lib/List';
import { spyElementPrototype } from 'rc-util/lib/test/domHook';
import VirtualList from 'rc-virtual-list';
import type { SelectProps } from '../src';
Expand Down Expand Up @@ -1776,13 +1777,12 @@ describe('Select.Basic', () => {
});
});

it('scrollTo should work', () => {
it('scrollTo should work with number', () => {
const ref = React.createRef<BaseSelectRef>();
const wrapper = mount(<Select ref={ref} />);

// Not crash
ref.current.scrollTo(100);

// Open to call again
wrapper.setProps({
open: true,
Expand All @@ -1791,6 +1791,24 @@ describe('Select.Basic', () => {
ref.current.scrollTo(100);
});

it('scrollTo should work with scrollConfig object', () => {
const ref = React.createRef<BaseSelectRef>();
const wrapper = mount(<Select ref={ref} />);
const scrollParams: ScrollConfig = {
index: 30,
align: 'top',
};

// Not crash
ref.current.scrollTo(scrollParams);
// Open to call again
wrapper.setProps({
open: true,
});
wrapper.update();
ref.current.scrollTo(scrollParams);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a new test case for this.

});

it('pass props', () => {
// `count` is not a valid dom prop. Just compatible with origin logic.
const wrapper = mount(
Expand Down