简体   繁体   English

在 Material UI 菜单组件中禁用“通过键入选择”

[英]Disable "Select By Typing" in Material UI Menu Component

I'm trying to make a pop-up menu that has a couple different filters (buttons, selects, and text fields).我正在尝试制作一个弹出菜单,其中包含几个不同的过滤器(按钮、选择和文本字段)。 I'm using the Material UI Menu component, but have run into an issue when trying to use the text fields.我正在使用 Material UI Menu 组件,但在尝试使用文本字段时遇到了问题。 Because the Menu component is acting like a <Select /> , when I type something in the text fields, it tries to select different MenuItems instead of staying focused on the text box.因为 Menu 组件的行为类似于<Select /> ,所以当我在文本字段中键入内容时,它会尝试 select 不同的 MenuItems 而不是专注于文本框。

So using the example found in the sandbox below, users wouldn't be able to type anything in the "search for a different food" textbox if it started with an "A", "B", or "C".因此,使用下面沙箱中的示例,如果“搜索不同的食物”文本框以“A”、“B”或“C”开头,用户将无法在该文本框中输入任何内容。 If you wanted to type in "apricots", the menu would change focus from the textbox to the "Apples" MenuItem.如果您想输入“杏子”,菜单会将焦点从文本框更改为“Apples”菜单项。

I don't see any props for this in the Menu API, so I'm curious to know if there is any work arounds, or even a different component that is more suited for this.我在菜单 API 中没有看到任何关于此的道具,所以我很想知道是否有任何解决方法,甚至是更适合此的不同组件。

Thanks!谢谢!

Here's a codesandbox: https://codesandbox.io/s/wizardly-mccarthy-zy2b7这是一个代码框: https://codesandbox.io/s/wizardly-mccarthy-zy2b7

import { useState } from "react";
import "./styles.css";
import { Button, Menu, MenuItem, TextField } from "@material-ui/core";

export default function App() {
  const [menu, setMenu] = useState(null);
  const [text, setText] = useState("");

  return (
    <div className="App">
      <Button variant="outlined" onClick={(e) => setMenu(e.currentTarget)}>
        Filters
      </Button>

      <Menu
        anchorEl={menu}
        open={Boolean(menu)}
        getContentAnchorEl={null}
        anchorOrigin={{
          vertical: "bottom",
          horizontal: "left"
        }}
        onClose={() => setMenu(null)}
      >
        <MenuItem>Apples</MenuItem>
        <MenuItem>Bananas</MenuItem>
        <MenuItem>Carrots</MenuItem>
        <MenuItem>
          <TextField
            value={text}
            onChange={(e) => setText(e.target.value)}
            variant={"outlined"}
            label={"search a different food..."}
          />
        </MenuItem>
      </Menu>
    </div>
  );
}

Use e.stopPropagation() in the onKeyDown event for the MenuItem containing the TextField.在包含 TextField 的 MenuItem 的 onKeyDown 事件中使用 e.stopPropagation()。 This will prevent the key down event from propagating to the Menu component.这将防止按键事件传播到 Menu 组件。

    <MenuItem onKeyDown={(e) => e.stopPropagation()}>
        <TextField
            value={text}
            onChange={(e) => setText(e.target.value)}
            variant={"outlined"}
            label={"search a different food..."}
        />
    </MenuItem>

Reference: How to disable the selection of an item when the first letter of the option is pressed in the Select component?参考: Select组件中按选项首字母时如何禁用选择项?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM