【问题标题】:Material UI sub drawer from main drawer主抽屉的材质 UI 子抽屉
【发布时间】:2020-09-16 03:01:54
【问题描述】:

我正在努力寻找从 Material UI 扩展主抽屉的“子”抽屉示例。 https://material-ui.com/components/drawers/#drawer

我发现了这个将抽屉放在 div(不是页面)中的示例,我认为可以对其进行调整:Is there a way to show a Material-UI Drawer nested inside a Grid component?

预期的行为是主抽屉上可以打开子抽屉的按钮或事件。这是一个截图示例:

【问题讨论】:

    标签: reactjs material-ui


    【解决方案1】:

    我已经为材质 ui 中的嵌套抽屉准备了一个示例。在此示例中,您可以打开左侧的抽屉。打开后,您会找到一个按钮Show Nested Drawer,如果您点击该按钮,您将获得另一个抽屉。

    import React from 'react';
    import clsx from 'clsx';
    import {makeStyles, useTheme} from '@material-ui/core/styles';
    import Drawer from '@material-ui/core/Drawer';
    import CssBaseline from '@material-ui/core/CssBaseline';
    import AppBar from '@material-ui/core/AppBar';
    import Toolbar from '@material-ui/core/Toolbar';
    import List from '@material-ui/core/List';
    import Typography from '@material-ui/core/Typography';
    import Divider from '@material-ui/core/Divider';
    import IconButton from '@material-ui/core/IconButton';
    import MenuIcon from '@material-ui/icons/Menu';
    import ChevronLeftIcon from '@material-ui/icons/ChevronLeft';
    import ChevronRightIcon from '@material-ui/icons/ChevronRight';
    import ListItem from '@material-ui/core/ListItem';
    import ListItemIcon from '@material-ui/core/ListItemIcon';
    import ListItemText from '@material-ui/core/ListItemText';
    import InboxIcon from '@material-ui/icons/MoveToInbox';
    import MailIcon from '@material-ui/icons/Mail';
    import Button from "@material-ui/core/Button";
    
    const drawerWidth = 240;
    
    const useStyles = makeStyles((theme) => ({
        root: {
            display: 'flex',
        },
        appBar: {
            transition: theme.transitions.create(['margin', 'width'], {
                easing: theme.transitions.easing.sharp,
                duration: theme.transitions.duration.leavingScreen,
            }),
        },
        appBarShift: {
            width: `calc(100% - ${drawerWidth}px)`,
            marginLeft: drawerWidth,
            transition: theme.transitions.create(['margin', 'width'], {
                easing: theme.transitions.easing.easeOut,
                duration: theme.transitions.duration.enteringScreen,
            }),
        },
        menuButton: {
            marginRight: theme.spacing(2),
        },
        hide: {
            display: 'none',
        },
        drawer: {
            width: drawerWidth,
            flexShrink: 0,
        },
        drawerPaper: {
            width: drawerWidth,
        },
        drawerHeader: {
            display: 'flex',
            alignItems: 'center',
            padding: theme.spacing(0, 1),
            // necessary for content to be below app bar
            ...theme.mixins.toolbar,
            justifyContent: 'flex-end',
        },
        content: {
            flexGrow: 1,
            padding: theme.spacing(3),
            transition: theme.transitions.create('margin', {
                easing: theme.transitions.easing.sharp,
                duration: theme.transitions.duration.leavingScreen,
            }),
            marginLeft: -drawerWidth,
        },
        contentShift: {
            transition: theme.transitions.create('margin', {
                easing: theme.transitions.easing.easeOut,
                duration: theme.transitions.duration.enteringScreen,
            }),
            marginLeft: 0,
        },
    }));
    
    export default function NestedDrawerExample() {
        const classes = useStyles();
        const theme = useTheme();
        const [drawerOpen, setDrawerOpen] = React.useState(false);
        const [subDrawerOpen, setSubDrawerOpen] = React.useState(false);
    
        const handleDrawerOpen = () => {
            setDrawerOpen(true);
        };
    
        const handleDrawerClose = () => {
            setDrawerOpen(false);
        };
    
        return (
            <div className={classes.root}>
                <CssBaseline/>
                <AppBar
                    position="fixed"
                    className={clsx(classes.appBar, {
                        [classes.appBarShift]: drawerOpen,
                    })}
                >
                    <Toolbar>
                        <IconButton
                            color="inherit"
                            aria-label="open drawer"
                            onClick={handleDrawerOpen}
                            edge="start"
                            className={clsx(classes.menuButton, drawerOpen && classes.hide)}
                        >
                            <MenuIcon/>
                        </IconButton>
                        <Typography variant="h6" noWrap>
                            Nested Drawer Example
                        </Typography>
                    </Toolbar>
                </AppBar>
                <Drawer
                    className={classes.drawer}
                    variant="persistent"
                    anchor="left"
                    open={drawerOpen}
                    classes={{
                        paper: classes.drawerPaper,
                    }}
                >
                    <div className={classes.drawerHeader}>
                        <IconButton onClick={handleDrawerClose}>
                            {theme.direction === 'ltr' ? <ChevronLeftIcon/> : <ChevronRightIcon/>}
                        </IconButton>
                    </div>
                    <Divider/>
                    <List>
                        {['Inbox', 'Starred', 'Send email', 'Drafts'].map((text, index) => (
                            <ListItem button key={text}>
                                <ListItemIcon>{index % 2 === 0 ? <InboxIcon/> : <MailIcon/>}</ListItemIcon>
                                <ListItemText primary={text}/>
                            </ListItem>
                        ))}
                    </List>
                    <Divider/>
                    <Button onClick={() => {
                        setSubDrawerOpen(true)
                    }}>Open Nested Drawer</Button>
                    <Drawer
                        className={classes.drawer}
                        variant="persistent"
                        anchor="left"
                        open={subDrawerOpen}
                        classes={{
                            paper: classes.drawerPaper,
                        }}
                    >
                        <div className={classes.drawerHeader}>
                            <IconButton onClick={()=>{setSubDrawerOpen(false) }}>
                                {theme.direction === 'ltr' ? <ChevronLeftIcon/> : <ChevronRightIcon/>}
                            </IconButton>
                        </div>
                        <Divider/>
                        <List>
                            {['All mail', 'Trash', 'Spam'].map((text, index) => (
                                <ListItem button key={text}>
                                    <ListItemIcon>{index % 2 === 0 ? <InboxIcon/> : <MailIcon/>}</ListItemIcon>
                                    <ListItemText primary={text}/>
                                </ListItem>
                            ))}
                        </List>
                    </Drawer>
                </Drawer>
                <main
                    className={clsx(classes.content, {
                        [classes.contentShift]: drawerOpen,
                    })}
                >
                    <div className={classes.drawerHeader}/>
                    <Typography paragraph>
                        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt
                        ut labore et dolore magna aliqua.
                    </Typography>
                    <Typography paragraph>
                        Consequat mauris nunc congue nisi vitae suscipit. Fringilla est ullamcorper eget nulla
                        facilisi etiam dignissim diam. Pulvinar elementum integer enim neque volutpat ac
                        tincidunt.
                    </Typography>
                </main>
            </div>
        );
    }
    

    【讨论】:

    • 感谢@Khabir,在您的示例中,嵌套抽屉滑到第一个抽屉的顶部。这不是我要找的。我正在寻找在第一个抽屉末端打开的第二个抽屉,以便它们并排放置。
    • Material ui 抽屉在左/右/上/下侧打开。恐怕material-ui 支持或不支持您的要求
    • 在这个例子中:stackblitz.com/edit/…(来自:stackoverflow.com/questions/57749947/…)——他们设法从一个 div 中打开了抽屉,同样的概念也适用于另一个抽屉的边缘。这应该是可能的?
    • 该示例没有并排放置 2 个抽屉
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 2018-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多