-
-
Notifications
You must be signed in to change notification settings - Fork 32.6k
[MenuItem] add aria-checked for checkbox and radio menu items
#48651
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
Open
siriwatknp
wants to merge
1
commit into
mui:master
Choose a base branch
from
siriwatknp:feat/menuitem-checkbox-role
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+231
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import * as React from 'react'; | ||
| import Paper from '@mui/material/Paper'; | ||
| import MenuList from '@mui/material/MenuList'; | ||
| import MenuItem from '@mui/material/MenuItem'; | ||
| import ListItemText from '@mui/material/ListItemText'; | ||
| import ListItemIcon from '@mui/material/ListItemIcon'; | ||
| import Check from '@mui/icons-material/Check'; | ||
|
|
||
| const options = ['Show toolbar', 'Show sidebar', 'Show status bar']; | ||
|
|
||
| export default function CheckboxMenu() { | ||
| const [checked, setChecked] = React.useState({ | ||
| 'Show toolbar': true, | ||
| }); | ||
|
|
||
| const handleToggle = (option) => () => { | ||
| setChecked((prev) => ({ ...prev, [option]: !prev[option] })); | ||
| }; | ||
|
|
||
| return ( | ||
| <Paper sx={{ width: 320, maxWidth: '100%' }}> | ||
| <MenuList> | ||
| {options.map((option) => ( | ||
| <MenuItem | ||
| key={option} | ||
| role="menuitemcheckbox" | ||
| selected={Boolean(checked[option])} | ||
| onClick={handleToggle(option)} | ||
| > | ||
| <ListItemIcon> | ||
| {checked[option] ? <Check fontSize="small" /> : null} | ||
| </ListItemIcon> | ||
| <ListItemText>{option}</ListItemText> | ||
| </MenuItem> | ||
| ))} | ||
| </MenuList> | ||
| </Paper> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import * as React from 'react'; | ||
| import Paper from '@mui/material/Paper'; | ||
| import MenuList from '@mui/material/MenuList'; | ||
| import MenuItem from '@mui/material/MenuItem'; | ||
| import ListItemText from '@mui/material/ListItemText'; | ||
| import ListItemIcon from '@mui/material/ListItemIcon'; | ||
| import Check from '@mui/icons-material/Check'; | ||
|
|
||
| const options = ['Show toolbar', 'Show sidebar', 'Show status bar']; | ||
|
|
||
| export default function CheckboxMenu() { | ||
| const [checked, setChecked] = React.useState<Record<string, boolean>>({ | ||
| 'Show toolbar': true, | ||
| }); | ||
|
|
||
| const handleToggle = (option: string) => () => { | ||
| setChecked((prev) => ({ ...prev, [option]: !prev[option] })); | ||
| }; | ||
|
|
||
| return ( | ||
| <Paper sx={{ width: 320, maxWidth: '100%' }}> | ||
| <MenuList> | ||
| {options.map((option) => ( | ||
| <MenuItem | ||
| key={option} | ||
| role="menuitemcheckbox" | ||
| selected={Boolean(checked[option])} | ||
| onClick={handleToggle(option)} | ||
| > | ||
| <ListItemIcon> | ||
| {checked[option] ? <Check fontSize="small" /> : null} | ||
| </ListItemIcon> | ||
| <ListItemText>{option}</ListItemText> | ||
| </MenuItem> | ||
| ))} | ||
| </MenuList> | ||
| </Paper> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import * as React from 'react'; | ||
| import Paper from '@mui/material/Paper'; | ||
| import MenuList from '@mui/material/MenuList'; | ||
| import MenuItem from '@mui/material/MenuItem'; | ||
| import ListItemText from '@mui/material/ListItemText'; | ||
| import ListItemIcon from '@mui/material/ListItemIcon'; | ||
| import RadioButtonChecked from '@mui/icons-material/RadioButtonChecked'; | ||
| import RadioButtonUnchecked from '@mui/icons-material/RadioButtonUnchecked'; | ||
|
|
||
| const options = ['Name', 'Date modified', 'Size']; | ||
|
|
||
| export default function RadioMenu() { | ||
| const [selected, setSelected] = React.useState('Name'); | ||
|
|
||
| return ( | ||
| <Paper sx={{ width: 320, maxWidth: '100%' }}> | ||
| <MenuList> | ||
| {options.map((option) => ( | ||
| <MenuItem | ||
| key={option} | ||
| role="menuitemradio" | ||
| selected={selected === option} | ||
| onClick={() => setSelected(option)} | ||
| > | ||
| <ListItemIcon> | ||
| {selected === option ? ( | ||
| <RadioButtonChecked fontSize="small" /> | ||
| ) : ( | ||
| <RadioButtonUnchecked fontSize="small" /> | ||
| )} | ||
| </ListItemIcon> | ||
| <ListItemText>{option}</ListItemText> | ||
| </MenuItem> | ||
| ))} | ||
| </MenuList> | ||
| </Paper> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import * as React from 'react'; | ||
| import Paper from '@mui/material/Paper'; | ||
| import MenuList from '@mui/material/MenuList'; | ||
| import MenuItem from '@mui/material/MenuItem'; | ||
| import ListItemText from '@mui/material/ListItemText'; | ||
| import ListItemIcon from '@mui/material/ListItemIcon'; | ||
| import RadioButtonChecked from '@mui/icons-material/RadioButtonChecked'; | ||
| import RadioButtonUnchecked from '@mui/icons-material/RadioButtonUnchecked'; | ||
|
|
||
| const options = ['Name', 'Date modified', 'Size']; | ||
|
|
||
| export default function RadioMenu() { | ||
| const [selected, setSelected] = React.useState('Name'); | ||
|
|
||
| return ( | ||
| <Paper sx={{ width: 320, maxWidth: '100%' }}> | ||
| <MenuList> | ||
| {options.map((option) => ( | ||
| <MenuItem | ||
| key={option} | ||
| role="menuitemradio" | ||
| selected={selected === option} | ||
| onClick={() => setSelected(option)} | ||
| > | ||
| <ListItemIcon> | ||
| {selected === option ? ( | ||
| <RadioButtonChecked fontSize="small" /> | ||
| ) : ( | ||
| <RadioButtonUnchecked fontSize="small" /> | ||
| )} | ||
| </ListItemIcon> | ||
| <ListItemText>{option}</ListItemText> | ||
| </MenuItem> | ||
| ))} | ||
| </MenuList> | ||
| </Paper> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this API consistent with other such examples in the library?
if we want to keep this stateless, why not just create an example with both
roleandaria-checkedpassed directly by the users? we're not helping much with theselectedprop, we just translate it for them.the other option in my opinion is go stateful, and manage the checked state ourselves. in this case, we probably need a Context wrapper and maybe new component wrapping MenuItem: MenuItemCheckbox and MenuItemRadio or something.
what do you think? @siriwatknp @mj12albert
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re-using the name
selectedfor checkbox/radio items adds indirection because you typically think of them as "checked" and not "selected".Additionally if the API was to switch components via prop (e.g.
MenuItem variant="checkbox"), "selected" could influence any of these 3 things depending on the component:.Mui-selectedis added or notaria-checkedstateselectedprop is passed into the roving focus hookwhich could get hard to reason about when it's coupled to combinations of the
variantandselectedprops, does that make sense?So having new checkbox/radio item components gives us a way out of some restrictions/shortcomings of the current API