-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSidebarItem.tsx
180 lines (171 loc) · 4.45 KB
/
SidebarItem.tsx
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { ArrowDropDown, ArrowRight } from "@mui/icons-material";
import {
Badge,
Box,
Collapse,
ListItemButton,
ListItemIcon,
ListItemText
} from "@mui/material";
import type { BadgeProps } from "@mui/material";
import React from "react";
import { SidebarItemProps } from "./SidebarItem.types";
import { Theme } from "@mui/material/styles";
/**
* Sidebar list item with icon.
*/
export default function SidebarItem({
children,
className,
count,
disabled = false,
icon,
iconStyle = {},
initialOpen = false,
name,
onClick,
selected = false,
textStyle = {},
display = "inline"
}: SidebarItemProps) {
// use styles
const color = selected ? "primary" : "inherit";
const primaryTypographyProps = {
color,
sx: {
colorPrimary: { fontWeight: 500 }
}
};
// expansion state
const [expanded, setExpanded] = React.useState(initialOpen);
// item click callback
// if we have children we should set the expanded state as well
const handleClick = (event: React.MouseEvent<HTMLDivElement>) => {
children && setExpanded(!expanded);
onClick && onClick(event);
};
// Count badge component that displays the count
const CountBadge = (props: BadgeProps) => {
return count ? (
<Badge
badgeContent={count}
max={9}
color="primary"
// if display is inline then add a margin to the right
sx={{
...(display === "inline"
? { marginRight: (theme: Theme) => theme.spacing(3) }
: {})
}}
{...props}
/>
) : null;
};
// Expand icon component that displays the expand icon depending on the expanded state
const ExpandIcon = () => {
return children ? (
expanded ? (
<Badge badgeContent={<ArrowDropDown />} />
) : (
<Badge badgeContent={<ArrowRight />} />
)
) : null;
};
// Sidebar In Line component that gets displayed when the display prop is set to inline
const SidebarInLine = () => {
return (
<React.Fragment>
<ListItemIcon sx={iconStyle}>
{React.cloneElement(icon, { color })}
</ListItemIcon>
<ListItemText
primary={name}
primaryTypographyProps={primaryTypographyProps}
sx={textStyle}
/>
<CountBadge />
<ExpandIcon />
</React.Fragment>
);
};
// Sidebar Stacked component that gets displayed when the display prop is set to stacked
const SidebarStacked = () => {
return (
<Box
sx={{
display: "flex",
flexDirection: "row",
justifyContent: "center",
width: "100%"
}}
>
<Box
sx={{
display: "flex",
flexDirection: "column",
justifyContent: "center"
}}
>
<ListItemIcon
sx={{
display: "flex",
justifyContent: "center",
...iconStyle
}}
>
{React.cloneElement(icon, {
color,
fontSize: "medium"
})}
<CountBadge variant="dot" />
</ListItemIcon>
<ExpandIcon />
<ListItemText
primary={name}
primaryTypographyProps={primaryTypographyProps}
sx={{
".MuiTypography-root": { fontSize: "12px" },
display: "flex",
justifyContent: "center",
...textStyle
}}
/>
</Box>
</Box>
);
};
// return the sidebar item component
return (
<Box
sx={{
display: "flex",
flexDirection: "column"
}}
>
<ListItemButton
selected={selected}
onClick={handleClick}
disabled={disabled}
className={className}
sx={{
...(display === "inline" ? {} : { pb: 2, pl: 0, pr: 0, pt: 2 }),
...(selected === true && display === "stacked"
? {
borderRight: "2px solid",
borderRightColor: theme => theme.palette.primary.main
}
: {})
}}
>
{display === "inline" ? <SidebarInLine /> : <SidebarStacked />}
</ListItemButton>
<Collapse in={expanded} timeout="auto" unmountOnExit>
{React.Children.map(children, child =>
React.cloneElement(child as React.ReactElement, {
sx: { paddingLeft: (theme: Theme) => theme.spacing(4) }
})
)}
</Collapse>
</Box>
);
}