Skip to content

Commit

Permalink
[Docs][Menus] Adding an example for Grouped Menu
Browse files Browse the repository at this point in the history
  • Loading branch information
jackcrack3110 committed Feb 7, 2025
1 parent c2335e6 commit ac18cfc
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
49 changes: 49 additions & 0 deletions docs/data/material/components/menus/GroupedMenu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import * as React from 'react';
import Button from '@mui/material/Button';
import ListSubheader from '@mui/material/ListSubheader';
import Menu from '@mui/material/Menu';
import MenuItem from '@mui/material/MenuItem';
import MenuList from '@mui/material/MenuList';

export default function GroupedMenu() {
const [anchorEl, setAnchorEl] = React.useState(null);
const open = Boolean(anchorEl);
const handleClick = (event) => {
setAnchorEl(event.currentTarget);
};
const handleClose = () => {
setAnchorEl(null);
};

return (
<div>
<Button
id="basic-button"
aria-controls={open ? 'grouped-menu' : undefined}
aria-haspopup="true"
aria-expanded={open ? 'true' : undefined}
onClick={handleClick}
>
Dashboard
</Button>
<Menu
id="grouped-menu"
anchorEl={anchorEl}
open={open}
onClose={handleClose}
MenuListProps={{
'aria-labelledby': 'basic-button',
}}
>
<MenuList role="group">
<ListSubheader>Category 1</ListSubheader>
<MenuItem onClick={handleClose}>Option 1</MenuItem>
<MenuItem onClick={handleClose}>Option 2</MenuItem>
<ListSubheader>Category 2</ListSubheader>
<MenuItem onClick={handleClose}>Option 3</MenuItem>
<MenuItem onClick={handleClose}>Option 4</MenuItem>
</MenuList>
</Menu>
</div>
);
}
49 changes: 49 additions & 0 deletions docs/data/material/components/menus/GroupedMenu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import * as React from 'react';
import Button from '@mui/material/Button';
import ListSubheader from '@mui/material/ListSubheader';
import Menu from '@mui/material/Menu';
import MenuItem from '@mui/material/MenuItem';
import MenuList from '@mui/material/MenuList';

export default function GroupedMenu() {
const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);
const open = Boolean(anchorEl);
const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
setAnchorEl(event.currentTarget);
};
const handleClose = () => {
setAnchorEl(null);
};

return (
<div>
<Button
id="basic-button"
aria-controls={open ? 'grouped-menu' : undefined}
aria-haspopup="true"
aria-expanded={open ? 'true' : undefined}
onClick={handleClick}
>
Dashboard
</Button>
<Menu
id="grouped-menu"
anchorEl={anchorEl}
open={open}
onClose={handleClose}
MenuListProps={{
'aria-labelledby': 'basic-button',
}}
>
<MenuList role="group">
<ListSubheader>Category 1</ListSubheader>
<MenuItem onClick={handleClose}>Option 1</MenuItem>
<MenuItem onClick={handleClose}>Option 2</MenuItem>
<ListSubheader>Category 2</ListSubheader>
<MenuItem onClick={handleClose}>Option 3</MenuItem>
<MenuItem onClick={handleClose}>Option 4</MenuItem>
</MenuList>
</Menu>
</div>
);
}
6 changes: 6 additions & 0 deletions docs/data/material/components/menus/menus.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ Here is an example of a context menu. (Right click to open.)

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

## Grouped Menu

Display categories with the `ListSubheader` component.

{{"demo": "GroupedMenu.js"}}

## Supplementary projects

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

0 comments on commit ac18cfc

Please sign in to comment.