adobe / react-spectrum

A collection of libraries and tools that help you build adaptive, accessible, and robust user experiences.
https://react-spectrum.adobe.com
Apache License 2.0
12.76k stars 1.09k forks source link

Keyboard selection broken with Select when options include spaces - react-aria-components #5968

Open subvertallchris opened 7 months ago

subvertallchris commented 7 months ago

Provide a general summary of the issue here

The native HTML select element allows one to focus and then type an option name, like "new york" to select it. The Select element from react-aria-components instead opens the menu when spacebar is pressed and does not respect the spacebar as part of the typed selection state. Keyboard selection is broken as a result.

๐Ÿค” Expected Behavior?

One should be able to focus on a Select and type a multi-word option.

๐Ÿ˜ฏ Current Behavior

This fails as soon as you hit the space bar. See https://codesandbox.io/p/sandbox/async-flower-lj4gz5 for a usable demo. It also includes an example of the native select element.

Note that if you open the menu first and then type, "New York" can be selected.

๐Ÿ’ Possible Solution

I would expect some combination of the following

๐Ÿ”ฆ Context

The ability to quickly navigate and fill in forms is something I expect with all form elements. I hit this repeatedly while working on an e-commerce system, where Select Option elements might be multiple words. You'll notice that all of the examples on in the docs are single words, so it wouldn't be immediately obvious.

๐Ÿ–ฅ๏ธ Steps to Reproduce

See the reproduction here https://codesandbox.io/p/sandbox/async-flower-lj4gz5.

Version

1.1.0

What browsers are you seeing the problem on?

Firefox, Chrome, Safari

If other, please specify.

No response

What operating system are you using?

Mac OS

๐Ÿงข Your Company/Team

No response

๐Ÿ•ท Tracking Issue

No response

reidbarber commented 7 months ago

Looks like https://github.com/adobe/react-spectrum/pull/869 handled this in listbox, but we'll need to review the behavior to see why it doesn't work for select.

subvertallchris commented 7 months ago

Hey @reidbarber, thanks for taking a look. Any thoughts on the effort level or timeline? Is this something I can reasonably investigate on my own? I'm trying to decide if I should swap my Select implementation for a native HTML select in the meantime.

reidbarber commented 7 months ago

I would guess it wouldn't be too difficult of a fix once the cause is identified. Another related PR to look at is #1374. This maybe something to look at:

https://github.com/adobe/react-spectrum/blob/a8ec9667a2b70b88b45e329bf44918f0f93d54f9/packages/%40react-aria/select/src/useSelect.ts#L133-L134

There is a useSelect story in storybook that would make a quick feedback loop to test things. The main thing to look for is why the event makes it all the way to the onKeyDown handler in useMenuTrigger.