Skip to content

[select] Consider space as part of the search query - #49929

Open
silviuaavram wants to merge 5 commits into
mui:masterfrom
silviuaavram:fix/select-space-typeahead
Open

silviuaavram wants to merge 5 commits into
mui:masterfrom
silviuaavram:fix/select-space-typeahead

Conversation

@silviuaavram

@silviuaavram silviuaavram commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Fixes #49919
Closes #49923 since it has some missing cases.

When a Select is 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, typing day s should 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

  1. Defer Space activation until the event reaches the list. Previously, SelectInput clicked the option in its onKeyDown handler. That handler runs before the event bubbles to MenuList, which owns the typeahead buffer. The option now runs its own onKeyDown and sets event.defaultMuiPrevented to skip ButtonBase’s built-in Space handling. This lets the event reach the list with the consumer’s defaultPrevented decision intact.

  2. Let MenuList decide whether Space belongs to the query. MenuList checks its existing buffer and 500 ms timeout. If a query is active, it appends the space and calls preventDefault(), 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.

  3. Run selection after the list’s keyboard handler. Menu calls the resolved list onKeyDown, then invokes the private internalOnOptionSpaceKeyDown callback only if Space is still unconsumed. Menu is where handlers supplied directly or through MuiMenu.defaultProps have already been resolved, so either can cancel activation. The callback lives in SelectInput and 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. SelectInput doesn’t need to track previous keystrokes or maintain a second timeout, and custom list slots can continue forwarding the standard onKeyDown handler.

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.

@silviuaavram silviuaavram added the scope: select Changes related to the select. label Nov 26, 2026
Copilot AI max review requested due to automatic review settings November 26, 2026 16:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@code-infra-dashboard

code-infra-dashboard Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Deploy preview

https://deploy-preview-49929--material-ui.netlify.app/
QR code for https://deploy-preview-49929--material-ui.netlify.app/

Bundle size

Bundle Parsed size Gzip size
@mui/material 🔺+631B(+0.11%) 🔺+230B(+0.14%)
@mui/lab 0B(0.00%) 0B(0.00%)
@mui/private-theming 0B(0.00%) 0B(0.00%)
@mui/system 0B(0.00%) 0B(0.00%)
@mui/utils 0B(0.00%) 0B(0.00%)

Details of bundle changes

Performance

No 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 aarongarciah left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@silviuaavram

Copy link
Copy Markdown
Member Author

Tested locally on 4c253fe. The approach holds up and multi-word queries work as described. A few notes:

Thanks @aarongarciah ! Addressed the points:

  1. **Preserve option activation when propagation stops, and respect aria-disabled. If an option calls stopPropagation(), Space can still activate it locally because the keydown never reaches the list. Calling preventDefault() cancels activation. Both paths use the same guard for aria-disabled="true", and the regression test focuses the disabled option before pressing Space. 81cb663
  2. End typeahead when the user navigates. MenuList clears the query when it handles ArrowUp, ArrowDown, Home, or End. Typing u → ArrowDown → Space now selects the newly focused option immediately. A letter after navigation starts a fresh query, while Shift preserves the current query for capital letters. d9e6f39
  3. Test the buffer directly in MenuList. Added three integration cases beside the 500 ms timeout test. They verify that Space extends an active query, standalone Space stays out of the next query, and leaving and re-entering the list clears the buffer before the timeout expires. 4fcff83**

@aarongarciah aarongarciah left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-disabled guard removed. onChange was never called for a disabled MenuItem, the regression was the bubbling click. A list onClick spy 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 cloned onKeyDown. Unlikely, but event.isPropagationStopped?.() avoids it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: select Changes related to the select.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[select] Space on an option ignores the open-menu type-ahead and modifier keys, and starts a type-ahead search after toggling (since 9.1)

3 participants