javascript - Material UI - 动态渲染/打开菜单

标签 javascript reactjs material-ui

我正在尝试为每个 JSX 按钮元素渲染一个 Material UI 菜单。

现在我的所有菜单项都相互堆叠。我只能显示菜单 与已单击的按钮相关。

See it in Action

export default function App() {
  const [anchorEl, setAnchorEl] = useState(null);
  const menuOptions = [
    { id: 0, name: "Remove", method: "remove" },
    { id: 1, name: "Duplicate", method: "duplicate" },
    { id: 2, name: "New", method: "addNew" }
  ];

  const [addressInputFields, setAddressInputFields] = useState([
    { firstName: "", lastName: "" },
    { firstName: "", lastName: "" },
    { firstName: "", lastName: "" },
    { firstName: "", lastName: "" }
  ]);

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

  const handleMenuClose = (event) => {
    setAnchorEl(null);
  };

  return (
    <div>
      {addressInputFields.map((input, index) => (
        <span key={index}>
          <button type="button" name="address" onClick={handleMenu}>
            ShowMenu
          </button>
          <Menu
            id="option-menu"
            anchorEl={anchorEl}
            open={Boolean(anchorEl)}
            onClose={handleMenuClose}
            anchorOrigin={{
              vertical: "bottom",
              horizontal: "left"
            }}
            getContentAnchorEl={null}
          >
            <span>Menu Index: {index}</span>
            {menuOptions.map((option) => (
              <MenuItem key={option.id}>{option.name}</MenuItem>
            ))}
          </Menu>
        </span>
      ))}
    </div>
  );
}

最佳答案

您可以通过其index跟踪当前打开的Menu,然后仅在open属性设置为true的情况下渲染菜单 基于 Boolean(anchorEl) 和开放索引,如下所示:

const [anchorEl, setAnchorEl] = useState(null);

// track with menu should be opened
const [openIndex, setOpenIndex] = useState(-1);

const handleMenu = (index) => (event) => {
  setAnchorEl(event.currentTarget);
  setOpenIndex(index); // set current menu index to open
};

const handleMenuClose = (event) => {
  setAnchorEl(null);
};

return (
  <div>
    {addressInputFields.map((input, index) => (
      <span key={index}>
        <button
          name="address"
          // handleMenu now need the index context to know which menu to open
          onClick={handleMenu(index)}
        >
          ShowMenu
        </button>
        <Menu
          // only render currently open menu
          open={Boolean(anchorEl) && index === openIndex}
          onClose={handleMenuClose}
          {...props}
        >
          <span>Menu Index: {index}</span>
          {menuOptions.map((option) => (
            <MenuItem key={option.id}>{option.name}</MenuItem>
          ))}
        </Menu>
      </span>
    ))}
  </div>
);

现场演示

Edit 67211084/material-ui-render-open-menu-dynamically

关于javascript - Material UI - 动态渲染/打开菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67211084/

相关文章:

javascript - 无法从 React Native 中的不同文件夹导入图像

reactjs - React Hook Form onChange 选择 Materialui

html - 如何在 Bootstrap 4 中创建自定义按钮网格?

c# - 在 MVC Controller 中将 Javascript 时间字符串转换为 DateTime 或 TimeSpan

javascript - 值未传递给指令 - AngularJS

javascript - Chrome : format specifier prints NaN instead of number

reactjs - Material UI TablePagination 句柄与 Redux/React

javascript - CSS3动画如何移动到某个关键帧?

javascript - 单击更改图像 - React

reactjs - 如何使用展开运算符来更新 React 中的状态值?