Skip to content

Commit

Permalink
[Autocomplete] Fix double change event issue (#18786)
Browse files Browse the repository at this point in the history
  • Loading branch information
tplai authored and oliviertassinari committed Dec 11, 2019
1 parent 333749d commit f196f94
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
22 changes: 22 additions & 0 deletions packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -720,4 +720,26 @@ describe('<Autocomplete />', () => {
expect(options.length).to.equal(1);
});
});

describe('prop: freeSolo', () => {
it('pressing twice enter should not call onChange listener twice', () => {
const handleChange = spy();
const options = [{ name: 'foo' }];
render(
<Autocomplete
freeSolo
onChange={handleChange}
options={options}
getOptionLabel={option => option.name}
renderInput={params => <TextField {...params} autoFocus />}
/>,
);
fireEvent.keyDown(document.activeElement, { key: 'ArrowDown' });
fireEvent.keyDown(document.activeElement, { key: 'Enter' });
expect(handleChange.callCount).to.equal(1);
expect(handleChange.args[0][1]).to.deep.equal(options[0]);
fireEvent.keyDown(document.activeElement, { key: 'Enter' });
expect(handleChange.callCount).to.equal(1);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ export default function useAutocomplete(props) {
inputRef.current.value.length,
);
}
} else if (freeSolo && inputValue !== '') {
} else if (freeSolo && inputValueFilter !== '') {
selectNewValue(event, inputValue);
}
break;
Expand Down

0 comments on commit f196f94

Please sign in to comment.