|
| 1 | +import PropTypes from 'prop-types'; |
| 2 | +import { useState } from 'react'; |
| 3 | +import { makeStyles, createStyles } from '@material-ui/core/styles'; |
| 4 | +import List from '@material-ui/core/List'; |
| 5 | +import ListItemIcon from '@material-ui/core/ListItemIcon'; |
| 6 | +import ListItemText from '@material-ui/core/ListItemText'; |
| 7 | +import Divider from '@material-ui/core/Divider'; |
| 8 | +import Collapse from '@material-ui/core/Collapse'; |
| 9 | +import IconExpandLess from '@material-ui/icons/ExpandLess'; |
| 10 | +import IconExpandMore from '@material-ui/icons/ExpandMore'; |
| 11 | +import SideBarItemComponent from './SideBarItemComponent'; |
| 12 | + |
| 13 | +const useStyles = makeStyles(() => |
| 14 | + createStyles({ |
| 15 | + sideBarItem: { |
| 16 | + '&.active': { |
| 17 | + color: '#2F80ED', // text |
| 18 | + '& .MuiListItemIcon-root': { |
| 19 | + color: '#2F80ED', // icon active |
| 20 | + }, |
| 21 | + }, |
| 22 | + }, |
| 23 | + sideBarItemIcon: { |
| 24 | + color: '#AAB1BA', |
| 25 | + }, |
| 26 | + }) |
| 27 | +); |
| 28 | + |
| 29 | +export const SideBarItemPropTypes = { |
| 30 | + name: PropTypes.string.isRequired, |
| 31 | + link: PropTypes.string, |
| 32 | + Icon: PropTypes.elementType, |
| 33 | + items: PropTypes.array, |
| 34 | +}; |
| 35 | + |
| 36 | +type SideBarItemPropTypes = PropTypes.InferProps<typeof SideBarItemPropTypes>; |
| 37 | +type SideBarItemPropsWithoutItems = Omit<SideBarItemPropTypes, 'items'>; |
| 38 | + |
| 39 | +export type SideBarItemProps = SideBarItemPropsWithoutItems & { |
| 40 | + items?: SideBarItemProps[]; |
| 41 | +}; |
| 42 | + |
| 43 | +function SideBarItem({ name, link, Icon, items = [] }: SideBarItemProps) { |
| 44 | + const classes = useStyles(); |
| 45 | + const isExpandable = items && items.length > 0; |
| 46 | + const [open, setOpen] = useState(false); |
| 47 | + function handleClick() { |
| 48 | + setOpen(!open); |
| 49 | + } |
| 50 | + const SideBarItemRoot = ( |
| 51 | + <SideBarItemComponent className={classes.sideBarItem} link={link} onClick={handleClick}> |
| 52 | + {!!Icon && ( |
| 53 | + <ListItemIcon className={classes.sideBarItemIcon}> |
| 54 | + <Icon /> |
| 55 | + </ListItemIcon> |
| 56 | + )} |
| 57 | + <ListItemText primary={name} inset={!Icon} /> |
| 58 | + {isExpandable && !open && <IconExpandMore />} |
| 59 | + {isExpandable && open && <IconExpandLess />} |
| 60 | + </SideBarItemComponent> |
| 61 | + ); |
| 62 | + |
| 63 | + const SideBarItemChildren = isExpandable ? ( |
| 64 | + <Collapse in={open} timeout="auto" unmountOnExit> |
| 65 | + <Divider /> |
| 66 | + <List component="div" disablePadding> |
| 67 | + {items.map((item, index) => ( |
| 68 | + <SideBarItem {...item} key={index} /> |
| 69 | + ))} |
| 70 | + </List> |
| 71 | + </Collapse> |
| 72 | + ) : null; |
| 73 | + |
| 74 | + return ( |
| 75 | + <> |
| 76 | + {SideBarItemRoot} |
| 77 | + {SideBarItemChildren} |
| 78 | + </> |
| 79 | + ); |
| 80 | +} |
| 81 | + |
| 82 | +export default SideBarItem; |
0 commit comments