-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathNestedMenu.js
More file actions
44 lines (40 loc) · 1.38 KB
/
NestedMenu.js
File metadata and controls
44 lines (40 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { useState, useCallback } from 'react';
import PropTypes from 'prop-types';
import ListItemIcon from '@mui/material/ListItemIcon';
import ListItemText from '@mui/material/ListItemText';
import MenuItem from '@mui/material/MenuItem';
import ExpandLess from '@mui/icons-material/ExpandLessSharp';
import ExpandMore from '@mui/icons-material/ExpandMoreSharp';
/**
* NestedMenu ~ A presentation component to render a menu item and have
* it control the visibility of the MUI List passed in as the children
*/
export function NestedMenu({
children, icon = null, label, ...otherProps
}) {
const [nestedMenuIsOpen, setNestedMenuIsOpen] = useState(false);
const handleMenuClick = useCallback(() => {
setNestedMenuIsOpen(!nestedMenuIsOpen);
}, [nestedMenuIsOpen, setNestedMenuIsOpen]);
return (
<>
<MenuItem aria-expanded={nestedMenuIsOpen} onClick={handleMenuClick} divider={nestedMenuIsOpen} {...otherProps}>
{icon && (<ListItemIcon>{icon}</ListItemIcon>)}
<ListItemText primaryTypographyProps={{ variant: 'body1' }}>
{label}
</ListItemText>
{
nestedMenuIsOpen
? <ExpandLess />
: <ExpandMore />
}
</MenuItem>
{nestedMenuIsOpen && children}
</>
);
}
NestedMenu.propTypes = {
children: PropTypes.element.isRequired,
icon: PropTypes.element,
label: PropTypes.string.isRequired,
};