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

[Autocomplete] Support limiting the amount of options #19539

Merged
merged 4 commits into from
Feb 4, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/src/pages/components/autocomplete/autocomplete.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ It supports the following options:
- `config.matchFrom` (*'any' | 'start'* [optional]): Defaults to `'any'`.
- `config.stringify` (*Func* [optional]): Defaults to `JSON.stringify`.
- `config.trim` (*Boolean* [optional]): Defaults to `false`. Remove trailing spaces.
- `config.limit` (*Number* [optional]): Default to null. Limit the number of suggested options to be shown. For example, if `config.limit` is `100`, only the first `100` matching options are shown. It can be useful if a lot of options match and virtualization wasn't set up.

In the following demo, the options need to start with the query prefix:

Expand Down
25 changes: 25 additions & 0 deletions packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import describeConformance from '@material-ui/core/test-utils/describeConformanc
import consoleErrorMock from 'test/utils/consoleErrorMock';
import { spy } from 'sinon';
import { createClientRender, fireEvent } from 'test/utils/createClientRender';
import { createFilterOptions } from '../useAutocomplete/useAutocomplete';
import Autocomplete from './Autocomplete';
import TextField from '@material-ui/core/TextField';

Expand Down Expand Up @@ -867,6 +868,30 @@ describe('<Autocomplete />', () => {
options = queryAllByRole('option');
expect(options.length).to.equal(1);
});

it('limits the amount of rendered options when `limit` is set in `createFilterOptions`', () => {
const filterOptions = createFilterOptions({ limit: 2 });
const { queryAllByRole } = render(
<Autocomplete
options={['one', 'two', 'three']}
renderInput={params => <TextField {...params} autoFocus />}
filterOptions={filterOptions}
/>,
);
expect(queryAllByRole('option').length).to.equal(2);
});

it('does not limit the amount of rendered options when `limit` is not set in `createFilterOptions`', () => {
const filterOptions = createFilterOptions({});
const { queryAllByRole } = render(
<Autocomplete
options={['one', 'two', 'three']}
renderInput={params => <TextField {...params} autoFocus />}
filterOptions={filterOptions}
/>,
);
expect(queryAllByRole('option').length).to.equal(3);
});
});

describe('prop: freeSolo', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface CreateFilterOptionsConfig<T> {
matchFrom?: 'any' | 'start';
stringify?: (option: T) => string;
trim?: boolean;
limit?: number;
}

export interface FilterOptionsState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export function createFilterOptions(config = {}) {
matchFrom = 'any',
stringify = defaultStringify,
trim = false,
limit,
} = config;

return (options, { inputValue }) => {
Expand All @@ -46,8 +47,7 @@ export function createFilterOptions(config = {}) {
if (ignoreAccents) {
input = stripDiacritics(input);
}

return options.filter(option => {
const filteredOptions = options.filter(option => {
let candidate = stringify(option);
if (ignoreCase) {
candidate = candidate.toLowerCase();
Expand All @@ -58,6 +58,8 @@ export function createFilterOptions(config = {}) {

return matchFrom === 'start' ? candidate.indexOf(input) === 0 : candidate.indexOf(input) > -1;
});

return typeof limit === 'number' ? filteredOptions.slice(0, limit) : filteredOptions;
};
}

Expand Down