Skip to content
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
30 changes: 30 additions & 0 deletions packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,36 @@ describe('<Autocomplete />', () => {
'For the input option: "a", `getOptionLabel` returns: undefined',
);
});

it('warn if getOptionSelected match multiple values for a given option', () => {
const value = [{ id: '10', text: 'One' }, { id: '20', text: 'Two' }];
const options = [
{ id: '10', text: 'One' },
{ id: '20', text: 'Two' },
{ id: '30', text: 'Three' },
];

render(
<Autocomplete
{...defaultProps}
multiple
options={options}
value={value}
getOptionLabel={option => option.text}
getOptionSelected={option => value.find(v => v.id === option.id)}
renderInput={params => <TextField {...params} autoFocus />}
/>,
);

fireEvent.keyDown(document.activeElement, { key: 'ArrowDown' });
fireEvent.keyDown(document.activeElement, { key: 'ArrowDown' });
fireEvent.keyDown(document.activeElement, { key: 'Enter' });

expect(consoleErrorMock.callCount()).to.equal(1); // strict mode renders twice
expect(consoleErrorMock.args()[0][0]).to.include(
'The component expects a single value to match a given option but found 2 matches.',
);
});
});

describe('prop: options', () => {
Expand Down
15 changes: 15 additions & 0 deletions packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,21 @@ export default function useAutocomplete(props) {
const item = newValue;
newValue = Array.isArray(value) ? [...value] : [];

if (process.env.NODE_ENV !== 'production') {
const matches = newValue.filter(val => getOptionSelected(item, val));

if (matches.length > 1) {
console.error(
[
'Material-UI: the `getOptionSelected` method of useAutocomplete do not handle the arguments correctly.',
`The component expects a single value to match a given option but found ${
matches.length
} matches.`,
].join('\n'),
);
}
}

const itemIndex = findIndex(newValue, valueItem => getOptionSelected(item, valueItem));

if (itemIndex === -1) {
Expand Down