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] Fix Select when using the spacebar #43966

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

sai6855
Copy link
Contributor

@sai6855 sai6855 commented Oct 2, 2024

@sai6855 sai6855 marked this pull request as draft October 2, 2024 13:45
@sai6855 sai6855 added bug 🐛 Something doesn't work component: select This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material labels Oct 2, 2024
@sai6855 sai6855 changed the title [material-ui][Select] Fix Select selecting using the spacebar does not work [material-ui][Select] Fix Select when using the spacebar Oct 2, 2024
@mui-bot
Copy link

mui-bot commented Oct 2, 2024

Netlify deploy preview

https://deploy-preview-43966--material-ui.netlify.app/

Bundle size report

Details of bundle changes (Toolpad)
Details of bundle changes

Generated by 🚫 dangerJS against 002a319

Comment on lines 333 to 335
const child = childrenArray.find(
(childItem) => childItem.props.value === event.target.dataset.value,
);
Copy link
Member

Choose a reason for hiding this comment

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

Should be handled like onClick: handleItemClick(child), no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this is much better. updated in this commit 9e01896

@sai6855 sai6855 marked this pull request as ready for review October 3, 2024 07:15
@sai6855 sai6855 marked this pull request as draft October 3, 2024 07:16
@sai6855 sai6855 marked this pull request as ready for review October 3, 2024 07:27
Comment on lines 333 to 335
if (child.props.onKeyDown) {
child.props.onKeyDown(event);
}
Copy link
Member

Choose a reason for hiding this comment

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

Why do we call the custom onKeyDown handler only on space, shouldn't this be outside of the condition?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops sorry, i overlooked this. moved child.props.onKeyDown(event); out of condition and modified test to reflect same here e9f5d5b

Copy link
Member

@mnajdova mnajdova left a comment

Choose a reason for hiding this comment

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

Left just one more nit, it looks good to me 👍

Co-authored-by: Marija Najdova <[email protected]>
Signed-off-by: sai chand <[email protected]>
Copy link
Member

@DiegoAndai DiegoAndai left a comment

Choose a reason for hiding this comment

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

I don't think this is the correct solution to this problem.

The reason the spacebar doesn't select items is because we're blocking it here: https://github.com/mui/material-ui/blob/master/packages/mui-material/src/Select/SelectInput.js#L401-L404. If we remove line 404's preventDefault, then the spacebar selects items as it triggers onClick.

This change might also open a can of worms of behavior changes that users might consider breaking, in a very important component as Select. This behavior has been there from v5 at least. I wonder if it's better to wait for the Base UI refactor, given that we will have those changes coming shortly.

@mnajdova
Copy link
Member

The reason the spacebar doesn't select items is because we're blocking it here: https://github.com/mui/material-ui/blob/master/packages/mui-material/src/Select/SelectInput.js#L401-L404. If we remove line 404's preventDefault, then the spacebar selects items as it triggers onClick.

But we can't remove it, because it causes the other issue described in the comment. Adding the onKeyDown, actually allow us to capture an event and add the functionality without regressing the behavior.

This change might also open a can of worms of behavior changes that users might consider breaking, in a very important component as Select. This behavior has been there from v5 at least. I wonder if it's better to wait for the Base UI refactor, given that we will have those changes coming shortly.

I think regardless of how the solution looks like, we can investigate further if there is a better solution, we should fix the behavior. This is a basic accessibility feature that is broken on the component. It being broken for a long time is even more of a reason to fix it faster rather then waiting longer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: select This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[material-ui][Select] selecting using the spacebar does not work
5 participants