1 | import { forwardRef } from 'react';
|
---|
2 | // @mui
|
---|
3 | import Box from '@mui/material/Box';
|
---|
4 | import Link from '@mui/material/Link';
|
---|
5 | import Tooltip from '@mui/material/Tooltip';
|
---|
6 | import ListItemText from '@mui/material/ListItemText';
|
---|
7 | // routes
|
---|
8 | import { RouterLink } from 'src/routes/components';
|
---|
9 | //
|
---|
10 | import Iconify from '../../iconify';
|
---|
11 | //
|
---|
12 | import { NavItemProps, NavConfigProps } from '../types';
|
---|
13 | import { StyledItem, StyledIcon } from './styles';
|
---|
14 |
|
---|
15 | // ----------------------------------------------------------------------
|
---|
16 |
|
---|
17 | type Props = NavItemProps & {
|
---|
18 | config: NavConfigProps;
|
---|
19 | };
|
---|
20 |
|
---|
21 | const NavItem = forwardRef<HTMLDivElement, Props>(
|
---|
22 | ({ item, depth, open, active, externalLink, config, ...other }, ref) => {
|
---|
23 | const { title, path, icon, info, children, disabled, caption, roles } = item;
|
---|
24 |
|
---|
25 | const subItem = depth !== 1;
|
---|
26 |
|
---|
27 | const renderContent = (
|
---|
28 | <StyledItem
|
---|
29 | disableGutters
|
---|
30 | ref={ref}
|
---|
31 | open={open}
|
---|
32 | depth={depth}
|
---|
33 | active={active}
|
---|
34 | disabled={disabled}
|
---|
35 | config={config}
|
---|
36 | {...other}
|
---|
37 | >
|
---|
38 | {icon && (
|
---|
39 | <StyledIcon
|
---|
40 | size={config.iconSize}
|
---|
41 | sx={{
|
---|
42 | ...(subItem && { mr: 1.5 }),
|
---|
43 | }}
|
---|
44 | >
|
---|
45 | {icon}
|
---|
46 | </StyledIcon>
|
---|
47 | )}
|
---|
48 |
|
---|
49 | {!(config.hiddenLabel && !subItem) && (
|
---|
50 | <ListItemText
|
---|
51 | sx={{
|
---|
52 | ...(!subItem && {
|
---|
53 | ml: 1,
|
---|
54 | }),
|
---|
55 | }}
|
---|
56 | primary={title}
|
---|
57 | primaryTypographyProps={{
|
---|
58 | noWrap: true,
|
---|
59 | typography: 'body2',
|
---|
60 | textTransform: 'capitalize',
|
---|
61 | fontWeight: active ? 'fontWeightBold' : 'fontWeightMedium',
|
---|
62 | ...(subItem && {
|
---|
63 | fontWeight: active ? 'fontWeightSemiBold' : 'fontWeightMedium',
|
---|
64 | }),
|
---|
65 | }}
|
---|
66 | />
|
---|
67 | )}
|
---|
68 |
|
---|
69 | {info && (
|
---|
70 | <Box component="span" sx={{ ml: 0.5, lineHeight: 0 }}>
|
---|
71 | {info}
|
---|
72 | </Box>
|
---|
73 | )}
|
---|
74 |
|
---|
75 | {caption && (
|
---|
76 | <Tooltip title={caption} arrow>
|
---|
77 | <Iconify width={16} icon="eva:info-outline" sx={{ ml: 0.5, color: 'text.disabled' }} />
|
---|
78 | </Tooltip>
|
---|
79 | )}
|
---|
80 |
|
---|
81 | {!!children && (
|
---|
82 | <Iconify
|
---|
83 | icon={subItem ? 'eva:arrow-ios-forward-fill' : 'eva:arrow-ios-downward-fill'}
|
---|
84 | width={16}
|
---|
85 | sx={{ flexShrink: 0, ml: 0.5 }}
|
---|
86 | />
|
---|
87 | )}
|
---|
88 | </StyledItem>
|
---|
89 | );
|
---|
90 |
|
---|
91 | // Hidden item by role
|
---|
92 | if (roles && !roles.includes(`${config.currentRole}`)) {
|
---|
93 | return null;
|
---|
94 | }
|
---|
95 |
|
---|
96 | // External link
|
---|
97 | if (externalLink)
|
---|
98 | return (
|
---|
99 | <Link
|
---|
100 | href={path}
|
---|
101 | target="_blank"
|
---|
102 | rel="noopener"
|
---|
103 | underline="none"
|
---|
104 | sx={{
|
---|
105 | ...(disabled && {
|
---|
106 | cursor: 'default',
|
---|
107 | }),
|
---|
108 | }}
|
---|
109 | >
|
---|
110 | {renderContent}
|
---|
111 | </Link>
|
---|
112 | );
|
---|
113 |
|
---|
114 | // Default
|
---|
115 | return (
|
---|
116 | <Link
|
---|
117 | component={RouterLink}
|
---|
118 | href={path}
|
---|
119 | underline="none"
|
---|
120 | sx={{
|
---|
121 | ...(disabled && {
|
---|
122 | cursor: 'default',
|
---|
123 | }),
|
---|
124 | }}
|
---|
125 | >
|
---|
126 | {renderContent}
|
---|
127 | </Link>
|
---|
128 | );
|
---|
129 | }
|
---|
130 | );
|
---|
131 |
|
---|
132 | export default NavItem;
|
---|