Skip to content

Commit

Permalink
[Autocomplete] Skip filtering when list of options is loading (mui#33278
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ndebeiss authored and Daniel Rabe committed Nov 29, 2022
1 parent 5f81df5 commit e7e1cd6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
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

0 comments on commit e7e1cd6

Please sign in to comment.