NavMenu.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import { useState, FC, useEffect } from 'react';
  2. import clsx from 'clsx';
  3. import { makeStyles, Theme, createStyles } from '@material-ui/core/styles';
  4. import Button from '@material-ui/core/Button';
  5. import List from '@material-ui/core/List';
  6. import ListItem from '@material-ui/core/ListItem';
  7. import ListItemIcon from '@material-ui/core/ListItemIcon';
  8. import ListItemText from '@material-ui/core/ListItemText';
  9. import { NavMenuItem, NavMenuType } from './Types';
  10. import icons from '../icons/Icons';
  11. import { useTranslation } from 'react-i18next';
  12. import Typography from '@material-ui/core/Typography';
  13. import ChevronRightIcon from '@material-ui/icons/ChevronRight';
  14. const duration = '100ms';
  15. const useStyles = makeStyles((theme: Theme) =>
  16. createStyles({
  17. root: {
  18. background: theme.palette.common.white,
  19. paddingTop: 0,
  20. paddingBottom: theme.spacing(5),
  21. display: 'flex',
  22. flexDirection: 'column',
  23. justifyContent: 'space-between',
  24. transition: theme.transitions.create('width', {
  25. duration,
  26. }),
  27. overflow: 'hidden',
  28. },
  29. rootCollapse: {
  30. width: '86px',
  31. },
  32. rootExpand: {
  33. width: (props: any) => props.width || '100%',
  34. },
  35. nested: {
  36. paddingLeft: theme.spacing(4),
  37. },
  38. item: {
  39. marginBottom: theme.spacing(2),
  40. paddingLeft: theme.spacing(4),
  41. boxSizing: 'content-box',
  42. height: theme.spacing(3),
  43. width: 'initial',
  44. color: theme.palette.milvusGrey.dark,
  45. },
  46. itemIcon: {
  47. minWidth: '20px',
  48. marginRight: theme.spacing(1),
  49. '& .icon': {
  50. fill: 'transparent',
  51. '& path': {
  52. stroke: theme.palette.milvusGrey.dark,
  53. },
  54. },
  55. },
  56. itemText: {
  57. transition: theme.transitions.create('opacity', {
  58. duration,
  59. }),
  60. whiteSpace: 'nowrap',
  61. opacity: 0,
  62. },
  63. itemTextExpand: {
  64. opacity: 1,
  65. },
  66. active: {
  67. color: theme.palette.primary.main,
  68. borderRight: `2px solid ${theme.palette.primary.main}`,
  69. '& .icon': {
  70. '& path': {
  71. stroke: theme.palette.primary.main,
  72. },
  73. },
  74. },
  75. logoWrapper: {
  76. width: '100%',
  77. display: 'flex',
  78. alignItems: 'center',
  79. height: '86px',
  80. marginBottom: theme.spacing(8),
  81. backgroundColor: theme.palette.primary.main,
  82. paddingLeft: theme.spacing(4),
  83. '& .title': {
  84. margin: 0,
  85. fontSize: '16px',
  86. letterSpacing: '0.15px',
  87. color: 'white',
  88. whiteSpace: 'nowrap',
  89. lineHeight: '86px',
  90. opacity: 0,
  91. transition: theme.transitions.create('opacity', {
  92. duration,
  93. }),
  94. },
  95. },
  96. logoWrapperExpand: {
  97. '& .title': {
  98. opacity: 1,
  99. },
  100. },
  101. logo: {
  102. transition: theme.transitions.create('all', {
  103. duration,
  104. }),
  105. },
  106. logoExpand: {
  107. marginRight: theme.spacing(1),
  108. '& path': {
  109. fill: 'white',
  110. },
  111. },
  112. logoCollapse: {
  113. backgroundColor: theme.palette.primary.main,
  114. '& path': {
  115. fill: 'white',
  116. },
  117. transform: 'scale(1.5)',
  118. },
  119. actionIcon: {
  120. position: 'fixed',
  121. borderRadius: '50%',
  122. backgroundColor: 'white',
  123. top: '74px',
  124. transition: theme.transitions.create('all', {
  125. duration,
  126. }),
  127. minWidth: '24px',
  128. padding: 0,
  129. '& svg path': {
  130. fill: theme.palette.milvusGrey.dark,
  131. },
  132. '&:hover': {
  133. backgroundColor: theme.palette.primary.main,
  134. '& svg path': {
  135. fill: 'white',
  136. },
  137. },
  138. },
  139. expandIcon: {
  140. left: '187px',
  141. transform: 'rotateZ(180deg)',
  142. },
  143. collapseIcon: {
  144. left: '73px',
  145. },
  146. })
  147. );
  148. const NavMenu: FC<NavMenuType> = props => {
  149. const { width, data, defaultActive = '' } = props;
  150. const classes = useStyles({ width });
  151. const [expanded, setExpanded] = useState<boolean>(true);
  152. const [active, setActive] = useState<string>(defaultActive);
  153. const { t } = useTranslation();
  154. const milvusTrans: { [key in string]: string } = t('milvus');
  155. useEffect(() => {
  156. if (defaultActive) {
  157. setActive(defaultActive);
  158. }
  159. }, [defaultActive]);
  160. const NestList = (props: { data: NavMenuItem[]; className?: string }) => {
  161. const { className = '', data } = props;
  162. return (
  163. <>
  164. {data.map((v: NavMenuItem) => {
  165. const IconComponent = v.icon;
  166. const isActive = active === v.label;
  167. const iconClass =
  168. v.iconActiveClass && v.iconNormalClass
  169. ? isActive
  170. ? v.iconActiveClass
  171. : v.iconNormalClass
  172. : 'icon';
  173. return (
  174. <ListItem
  175. button
  176. key={v.label}
  177. title={v.label}
  178. className={
  179. clsx(classes.item, {
  180. [className]: className,
  181. [classes.active]: isActive,
  182. })
  183. }
  184. onClick={() => {
  185. setActive(v.label);
  186. v.onClick && v.onClick();
  187. }}
  188. >
  189. <ListItemIcon className={classes.itemIcon}>
  190. <IconComponent classes={{ root: iconClass }} />
  191. </ListItemIcon>
  192. <ListItemText
  193. className={
  194. clsx(classes.itemText, {
  195. [classes.itemTextExpand]: expanded,
  196. })
  197. }
  198. primary={v.label} />
  199. </ListItem>
  200. );
  201. })}
  202. </>
  203. );
  204. };
  205. const Logo = icons.milvus;
  206. return (
  207. <List component="nav" className={
  208. clsx(classes.root, {
  209. [classes.rootExpand]: expanded,
  210. [classes.rootCollapse]: !expanded,
  211. })
  212. }>
  213. <div>
  214. <div className={
  215. clsx(classes.logoWrapper, {
  216. [classes.logoWrapperExpand]: expanded,
  217. })
  218. }>
  219. <Logo
  220. classes={{ root: classes.logo }}
  221. className={
  222. clsx({
  223. [classes.logoExpand]: expanded,
  224. [classes.logoCollapse]: !expanded,
  225. })
  226. }
  227. />
  228. <Typography variant="h3" className="title">
  229. {milvusTrans.admin}
  230. </Typography>
  231. </div>
  232. <Button
  233. onClick={() => { setExpanded(!expanded) }}
  234. className={
  235. clsx(classes.actionIcon, {
  236. [classes.expandIcon]: expanded,
  237. [classes.collapseIcon]: !expanded,
  238. })
  239. }
  240. >
  241. <ChevronRightIcon />
  242. </Button>
  243. <NestList data={data} />
  244. </div>
  245. </List>
  246. );
  247. };
  248. export default NavMenu;