react-spectrum icon indicating copy to clipboard operation
react-spectrum copied to clipboard

Combobox Input requires hitting enter twice to submit a form

Open ashleyryan opened this issue 1 year ago โ€ข 4 comments

Provide a general summary of the issue here

Some of my consumers are trying to switch to our Combobox and are running into a few issues, one is #5234, so I'll respond in that issue for that

The other is that, when using "allowsCustomValue," it requires two enter presses to submit the html form when the popover is open - one to close the popover, one to submit the form. Generally, hitting enter inside of an input submits an HTML form, no JS required, but the combobox logic is stealing the keypress.

๐Ÿค” Expected Behavior?

When I hit enter inside of a combox input the html form submits

๐Ÿ˜ฏ Current Behavior

It requires two enters - one to close the popover, one to submit the form

๐Ÿ’ Possible Solution

No response

๐Ÿ”ฆ Context

No response

๐Ÿ–ฅ๏ธ Steps to Reproduce

https://codesandbox.io/p/sandbox/vigilant-frost-8fz2ns?file=%2Fsrc%2FApp.tsx

Version

Dec 2023 release, except for overlays which is 3.19.0

What browsers are you seeing the problem on?

Chrome

If other, please specify.

No response

What operating system are you using?

OSX Sonoma

๐Ÿงข Your Company/Team

No response

๐Ÿ•ท Tracking Issue

No response

ashleyryan avatar Apr 18 '24 16:04 ashleyryan

Maybe we could modify https://github.com/adobe/react-spectrum/blob/64ed13090ce77cc0e4cb4cd5602e75f655bff6bb/packages/%40react-aria/combobox/src/useComboBox.ts#L118-L120 so that it also skips the preventDefault if the user is in an allowsCustomValue combobox and none of the keys are focused?

LFDanLu avatar Apr 18 '24 17:04 LFDanLu

Correct me if I'm wrong, but this issue is only a problem when both allowsCustomValue and allowsEmptyCollection are specified, right? I've noticed that if only allowsCustomValue is set, then the combobox would close automatically when the typed input doesn't match any of the items, and thus the form is submitted via a single enter keystroke.

So I'm not entirely sure if it's a good idea to add an additional logic in the above if-statement to handle this specific use case? since it makes sense to close the menu first (even if it's an empty one), and then submit the form?

Would there be a better way to deal with this?

sookmax avatar Apr 23 '24 14:04 sookmax

There could also be the case where the user's custom value is a substring of the combobox's dropdown values in which case the dropdown would be open still even if allowsEmptyCollection is false. Open to alternative ideas/opinions but it feels to me that as long as virtual focus isn't set within the dropdown then we can treat interactions as happening purely on the input element and thus we no longer need to prevent default.

LFDanLu avatar Apr 24 '24 19:04 LFDanLu

For now, I've added a workaround that adds a keypress event listener to the input and manually triggers a click on a submit button we have in our component if there's no focusedKey

ashleyryan avatar May 13 '24 20:05 ashleyryan