【问题标题】:Dropdownlist alternative in material UI of react反应的材料 UI 中的下拉列表替代方案
【发布时间】:2021-02-27 07:19:33
【问题描述】:

我目前正在使用simple-menu 的材料 UI,如codesandbox here 所示。在我的 react 应用程序中,我在 primereact 对话框中显示此菜单。

但是,我的主要要求是在对话框中显示一个下拉列表,以便当用户选择某些内容时,可以选择它并且我可以根据它做一些事情。 例如,如果用户选择Car,我可以看到它被选中。另外,我有一些与每个条目相关的值。 例如,Company 的值为 0Car 的值为 1Office 的值为 2

所以我计划在对话框中添加一些按钮,以便在选择列表中的项目并选择 Ok 按钮后,我可以在 web 服务调用中发送与所选项目关联的值。

我能找到的最接近的是 simplelist:

https://material-ui.com/components/lists/#simple-list

但没有什么接近下拉列表。我在这里错过了什么吗?

更多信息:

对话框如下所示:

假设当我选择一个项目时,假设是Car,它不会显示Car,但会显示我不想要的Open Menu

【问题讨论】:

  • 是的,首先使用material-ui制作模态很容易,然后将simplelist作为模态的内容
  • 我添加了我拥有的对话框的屏幕截图,它显示了Open Menu。但它没有保留我上面在帖子中提到的所选项目。所以你是说simplelist 不会产生这个问题?另外,简单列表不会以下拉方式显示,对吧?
  • 你想要什么,选择选项后对话框应该打开并且应该保留选择的选项?如果这是你想要的,我会发布代码。
  • 并非如此。当用户单击 UI 中的某个按钮时,我已经有一个对话框。在里面我有Open Menu 按钮,如代码框(codesandbox.io/s/material-demo-forked-h70e7?file=/demo.tsx)中所述。因此,当我选择某些内容时,下拉菜单会关闭并且不显示所选项目。让我知道我是否可以回答更多问题。谢谢 !或者您可以发布您的代码,然后我可以让您知道是否有一些混淆。
  • 我的解决方案对你有用吗

标签: reactjs material-ui


【解决方案1】:

What I have achieved is when the dropdown is opened and some option is selected then users can see what they have selected and dropdown doesn't close when some option is selected and in the console you can see what option is selected by index号码。

您可以按照这个代码

import React, { useState } from "react";
import Button from "@material-ui/core/Button";
import Menu from "@material-ui/core/Menu";
import MenuItem from "@material-ui/core/MenuItem";
import { makeStyles } from "@material-ui/core/styles";

const useStyles = makeStyles((theme) => ({
  root: {
    width: "100%",
    maxWidth: 360,
    backgroundColor: theme.palette.background.paper
  }
}));

export default function SimpleMenu() {
  const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);
  const [visval, setVisval] = useState("Open Menu");

  const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
    setAnchorEl(event.currentTarget);
    //console.log(event.currentTarget);
  };

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

  const classes = useStyles();
  const [selectedIndex, setSelectedIndex] = React.useState();

  const handleListItemClick = (event, index) => {
    setSelectedIndex(index);
    handleClose();
    //console.log(index);
    if (index === 0) {
      setVisval("Company");
    } else if (index === 1) {
      setVisval("Car");
    } else {
      setVisval("Office");
    }
  };

  return (
    <div>
      <Button
        aria-controls="simple-menu"
        aria-haspopup="true"
        onClick={handleClick}
      >
        {visval}
      </Button>
      <Menu
        id="simple-menu"
        anchorEl={anchorEl}
        keepMounted
        open={Boolean(anchorEl)}
        onClose={handleClose}
      >
        <MenuItem
          data-my-value={1}
          selected={selectedIndex === 0}
          onClick={(event) => handleListItemClick(event, 0)}
        >
          Company
        </MenuItem>
        <MenuItem
          data-my-value={2}
          selected={selectedIndex === 1}
          onClick={(event) => handleListItemClick(event, 1)}
        >
          Car
        </MenuItem>
        <MenuItem
          data-my-value={3}
          selected={selectedIndex === 2}
          onClick={(event) => handleListItemClick(event, 2)}
        >
          Office
        </MenuItem>
      </Menu>
    </div>
  );
}

您可以查看更新后的工作codesandbox here

编辑:

我已经实现了您的要求,即当我们选择某个选项时,显示三个选项的菜单应该关闭,并且应该在 Open Menu 的位置看到相同的选项。但我看到的是代码不再可重用并且变得太大(我意识到这一点)。

如果您希望代码最小化,请遵循以下建议

建议: 我看到 Material-ui 的 autocomplete 只是用干净的代码完成了工作。 它为您提供了一组选项,例如 dropdown menu,当用户选择某项时,用户会看到所选选项。

Link to the Working Demo

您可以将相同的代码迁移到对话框的内容中,它会正常工作。

【讨论】:

  • 所以当我选择某些东西时,它不会像你提到的那样自行关闭。是否可以使用选择为显示而不是打开菜单的选项来关闭它?
  • 是的,可能我现在有点忙,所以我将使用更新的代码编辑答案。
  • @Tan 我已经编辑了我的答案(代码,链接),但我看到的是代码不再可重用,所以我建议你遵循我在答案中给出的建议代码工作正常。
猜你喜欢
  • 2020-06-04
  • 2019-10-06
  • 2021-11-04
  • 1970-01-01
  • 2023-01-24
  • 2020-10-14
  • 1970-01-01
  • 2022-06-30
  • 1970-01-01
相关资源
最近更新 更多