Skip to content

Commit

Permalink
[Autocomplete] Fix calling onChange for duplicate values (#30374)
Browse files Browse the repository at this point in the history
  • Loading branch information
alisasanib committed Dec 30, 2021
1 parent 2663458 commit dc3b696
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,11 @@ export default function useAutocomplete(props) {
};

const handleValue = (event, newValue, reason, details) => {
if (value === newValue) {
if (Array.isArray(value)) {
if (value.length === newValue.length && value.every((val, i) => val === newValue[i])) {
return;
}
} else if (value === newValue) {
return;
}

Expand Down
24 changes: 24 additions & 0 deletions packages/mui-material/src/Autocomplete/Autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,30 @@ describe('<Autocomplete />', () => {
expect(textbox).toHaveFocus();
});

it('should not call onChange function for duplicate values', () => {
const handleChange = spy();
const options = ['one', 'two'];
render(
<Autocomplete
freeSolo
defaultValue={options}
options={options}
onChange={handleChange}
renderInput={(params) => <TextField {...params} autoFocus />}
multiple
/>,
);
const textbox = screen.getByRole('textbox');

fireEvent.change(textbox, { target: { value: 'two' } });
fireEvent.keyDown(textbox, { key: 'Enter' });
expect(handleChange.callCount).to.equal(0);

fireEvent.change(textbox, { target: { value: 'three' } });
fireEvent.keyDown(textbox, { key: 'Enter' });
expect(handleChange.callCount).to.equal(1);
});

it('has no textbox value', () => {
render(
<Autocomplete
Expand Down

0 comments on commit dc3b696

Please sign in to comment.