Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Autocomplete] Fix autoSelect logic #19384

Merged
merged 4 commits into from
Jan 25, 2020
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
58 changes: 49 additions & 9 deletions packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ describe('<Autocomplete />', () => {

describe('combobox', () => {
it('should clear the input when blur', () => {
const { container } = render(
const { getByRole } = render(
<Autocomplete renderInput={params => <TextField {...params} />} />,
);
const input = container.querySelector('input');
const input = getByRole('textbox');
input.focus();
fireEvent.change(document.activeElement, { target: { value: 'a' } });
expect(input.value).to.equal('a');
Expand All @@ -49,12 +49,51 @@ describe('<Autocomplete />', () => {
});
});

describe('multiple', () => {
describe('prop: autoSelect', () => {
it('should add new value when autoSelect & multiple on blur', () => {
const handleChange = spy();
const options = ['one', 'two'];
render(
<Autocomplete
autoSelect
multiple
value={[options[0]]}
options={options}
onChange={handleChange}
renderInput={params => <TextField autoFocus {...params} />}
/>,
);
fireEvent.change(document.activeElement, { target: { value: 't' } });
fireEvent.keyDown(document.activeElement, { key: 'ArrowDown' });
document.activeElement.blur();
expect(handleChange.callCount).to.equal(1);
expect(handleChange.args[0][1]).to.deep.equal(options);
});

it('should add new value when autoSelect & multiple & freeSolo on blur', () => {
const handleChange = spy();
render(
<Autocomplete
autoSelect
freeSolo
multiple
onChange={handleChange}
renderInput={params => <TextField autoFocus {...params} />}
/>,
);
fireEvent.change(document.activeElement, { target: { value: 'a' } });
document.activeElement.blur();
expect(handleChange.callCount).to.equal(1);
expect(handleChange.args[0][1]).to.deep.equal(['a']);
});
});

describe('prop: multiple', () => {
it('should not crash', () => {
const { container } = render(
const { getByRole } = render(
<Autocomplete renderInput={params => <TextField {...params} />} multiple />,
);
const input = container.querySelector('input');
const input = getByRole('textbox');
input.focus();
document.activeElement.blur();
input.focus();
Expand Down Expand Up @@ -524,14 +563,15 @@ describe('<Autocomplete />', () => {

describe('prop: disabled', () => {
it('should disable the input', () => {
const { container } = render(
const { getByRole } = render(
<Autocomplete
disabled
options={['one', 'two', 'three']}
renderInput={params => <TextField {...params} />}
/>,
);
expect(container.querySelector('input').disabled).to.be.true;
const input = getByRole('textbox');
expect(input.disabled).to.be.true;
});

it('should disable the popup button', () => {
Expand Down Expand Up @@ -645,14 +685,14 @@ describe('<Autocomplete />', () => {

it('should not select undefined ', () => {
const handleChange = spy();
const { container, getByRole } = render(
const { getByRole } = render(
<Autocomplete
onChange={handleChange}
options={['one', 'two']}
renderInput={params => <TextField {...params} />}
/>,
);
const input = container.querySelector('input');
const input = getByRole('textbox');
fireEvent.click(input);

const listbox = getByRole('listbox');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,9 @@ export default function useAutocomplete(props) {
}

if (autoSelect && selectedIndexRef.current !== -1) {
handleValue(event, filteredOptions[selectedIndexRef.current]);
selectNewValue(event, filteredOptions[selectedIndexRef.current]);
} else if (autoSelect && freeSolo && inputValue !== '') {
selectNewValue(event, inputValue, 'freeSolo');
} else if (!freeSolo) {
resetInputValue(event, value);
}
Expand Down