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 form submit with freeSolo and multiple #19072

Merged
merged 2 commits into from
Jan 4, 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
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