我使用 material-ui在此链接https://material-ui.com/components/lists/#simple-list上找到了我需要的内容。在此链接上有“嵌套列表项”一章。本章只有一个嵌套列表项,带有扩展更多/更少的方法。我的侧边栏中有两个嵌套项目。他们都调用了一个方法 handleClick 来扩展更多/更少的选项。我想每次单击仅展开一个(单击的)嵌套项目。我还想减少对上一项的扩展。这是我的侧边栏:
import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import Drawer from "@material-ui/core/Drawer";
import Toolbar from "@material-ui/core/Toolbar";
import List from "@material-ui/core/List";
import ListItem from "@material-ui/core/ListItem";
import ListItemIcon from "@material-ui/core/ListItemIcon";
import ListItemText from "@material-ui/core/ListItemText";
import Collapse from "@material-ui/core/Collapse";
import ExpandLess from "@material-ui/icons/ExpandLess";
import ExpandMore from "@material-ui/icons/ExpandMore";
import HomeIcon from '@material-ui/icons/Home';
import SupervisorAccountIcon from '@material-ui/icons/SupervisorAccount';
import AccessibilityNewIcon from '@material-ui/icons/AccessibilityNew';
import VisibilityIcon from '@material-ui/icons/Visibility';
import AddIcon from '@material-ui/icons/Add';
const drawerWidth = 260;
const useStyles = makeStyles((theme) => ({
drawer: {
width: drawerWidth,
flexShrink: 0,
},
drawerPaper: {
width: drawerWidth,
},
drawerContainer: {
overflow: "auto",
},
root: {
width: "100%",
maxWidth: 360,
backgroundColor: theme.palette.background.paper,
},
nested: {
paddingLeft: theme.spacing(4),
},
}));
export default function SideBar() {
const classes = useStyles();
const [open, setOpen] = React.useState(true);
const handleClick = () => {
setOpen(!open);
};
return (
<Drawer
className={classes.drawer}
variant="permanent"
classes={{
paper: classes.drawerPaper,
}}
>
<Toolbar />
<div className={classes.drawerContainer}>
<List
component="nav"
aria-labelledby="nested-list-subheader"
className={classes.root}
>
<ListItem button>
<ListItemIcon>
<HomeIcon />
</ListItemIcon>
<ListItemText primary="Home" />
</ListItem>
<ListItem button onClick={handleClick}>
<ListItemIcon>
<SupervisorAccountIcon />
</ListItemIcon>
<ListItemText primary="Administrators" />
{open ? <ExpandLess /> : <ExpandMore />}
</ListItem>
<Collapse in={open} timeout="auto" unmountOnExit>
<List component="div" disablePadding>
<ListItem button className={classes.nested}>
<ListItemIcon>
<VisibilityIcon />
</ListItemIcon>
<ListItemText primary="View" />
</ListItem>
<ListItem button className={classes.nested}>
<ListItemIcon>
<AddIcon />
</ListItemIcon>
<ListItemText primary="Add administrator" />
</ListItem>
</List>
</Collapse>
<ListItem button onClick={handleClick}>
<ListItemIcon>
<AccessibilityNewIcon />
</ListItemIcon>
<ListItemText primary="Trainers" />
{open ? <ExpandLess /> : <ExpandMore />}
</ListItem>
<Collapse in={open} timeout="auto" unmountOnExit>
<List component="div" disablePadding>
<ListItem button className={classes.nested}>
<ListItemIcon>
<VisibilityIcon />
</ListItemIcon>
<ListItemText primary="View" />
</ListItem>
<ListItem button className={classes.nested}>
<ListItemIcon>
<AddIcon />
</ListItemIcon>
<ListItemText primary="Add trainer" />
</ListItem>
</List>
</Collapse>
</List>
</div>
</Drawer>
);
}
知道如何做到这一点吗?