gpt4 book ai didi

reactjs - 在 material-ui React 中处理多个 MenuItem

转载 作者:行者123 更新时间:2023-12-05 04:57:28 25 4
gpt4 key购买 nike

enter image description here请帮我处理这个多个菜单处理事件。我在 json 中有动态菜单及其相应的菜单项。我想显示在该特定父菜单下声明的菜单项,而不是在单击单个菜单按钮时重叠并显示所有父菜单项。这是我的 Nvbar.js

import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import Toolbar from '@material-ui/core/Toolbar';
import Button from '@material-ui/core/Button';
import { InputData } from '../InputJSON';
import Menu from '@material-ui/core/Menu';
import MenuItem from '@material-ui/core/MenuItem';


const useStyles = makeStyles((theme) => ({
root: {
flexGrow: 1,
},
navButton: {
margin: 'auto',
},
title: {
flexGrow: 1,
},
toolbar:{
backgroundColor: "orange",
},
}));



export default function ButtonAppBar() {
const classes = useStyles();
const [anchorEl, setAnchorEl] = React.useState(null);

const handleClick = (event) => {
setAnchorEl(event.currentTarget);
};

const handleClose = () => {
setAnchorEl(null);
};

return (
<div className={classes.root}>
<Toolbar className={classes.toolbar}>
{Object.keys(InputData).map((item, index) => (
<div
className={classes.navButton}
key={index}
>
<Button
color="inherit"
onClick={handleClick}
>
{item} <i className='fas fa-caret-down' />
</Button>
<Menu
anchorEl={anchorEl}
keepMounted
open={Boolean(anchorEl)}
onClose={handleClose}
getContentAnchorEl={null}
anchorOrigin={{vertical: 'bottom', horizontal: 'center'}}
transformOrigin={{vertical: 'top', horizontal: 'center'}}
>
{InputData[item].map((menuitems, menuindex) =>
(
<MenuItem
key={menuindex}
selected={menuitems === item}
onClick={handleClose}
>
{menuitems.title}
</MenuItem>

)
)}
</Menu>

</div>
))
}
</Toolbar>
</div>
);
}

输入数据.js

export const MenuItems = 
{"furniture":
[
{
title: 'Marketing',
path: '/marketing',
cName: 'dropdown-link'
},
{
title: 'Consulting',
path: '/consulting',
cName: 'dropdown-link'
},
{
title: 'Design',
path: '/design',
cName: 'dropdown-link'
},
{
title: 'Development',
path: '/development',
cName: 'dropdown-link'
}
],
"mobiles":
[
{
title: 'iphone',
path: '/marketing',
cName: 'dropdown-link'
},
{
title: 'samsung',
path: '/consulting',
cName: 'dropdown-link'
},
{
title: 'oneplus',
path: '/design',
cName: 'dropdown-link'
},
{
title: 'sony',
path: '/development',
cName: 'dropdown-link'
}
],
"laptops":
[
{
title: 'iphone',
path: '/marketing',
cName: 'dropdown-link'
},
{
title: 'samsung',
path: '/consulting',
cName: 'dropdown-link'
},
{
title: 'oneplus',
path: '/design',
cName: 'dropdown-link'
},
{
title: 'sony',
path: '/development',
cName: 'dropdown-link'
}
],
"aircon":
[
{
title: 'iphone',
path: '/marketing',
cName: 'dropdown-link'
},
{
title: 'samsung',
path: '/consulting',
cName: 'dropdown-link'
},
{
title: 'oneplus',
path: '/design',
cName: 'dropdown-link'
},
{
title: 'sony',
path: '/development',
cName: 'dropdown-link'
}
],
"kitapp":
[
{
title: 'iphone',
path: '/marketing',
cName: 'dropdown-link'
},
{
title: 'samsung',
path: '/consulting',
cName: 'dropdown-link'
},
{
title: 'oneplus',
path: '/design',
cName: 'dropdown-link'
},
{
title: 'sony123',
path: '/development',
cName: 'dropdown-link'
}
],

};

最佳答案

您需要根据单击的菜单项分别跟踪每个 anchor 元素。您可以通过根据项目键设置元素来做到这一点:

export default function ButtonAppBar() {
const classes = useStyles();
const [anchorEl, setAnchorEl] = React.useState(null);

// Instead of tracking a single element, set the element according to
// the menu item's index.
const handleClick = (index, event) => {
setAnchorEl({ [index]: event.currentTarget });
};

const handleClose = () => {
setAnchorEl(null);
};

return (
<div className={classes.root}>
<Toolbar className={classes.toolbar}>
{Object.keys(InputData).map((item, index) => (
<div className={classes.navButton} key={index}>
<Button color="inherit" onClick={(e) => handleClick(index, e)}>
{item} <i className="fas fa-caret-down" />
</Button>
<Menu
anchorEl={
// Check to see if the anchor is set.
anchorEl && anchorEl[index]
}
keepMounted
open={
// Likewise, check here to see if the anchor is set.
Boolean(anchorEl && anchorEl[index])
}
onClose={handleClose}
getContentAnchorEl={null}
anchorOrigin={{ vertical: "bottom", horizontal: "center" }}
transformOrigin={{ vertical: "top", horizontal: "center" }}
>
{menuItems[item].map((menuitems, menuindex) => (
<MenuItem
key={menuindex}
selected={menuitems === item}
onClick={handleClose}
>
{menuitems.title}
</MenuItem>
))}
</Menu>
</div>
))}
</Toolbar>
</div>
);
}

工作沙箱:https://codesandbox.io/s/icy-haze-h0594

关于reactjs - 在 material-ui React 中处理多个 MenuItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64405880/

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