Skip to content

Commit 68ac415

Browse files
[docs][Menu] Add Grouped Menu demo (#45241)
Co-authored-by: Ayush Shrivastava <[email protected]>
1 parent ffb8ed9 commit 68ac415

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import * as React from 'react';
2+
import Button from '@mui/material/Button';
3+
import ListSubheader from '@mui/material/ListSubheader';
4+
import Menu from '@mui/material/Menu';
5+
import MenuItem from '@mui/material/MenuItem';
6+
7+
export default function GroupedMenu() {
8+
const [anchorEl, setAnchorEl] = React.useState(null);
9+
const open = Boolean(anchorEl);
10+
const handleClick = (event) => {
11+
setAnchorEl(event.currentTarget);
12+
};
13+
const handleClose = () => {
14+
setAnchorEl(null);
15+
};
16+
17+
return (
18+
<div>
19+
<Button
20+
id="basic-button"
21+
aria-controls={open ? 'grouped-menu' : undefined}
22+
aria-haspopup="true"
23+
aria-expanded={open ? 'true' : undefined}
24+
onClick={handleClick}
25+
>
26+
Dashboard
27+
</Button>
28+
<Menu
29+
id="grouped-menu"
30+
anchorEl={anchorEl}
31+
open={open}
32+
onClose={handleClose}
33+
MenuListProps={{
34+
'aria-labelledby': 'basic-button',
35+
}}
36+
>
37+
<ListSubheader>Category 1</ListSubheader>
38+
<MenuItem onClick={handleClose}>Option 1</MenuItem>
39+
<MenuItem onClick={handleClose}>Option 2</MenuItem>
40+
<ListSubheader>Category 2</ListSubheader>
41+
<MenuItem onClick={handleClose}>Option 3</MenuItem>
42+
<MenuItem onClick={handleClose}>Option 4</MenuItem>
43+
</Menu>
44+
</div>
45+
);
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import * as React from 'react';
2+
import Button from '@mui/material/Button';
3+
import ListSubheader from '@mui/material/ListSubheader';
4+
import Menu from '@mui/material/Menu';
5+
import MenuItem from '@mui/material/MenuItem';
6+
7+
export default function GroupedMenu() {
8+
const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);
9+
const open = Boolean(anchorEl);
10+
const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
11+
setAnchorEl(event.currentTarget);
12+
};
13+
const handleClose = () => {
14+
setAnchorEl(null);
15+
};
16+
17+
return (
18+
<div>
19+
<Button
20+
id="basic-button"
21+
aria-controls={open ? 'grouped-menu' : undefined}
22+
aria-haspopup="true"
23+
aria-expanded={open ? 'true' : undefined}
24+
onClick={handleClick}
25+
>
26+
Dashboard
27+
</Button>
28+
<Menu
29+
id="grouped-menu"
30+
anchorEl={anchorEl}
31+
open={open}
32+
onClose={handleClose}
33+
MenuListProps={{
34+
'aria-labelledby': 'basic-button',
35+
}}
36+
>
37+
<ListSubheader>Category 1</ListSubheader>
38+
<MenuItem onClick={handleClose}>Option 1</MenuItem>
39+
<MenuItem onClick={handleClose}>Option 2</MenuItem>
40+
<ListSubheader>Category 2</ListSubheader>
41+
<MenuItem onClick={handleClose}>Option 3</MenuItem>
42+
<MenuItem onClick={handleClose}>Option 4</MenuItem>
43+
</Menu>
44+
</div>
45+
);
46+
}

docs/data/material/components/menus/menus.md

+6
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ Here is an example of a context menu. (Right click to open.)
113113

114114
{{"demo": "ContextMenu.js"}}
115115

116+
## Grouped Menu
117+
118+
Display categories with the `ListSubheader` component.
119+
120+
{{"demo": "GroupedMenu.js"}}
121+
116122
## Supplementary projects
117123

118124
For more advanced use cases you might be able to take advantage of:

0 commit comments

Comments
 (0)