Skip to content

Commit aed1f38

Browse files
eggbreadoliviertassinari
authored andcommitted
[Autocomplete] Ignore object keys in default filter (#18480)
1 parent 320b6f9 commit aed1f38

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,4 +658,38 @@ describe('<Autocomplete />', () => {
658658
expect(textbox.value).to.equal('');
659659
});
660660
});
661+
662+
describe('prop: filterOptions', () => {
663+
it('should ignore object keys by default', () => {
664+
const { queryAllByRole, getByRole } = render(
665+
<Autocomplete
666+
options={[
667+
{
668+
value: 'one',
669+
label: 'One',
670+
},
671+
{
672+
value: 'two',
673+
label: 'Two',
674+
},
675+
]}
676+
getOptionLabel={option => option.name}
677+
renderInput={params => <TextField autoFocus {...params} />}
678+
/>,
679+
);
680+
let options;
681+
const textbox = getByRole('textbox');
682+
683+
options = queryAllByRole('option');
684+
expect(options.length).to.equal(2);
685+
686+
fireEvent.change(textbox, { target: { value: 'value' } });
687+
options = queryAllByRole('option');
688+
expect(options.length).to.equal(0);
689+
690+
fireEvent.change(textbox, { target: { value: 'one' } });
691+
options = queryAllByRole('option');
692+
expect(options.length).to.equal(1);
693+
});
694+
});
661695
});

packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,30 @@ function stripDiacritics(string) {
1111
: string;
1212
}
1313

14+
function defaultStringify(value) {
15+
if (value == null) {
16+
return '';
17+
}
18+
19+
if (typeof value === 'string') {
20+
return value;
21+
}
22+
23+
if (typeof value === 'object') {
24+
return Object.keys(value)
25+
.map(key => value[key])
26+
.join(' ');
27+
}
28+
29+
return JSON.stringify(value);
30+
}
31+
1432
export function createFilterOptions(config = {}) {
1533
const {
1634
ignoreAccents = true,
1735
ignoreCase = true,
1836
matchFrom = 'any',
19-
stringify = JSON.stringify,
37+
stringify = defaultStringify,
2038
trim = false,
2139
} = config;
2240

0 commit comments

Comments
 (0)