Skip to content

Commit

Permalink
[Autocomplete] Ignore object keys in default filter (#18480)
Browse files Browse the repository at this point in the history
  • Loading branch information
eggbread authored and oliviertassinari committed Nov 21, 2019
1 parent 320b6f9 commit aed1f38
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
34 changes: 34 additions & 0 deletions packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -658,4 +658,38 @@ describe('<Autocomplete />', () => {
expect(textbox.value).to.equal('');
});
});

describe('prop: filterOptions', () => {
it('should ignore object keys by default', () => {
const { queryAllByRole, getByRole } = render(
<Autocomplete
options={[
{
value: 'one',
label: 'One',
},
{
value: 'two',
label: 'Two',
},
]}
getOptionLabel={option => option.name}

This comment has been minimized.

Copy link
@TrejGun

TrejGun Nov 22, 2019

Contributor

label

This comment has been minimized.

Copy link
@oliviertassinari

oliviertassinari Nov 22, 2019

Member

👍 thanks

renderInput={params => <TextField autoFocus {...params} />}
/>,
);
let options;
const textbox = getByRole('textbox');

options = queryAllByRole('option');
expect(options.length).to.equal(2);

fireEvent.change(textbox, { target: { value: 'value' } });
options = queryAllByRole('option');
expect(options.length).to.equal(0);

fireEvent.change(textbox, { target: { value: 'one' } });
options = queryAllByRole('option');
expect(options.length).to.equal(1);
});
});
});
20 changes: 19 additions & 1 deletion packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,30 @@ function stripDiacritics(string) {
: string;
}

function defaultStringify(value) {
if (value == null) {
return '';
}

if (typeof value === 'string') {
return value;
}

if (typeof value === 'object') {
return Object.keys(value)
.map(key => value[key])
.join(' ');
}

return JSON.stringify(value);
}

export function createFilterOptions(config = {}) {
const {
ignoreAccents = true,
ignoreCase = true,
matchFrom = 'any',
stringify = JSON.stringify,
stringify = defaultStringify,
trim = false,
} = config;

Expand Down

0 comments on commit aed1f38

Please sign in to comment.