Skip to content

Commit

Permalink
[Autocomplete] Don't delete tag if exists (in freesolo mode) (#19215)
Browse files Browse the repository at this point in the history
  • Loading branch information
adi carmel authored and oliviertassinari committed Jan 14, 2020
1 parent 26d0a8f commit e4f33b7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
21 changes: 21 additions & 0 deletions packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,27 @@ describe('<Autocomplete />', () => {
fireEvent.keyDown(document.activeElement, { key: 'Enter' });
expect(handleChange.callCount).to.equal(1);
});

it('should not delete exiting tag when try to add it twice', () => {
const handleChange = spy();
const options = ['one', 'two'];
const { container } = render(
<Autocomplete
defaultValue={options}
options={options}
onChange={handleChange}
freeSolo
renderInput={params => <TextField {...params} autoFocus />}
multiple
/>,
);
fireEvent.change(document.activeElement, { target: { value: 'three' } });
fireEvent.keyDown(document.activeElement, { key: 'Enter' });
expect(container.querySelectorAll('[class*="MuiChip-root"]')).to.have.length(3);
fireEvent.change(document.activeElement, { target: { value: 'three' } });
fireEvent.keyDown(document.activeElement, { key: 'Enter' });
expect(container.querySelectorAll('[class*="MuiChip-root"]')).to.have.length(3);
});
});

describe('prop: onInputChange', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ export default function useAutocomplete(props) {
setValue(newValue);
};

const selectNewValue = (event, newValue) => {
const selectNewValue = (event, newValue, origin = 'option') => {
if (multiple) {
const item = newValue;
newValue = Array.isArray(value) ? [...value] : [];
Expand All @@ -439,7 +439,7 @@ export default function useAutocomplete(props) {

if (itemIndex === -1) {
newValue.push(item);
} else {
} else if (origin !== 'freeSolo') {
newValue.splice(itemIndex, 1);
}
}
Expand Down Expand Up @@ -597,7 +597,7 @@ export default function useAutocomplete(props) {
// Allow people to add new values before they submit the form.
event.preventDefault();
}
selectNewValue(event, inputValue);
selectNewValue(event, inputValue, 'freeSolo');
}
break;
case 'Escape':
Expand Down

0 comments on commit e4f33b7

Please sign in to comment.