Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[material-ui][Select] Allow specifying which keydowns open the menu #40501

Open
1 task done
steven-cole-elliott opened this issue Jan 9, 2024 · 2 comments
Open
1 task done
Assignees
Labels
component: select This is the name of the generic UI component, not the React module! enhancement This is not a bug, nor a new feature package: material-ui Specific to @mui/material waiting for 👍 Waiting for upvotes

Comments

@steven-cole-elliott
Copy link

steven-cole-elliott commented Jan 9, 2024

Duplicates

  • I have searched the existing issues

Summary

It would be nice if developers could specify which keydowns open the menu without having to take ownership of what SelectInput otherwise manages internally when its open state is uncontrolled in order to do so.

Examples

No response

Motivation

We are using Selects as filter fields on a typical index screen, and we have already trained our users on using a CMD/CTRL + Enter keyboard shortcut to submit the current set of filter values to perform a search.

When that keyboard shortcut is issued and a Select has focus and/or is the active element, however, its menu will be opened.

It is great that pressing Enter when a Select has focus and/or is the active element does open its menu, but in the CMD/CTRL + Enter case, it would be a preferable UX to have it remained closed.

It is worth mentioning that Autocomplete does not open its menu when CMD/CTRL + Enter is pressed when it has focus and/or is the active element.

On a more general note, there seemed to be several old, closed issues that discussed event propagation and/or the ability to compose default behavior of event handling into custom event handlers, but all of those seemed to be in the context of Autocomplete:

Perhaps related was mui/mui-x#1403 specific to DataGrid that introduced the defaultMuiPrevented on events which I'm assuming eventually led to being able to disable the default behavior The motivations there may not be identical here, but it did seem to deal with allowing developers to override the default event handling in cases without having to completely opt out of it and write it themselves if there were still cases where they wanted the default event handling behavior.

I most certainly do not have the context that y'all have to make the best informed decision about how to best add something like this to the API, so take this suggestion purely as another way to explain what I'd like to be able to do. It seems like

const handleKeyDown = (event) => {
if (!readOnly) {
const validKeys = [
' ',
'ArrowUp',
'ArrowDown',
// The native select doesn't respond to enter on macOS, but it's recommended by
// https://www.w3.org/WAI/ARIA/apg/patterns/combobox/examples/combobox-select-only/
'Enter',
];
if (validKeys.indexOf(event.key) !== -1) {
event.preventDefault();
update(true, event);
}
}
};

could start by accepting some prop that allowed developers to specify which keydown event conditions should open the menu without having to provide the onKeyDown prop such that the logic in SelectInput's internal handleKeyDown could still be called. I'm imagining something like the following being evaluated that would take the place of the validKeys inclusion check within handleKeyDown in the snippet above

// Type for new openOnKeyDown prop
type OpenOnKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => boolean;

// Example openOnKeyDown prop function that would keep the existing behavior but opt-out when CMD/CTRL + Enter was pressed
const openOnKeyDown: OpenOnKeyDown = (event) => {
    const validKeys = ['ArrowUp', 'ArrowDown', ' '];
  if (validKeys.indexOf(event.key) !== -1 || (event.key === "Enter" && !event.metaKey)) {
    return true;
  }
  return false;
}

Thank you for your consideration and your work on this project.

Search keywords:

@steven-cole-elliott steven-cole-elliott added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Jan 9, 2024
@danilo-leal danilo-leal changed the title [Select] Allow composing keydown behavior [material-ui][Select] Allow composing keydown behavior Jan 9, 2024
@danilo-leal danilo-leal added component: select This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material labels Jan 9, 2024
@steven-cole-elliott steven-cole-elliott changed the title [material-ui][Select] Allow composing keydown behavior [material-ui][Select] Allow specifying which keydowns open the menu Jan 10, 2024
@steven-cole-elliott
Copy link
Author

Note that I changed the issue title and summary to be more pointed and specific about the functionality that is desired.

While composability of the default behavior that SelectInput implements via its internal handleKeyDown would be nice, that expands the scope of the actual problem I'm trying to solve, which is only wanting to control which keydowns open the menu without having to make the open state controlled.

@DiegoAndai DiegoAndai added enhancement This is not a bug, nor a new feature and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Jan 22, 2024
@DiegoAndai DiegoAndai assigned DiegoAndai and unassigned mj12albert Jan 22, 2024
@DiegoAndai DiegoAndai added the waiting for 👍 Waiting for upvotes label Jan 22, 2024
@DiegoAndai
Copy link
Member

Hey @steven-cole-elliott, thanks for the report!

I added the waiting for upvotes label so the community can vote for this new feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: select This is the name of the generic UI component, not the React module! enhancement This is not a bug, nor a new feature package: material-ui Specific to @mui/material waiting for 👍 Waiting for upvotes
Projects
None yet
Development

No branches or pull requests

4 participants