Repository navigation
[select] Consider space as part of the search query - #49929
silviuaavram wants to merge 5 commits into
Conversation
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.
Deploy previewBundle size
PerformanceNo regressions in 32 benchmarks · precise to ±10.2% (±14.1% at worst) · ran 4m 14s · details Check out the code infra dashboard for more information about this PR. |
aarongarciah
left a comment
There was a problem hiding this comment.
Tested locally on 4c253fe. The approach holds up and multi-word queries work as described. A few notes:
1. Space on a disabled, focusable option dispatches a click
With disabledItemsFocusable on the list slot, Space on a focused disabled option now reaches handleOptionSpaceKeyDown, which calls option.click(). The option isn't selected, because ButtonBase drops clicks while disabled, but the click still bubbles, so an onClick on the list fires. Before this PR no click was dispatched.
<Select
defaultOpen
defaultValue=""
MenuProps={{ slotProps: { list: { disabledItemsFocusable: true, onClick } } }}
>
<MenuItem value="admin" disabled>Admin</MenuItem>
<MenuItem value="user">User</MenuItem>
</Select>"Admin" is focused on open. Press Space and onClick runs once. Bailing out on aria-disabled fixes it:
- if (event.ctrlKey || event.metaKey || event.altKey || option.getAttribute('role') !== 'option') {
+ if (
+ event.ctrlKey ||
+ event.metaKey ||
+ event.altKey ||
+ option.getAttribute('role') !== 'option' ||
+ option.getAttribute('aria-disabled') === 'true'
+ ) {
return;
}2. Arrow keys don't end the query (optional)
u, ArrowDown, Space within 500 ms: the Space is consumed as part of the query and nothing is selected. Before this PR it selected. Clearing criteria.keys on ArrowUp/ArrowDown/Home/End in MenuList's handleKeyDown makes that Space select again. It also means a letter typed right after an arrow key starts a new query. It has to be limited to navigation keys: clearing on every non-character key resets the query on Shift, which breaks capitals mid-query.
With both changes applied, the Select, Menu, MenuList and MenuItem tests still pass.
3. MenuList coverage
The typeahead changes live in MenuList but are only covered through Select. Worth adding cases next to "should reset the character buffer after 500ms" in test/integration/MenuList.test.js: Space extends an active query, a standalone Space stays out of the buffer, and the query clears when focus re-enters the list.
4. Plain Menu (follow-up, not for this PR)
Typing day in a plain Menu still activates "Day off", since ButtonBase clicks on Space keyup regardless of the typeahead state. Same before this PR and outside #49919, but MenuList now knows when a Space belongs to the query, so it could be fixed there later.
Checked with throwaway tests in jsdom and in Chrome 158 (trusted key events, real timers), against this branch and its base.
Thanks @aarongarciah ! Addressed the points:
|
aarongarciah
left a comment
There was a problem hiding this comment.
Thanks! Re-tested 4fcff83 in Chrome 158 and jsdom, all three changes behave as described.
Two small things:
-
"leaves a focused aria-disabled option unselected" still passes with the
aria-disabledguard removed.onChangewas never called for a disabledMenuItem, the regression was the bubbling click. A listonClickspy catches it:MenuProps={{ slotProps: { list: { disabledItemsFocusable: true, onClick: onListClick } } }} // ... expect(onListClick).toHaveBeenCalledTimes(0);
-
Nit:
event.isPropagationStopped()throws if a custom option forwards a native keydown event to the clonedonKeyDown. Unlikely, butevent.isPropagationStopped?.()avoids it.
Fixes #49919
Closes #49923 since it has some missing cases.
When a
Selectis open, pressing Space while typing a multi-word label can select the focused option before the user finishes typing. For example, with “Day off” and “Day shift” in the list, typingday sshould move focus to “Day shift” and leave the listbox open. Pressing Space on its own should still select the focused option or toggle it in a multi-select.Implementation
Defer Space activation until the event reaches the list. Previously,
SelectInputclicked the option in itsonKeyDownhandler. That handler runs before the event bubbles toMenuList, which owns the typeahead buffer. The option now runs its ownonKeyDownand setsevent.defaultMuiPreventedto skipButtonBase’s built-in Space handling. This lets the event reach the list with the consumer’sdefaultPreventeddecision intact.Let
MenuListdecide whether Space belongs to the query.MenuListchecks its existing buffer and 500 ms timeout. If a query is active, it appends the space and callspreventDefault(), even when the resulting prefix matches nothing. Otherwise, a failed search could accidentally select the focused option. A standalone Space stays out of the buffer, so the user can toggle an option in a multi-select and immediately start a new search. Moving focus between options preserves the query. Returning focus from outside the list clears it, including when a kept-mounted menu reopens.Run selection after the list’s keyboard handler.
Menucalls the resolved listonKeyDown, then invokes the privateinternalOnOptionSpaceKeyDowncallback only if Space is still unconsumed.Menuis where handlers supplied directly or throughMuiMenu.defaultPropshave already been resolved, so either can cancel activation. The callback lives inSelectInputand clicks the option through the existing selection path. It ignores repeated keydowns, Ctrl/Alt/Meta+Space, and events belonging to nested menus.This keeps the open list’s search state in one place.
SelectInputdoesn’t need to track previous keystrokes or maintain a second timeout, and custom list slots can continue forwarding the standardonKeyDownhandler.Regression coverage
The tests cover multi-word and unmatched queries, selection after the timeout, starting a new search immediately after a multi-select toggle, cancellation by a themed handler, modifier keys, nested menus with and without portals, and reopening a kept-mounted listbox.