Skip to content

Commit

Permalink
[Autocomplete] Fix form submit with freeSolo and multiple (#19072)
Browse files Browse the repository at this point in the history
  • Loading branch information
haseebdaone authored and oliviertassinari committed Jan 4, 2020
1 parent 4b6cbf0 commit baab8d5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
3 changes: 1 addition & 2 deletions packages/material-ui-lab/src/Autocomplete/Autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,7 @@ const Autocomplete = React.forwardRef(function Autocomplete(props, ref) {
},
className,
)}
{...getRootProps()}
{...other}
{...getRootProps(other)}
>
{renderInput({
id,
Expand Down
36 changes: 36 additions & 0 deletions packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,42 @@ describe('<Autocomplete />', () => {
});
});

it('should trigger a form expectedly', () => {
const handleSubmit = spy();
const { setProps } = render(
<Autocomplete
options={['one', 'two']}
onKeyDown={event => {
if (!event.defaultPrevented && event.key === 'Enter') {
handleSubmit();
}
}}
renderInput={props2 => <TextField {...props2} autoFocus />}
/>,
);
fireEvent.keyDown(document.activeElement, { key: 'Enter' });
expect(handleSubmit.callCount).to.equal(1);

fireEvent.change(document.activeElement, { target: { value: 'o' } });
fireEvent.keyDown(document.activeElement, { key: 'ArrowDown' });
fireEvent.keyDown(document.activeElement, { key: 'Enter' });
expect(handleSubmit.callCount).to.equal(1);
fireEvent.keyDown(document.activeElement, { key: 'Enter' });
expect(handleSubmit.callCount).to.equal(2);

setProps({ key: 'test-2', multiple: true, freeSolo: true });
fireEvent.change(document.activeElement, { target: { value: 'o' } });
fireEvent.keyDown(document.activeElement, { key: 'Enter' });
expect(handleSubmit.callCount).to.equal(2);
fireEvent.keyDown(document.activeElement, { key: 'Enter' });
expect(handleSubmit.callCount).to.equal(3);

setProps({ key: 'test-3', freeSolo: true });
fireEvent.change(document.activeElement, { target: { value: 'o' } });
fireEvent.keyDown(document.activeElement, { key: 'Enter' });
expect(handleSubmit.callCount).to.equal(4);
});

describe('WAI-ARIA conforming markup', () => {
specify('when closed', () => {
const { getAllByRole, getByRole, queryByRole } = render(
Expand Down
15 changes: 12 additions & 3 deletions packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ export default function useAutocomplete(props) {
handleValue(event, multiple ? [] : null);
};

const handleKeyDown = event => {
const handleKeyDown = other => event => {
if (focusedTag !== -1 && ['ArrowLeft', 'ArrowRight'].indexOf(event.key) === -1) {
setFocusedTag(-1);
focusTag(-1);
Expand Down Expand Up @@ -612,6 +612,10 @@ export default function useAutocomplete(props) {
);
}
} else if (freeSolo && inputValue !== '' && inputValueIsSelectedValue === false) {
if (multiple) {
// Allow people to add new values before they submit the form.
event.preventDefault();
}
selectNewValue(event, inputValue);
}
break;
Expand Down Expand Up @@ -640,6 +644,10 @@ export default function useAutocomplete(props) {
break;
default:
}

if (other.onKeyDown) {
other.onKeyDown(event);
}
};

const handleFocus = event => {
Expand Down Expand Up @@ -792,11 +800,12 @@ export default function useAutocomplete(props) {
}

return {
getRootProps: () => ({
getRootProps: (other = {}) => ({
'aria-owns': popupOpen ? `${id}-popup` : null,
role: 'combobox',
'aria-expanded': popupOpen,
onKeyDown: handleKeyDown,
...other,
onKeyDown: handleKeyDown(other),
onMouseDown: handleMouseDown,
onClick: handleClick,
}),
Expand Down

0 comments on commit baab8d5

Please sign in to comment.