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] Skip filtering when list of options is loading #33278

Merged
merged 3 commits into from Sep 30, 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
24 changes: 14 additions & 10 deletions packages/mui-base/src/AutocompleteUnstyled/useAutocomplete.js
Expand Up @@ -34,17 +34,21 @@ export function createFilterOptions(config = {}) {
input = stripDiacritics(input);
}

const filteredOptions = options.filter((option) => {
let candidate = (stringify || getOptionLabel)(option);
if (ignoreCase) {
candidate = candidate.toLowerCase();
}
if (ignoreAccents) {
candidate = stripDiacritics(candidate);
}
const filteredOptions = !input
? options
: options.filter((option) => {
let candidate = (stringify || getOptionLabel)(option);
if (ignoreCase) {
candidate = candidate.toLowerCase();
}
if (ignoreAccents) {
candidate = stripDiacritics(candidate);
}

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

return typeof limit === 'number' ? filteredOptions.slice(0, limit) : filteredOptions;
};
Expand Down
11 changes: 11 additions & 0 deletions packages/mui-base/src/AutocompleteUnstyled/useAutocomplete.test.js
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react';
import { expect } from 'chai';
import { createRenderer, screen, ErrorBoundary, act, fireEvent } from 'test/utils';
import { useAutocomplete, createFilterOptions } from '@mui/base/AutocompleteUnstyled';
import { spy } from 'sinon';

describe('useAutocomplete', () => {
const { render } = createRenderer();
Expand Down Expand Up @@ -144,6 +145,16 @@ describe('useAutocomplete', () => {
});
});

describe('empty', () => {
it('does not call getOptionLabel if filter is empty', () => {
const getOptionLabelSpy = spy(getOptionLabel);
expect(
filterOptions(options, { inputValue: '', getOptionLabel: getOptionLabelSpy }),
).to.deep.equal(options);
expect(getOptionLabelSpy.callCount).to.equal(0);
});
});

describe('start', () => {
it('show only results that start with search', () => {
expect(filterOptions(options, { inputValue: 'a', getOptionLabel })).to.deep.equal(
Expand Down