gpt4 book ai didi

reactjs - material-ui reactjs中的嵌套抽屉

转载 作者:行者123 更新时间:2023-12-05 01:11:30 25 4
gpt4 key购买 nike

我在 Reactjs 中使用 Material UI。我正在寻找嵌套的抽屉。这是一个例子

Nested Drawer

我看到 Material UI 网站上有一些可用的抽屉。但是没有人在做我想做的事。能够请告诉我如何创建这样的内容。

最佳答案

您可以通过有条件地显示抽屉内容自行重新创建此功能。

访问 https://codesandbox.io/s/silly-star-s8oje?file=/src/App.js对于现场演示示例,或者您可以在此处找到代码:

import React from "react";
import clsx from "clsx";
import { makeStyles } from "@material-ui/core/styles";
import Drawer from "@material-ui/core/Drawer";
import Button from "@material-ui/core/Button";
import List from "@material-ui/core/List";
import ListItem from "@material-ui/core/ListItem";
import ListItemText from "@material-ui/core/ListItemText";
import ChevronLeftIcon from "@material-ui/icons/ChevronLeft";
import ChevronRightIcon from "@material-ui/icons/ChevronRight";

const useStyles = makeStyles({
list: {
width: 250
},
fullList: {
width: "auto"
}
});

export default function TemporaryDrawer() {
const classes = useStyles();
const [state, setState] = React.useState({
top: false,
left: false,
bottom: false,
right: false
});

const toggleDrawer = (anchor, open) => event => {
if (
event.type === "keydown" &&
(event.key === "Tab" || event.key === "Shift")
) {
return;
}

setState({ ...state, [anchor]: open });
};

const [menuName, setMenuName] = React.useState(null);

const mainMenuListArr = ["a", "b", "c"];

const subMenuObj = {
a: ["a.1", "a.2", "a.3"],
b: ["b.1", "b.2", "b.3"],
c: ["c.1", "c.2", "c.3"]
};

const list = anchor => {
let arr = menuName ? subMenuObj[menuName] : mainMenuListArr;
const clickListener = text => {
if (!menuName) {
return setMenuName(text);
} else {
return alert(`${text} clicked`);
}
};
return (
<div
className={clsx(classes.list, {
[classes.fullList]: anchor === "top" || anchor === "bottom"
})}
role="presentation"
onKeyDown={toggleDrawer(anchor, false)}
>
<List>
{arr.map((text, index) => (
<ListItem button key={text} onClick={() => clickListener(text)}>
<ListItemText primary={text} />
{!menuName && <ChevronRightIcon />}
</ListItem>
))}
</List>
</div>
);
};

return (
<div>
{["left", "right", "top", "bottom"].map(anchor => (
<React.Fragment key={anchor}>
<Button onClick={toggleDrawer(anchor, true)}>{anchor}</Button>
<Drawer
anchor={anchor}
open={state[anchor]}
onClose={toggleDrawer(anchor, false)}
>
{menuName && (
<ListItem button onClick={() => setMenuName(null)}>
<ListItemText primary="Back to main menu" />
<ChevronLeftIcon />
</ListItem>
)}
{list(anchor)}
</Drawer>
</React.Fragment>
))}
</div>
);
}

关于reactjs - material-ui reactjs中的嵌套抽屉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63087007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com