Skip to content

Commit baab8d5

Browse files
haseebdaoneoliviertassinari
authored andcommitted
[Autocomplete] Fix form submit with freeSolo and multiple (#19072)
1 parent 4b6cbf0 commit baab8d5

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

packages/material-ui-lab/src/Autocomplete/Autocomplete.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,7 @@ const Autocomplete = React.forwardRef(function Autocomplete(props, ref) {
356356
},
357357
className,
358358
)}
359-
{...getRootProps()}
360-
{...other}
359+
{...getRootProps(other)}
361360
>
362361
{renderInput({
363362
id,

packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,42 @@ describe('<Autocomplete />', () => {
9292
});
9393
});
9494

95+
it('should trigger a form expectedly', () => {
96+
const handleSubmit = spy();
97+
const { setProps } = render(
98+
<Autocomplete
99+
options={['one', 'two']}
100+
onKeyDown={event => {
101+
if (!event.defaultPrevented && event.key === 'Enter') {
102+
handleSubmit();
103+
}
104+
}}
105+
renderInput={props2 => <TextField {...props2} autoFocus />}
106+
/>,
107+
);
108+
fireEvent.keyDown(document.activeElement, { key: 'Enter' });
109+
expect(handleSubmit.callCount).to.equal(1);
110+
111+
fireEvent.change(document.activeElement, { target: { value: 'o' } });
112+
fireEvent.keyDown(document.activeElement, { key: 'ArrowDown' });
113+
fireEvent.keyDown(document.activeElement, { key: 'Enter' });
114+
expect(handleSubmit.callCount).to.equal(1);
115+
fireEvent.keyDown(document.activeElement, { key: 'Enter' });
116+
expect(handleSubmit.callCount).to.equal(2);
117+
118+
setProps({ key: 'test-2', multiple: true, freeSolo: true });
119+
fireEvent.change(document.activeElement, { target: { value: 'o' } });
120+
fireEvent.keyDown(document.activeElement, { key: 'Enter' });
121+
expect(handleSubmit.callCount).to.equal(2);
122+
fireEvent.keyDown(document.activeElement, { key: 'Enter' });
123+
expect(handleSubmit.callCount).to.equal(3);
124+
125+
setProps({ key: 'test-3', freeSolo: true });
126+
fireEvent.change(document.activeElement, { target: { value: 'o' } });
127+
fireEvent.keyDown(document.activeElement, { key: 'Enter' });
128+
expect(handleSubmit.callCount).to.equal(4);
129+
});
130+
95131
describe('WAI-ARIA conforming markup', () => {
96132
specify('when closed', () => {
97133
const { getAllByRole, getByRole, queryByRole } = render(

packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ export default function useAutocomplete(props) {
547547
handleValue(event, multiple ? [] : null);
548548
};
549549

550-
const handleKeyDown = event => {
550+
const handleKeyDown = other => event => {
551551
if (focusedTag !== -1 && ['ArrowLeft', 'ArrowRight'].indexOf(event.key) === -1) {
552552
setFocusedTag(-1);
553553
focusTag(-1);
@@ -612,6 +612,10 @@ export default function useAutocomplete(props) {
612612
);
613613
}
614614
} else if (freeSolo && inputValue !== '' && inputValueIsSelectedValue === false) {
615+
if (multiple) {
616+
// Allow people to add new values before they submit the form.
617+
event.preventDefault();
618+
}
615619
selectNewValue(event, inputValue);
616620
}
617621
break;
@@ -640,6 +644,10 @@ export default function useAutocomplete(props) {
640644
break;
641645
default:
642646
}
647+
648+
if (other.onKeyDown) {
649+
other.onKeyDown(event);
650+
}
643651
};
644652

645653
const handleFocus = event => {
@@ -792,11 +800,12 @@ export default function useAutocomplete(props) {
792800
}
793801

794802
return {
795-
getRootProps: () => ({
803+
getRootProps: (other = {}) => ({
796804
'aria-owns': popupOpen ? `${id}-popup` : null,
797805
role: 'combobox',
798806
'aria-expanded': popupOpen,
799-
onKeyDown: handleKeyDown,
807+
...other,
808+
onKeyDown: handleKeyDown(other),
800809
onMouseDown: handleMouseDown,
801810
onClick: handleClick,
802811
}),

0 commit comments

Comments
 (0)