NavSection.jsx 2.14 KB
Newer Older
wuhao's avatar
wuhao committed
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
import PropTypes from "prop-types";
import { NavLink as RouterLink } from "react-router-dom";
// @mui
import { Box, List, ListItemText, Typography } from "@mui/material";
//
import { StyledNavItem, StyledNavItemIcon } from "./styles";

// ----------------------------------------------------------------------

NavSection.propTypes = {
  data: PropTypes.array,
};

export default function NavSection({ data = [], collspan, ...other }) {
  return (
    <Box {...other}>
      <List disablePadding sx={{ p: 1 }}>
        {data.map((item) => {
          if (item.children) {
            return (
              <Box
                key={item.title}
                sx={{
                  pb:1.2
                }}
              >
                <Typography component="div" sx={{ pl: collspan?0:2.3, pt: 2, pb: 1,fontSize:"14px" }} align={collspan?"center":"left"}>
                  {item.title}
                </Typography>
                {item.children.map((item) => (
                  <NavItem collspan={collspan} key={item.title} item={item} />
                ))}
              </Box>
            );
          } else {
            return <NavItem collspan={collspan} key={item.title} item={item} />;
          }
        })}
      </List>
    </Box>
  );
}

// ----------------------------------------------------------------------

NavItem.propTypes = {
  item: PropTypes.object,
};

function NavItem({ item, collspan }) {
  const { title, path, icon, info } = item;

  return (
    <StyledNavItem
      component={RouterLink}
      to={path}
      sx={{
        display: "flex",
        justifyContent: "center",
        alignItems: "center",
        flexDirection: !collspan ? "row" : "column",
        margin: "6px 0",
        pt: !collspan ? 1 : 1.5,
        "&.active": {
          color: "text.primary",
          bgcolor: "action.selected",
          fontWeight: "fontWeightBold",
        },
      }}
    >
      <StyledNavItemIcon>{icon && icon}</StyledNavItemIcon>
      <ListItemText disableTypography primary={title} sx={{ pb: 0 }} />
      <Box sx={!collspan ? {} : { position: "absolute", right: 0, top: 0 }}>
        {info && info}
      </Box>
    </StyledNavItem>
  );
}