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

Add resultLimit to improve performance of react-select for many options #5648

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
61 changes: 33 additions & 28 deletions packages/react-select/src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ export interface Props<
pageSize: number;
/** Placeholder for the select value */
placeholder: ReactNode;
/** Limits the number of rendered options in the dropdown */
resultLimit?: number;
/** Status to relay to screen readers */
screenReaderStatus: (obj: { count: number }) => string;
/**
Expand Down Expand Up @@ -1874,6 +1876,7 @@ export default class Select<
noOptionsMessage,
onMenuScrollToTop,
onMenuScrollToBottom,
resultLimit,
} = this.props;

if (!menuIsOpen) return null;
Expand Down Expand Up @@ -1915,34 +1918,36 @@ export default class Select<
let menuUI: ReactNode;

if (this.hasOptions()) {
menuUI = this.getCategorizedOptions().map((item) => {
if (item.type === 'group') {
const { data, options, index: groupIndex } = item;
const groupId = `${this.getElementId('group')}-${groupIndex}`;
const headingId = `${groupId}-heading`;

return (
<Group
{...commonProps}
key={groupId}
data={data}
options={options}
Heading={GroupHeading}
headingProps={{
id: headingId,
data: item.data,
}}
label={this.formatGroupLabel(item.data)}
>
{item.options.map((option) =>
render(option, `${groupIndex}-${option.index}`)
)}
</Group>
);
} else if (item.type === 'option') {
return render(item, `${item.index}`);
}
});
menuUI = this.getCategorizedOptions()
.slice(0, resultLimit && resultLimit > 0 ? resultLimit : undefined)
.map((item) => {
if (item.type === 'group') {
const { data, options, index: groupIndex } = item;
const groupId = `${this.getElementId('group')}-${groupIndex}`;
const headingId = `${groupId}-heading`;

return (
<Group
{...commonProps}
key={groupId}
data={data}
options={options}
Heading={GroupHeading}
headingProps={{
id: headingId,
data: item.data,
}}
label={this.formatGroupLabel(item.data)}
>
{item.options.map((option) =>
render(option, `${groupIndex}-${option.index}`)
)}
</Group>
);
} else if (item.type === 'option') {
return render(item, `${item.index}`);
}
});
} else if (isLoading) {
const message = loadingMessage({ inputValue });
if (message === null) return null;
Expand Down
22 changes: 22 additions & 0 deletions packages/react-select/src/__tests__/Select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3120,6 +3120,28 @@ test('renders with custom theme', () => {
).toEqual(primary);
});

test('resultLimit limits number of rendered props', () => {
const resultLimit = 5;
const { container } = render(
<Select {...BASIC_PROPS} resultLimit={resultLimit} menuIsOpen />
);
expect(container.querySelectorAll('.react-select__option').length).toBe(
resultLimit
);
});

test.each([undefined, 0, -10])(
'resultLimit negative, undefined or zero should render all options',
(resultLimit) => {
const { container } = render(
<Select {...BASIC_PROPS} resultLimit={resultLimit} menuIsOpen />
);
expect(container.querySelectorAll('.react-select__option').length).toBe(
OPTIONS.length
);
}
);

cases(
'`required` prop',
({ props = BASIC_PROPS }) => {
Expand Down