MUI Core-使用自动完成组件作为搜索框

发布于 2025-02-03 18:20:09 字数 790 浏览 3 评论 0原文

我正在尝试使用Material-UI(MUI)自动完成组件,但更像是搜索栏。因此,当您单击搜索区域/文本字段时,我正在尝试禁用值的弹出窗口。我尝试使用onkeydown事件,但这只是键盘,我尝试了免费的solo,但是它一直打开下拉列表。有什么想法吗?

示例代码IVE在下面尝试过。

      return ( 
            <Autocomplete
            openOnFocus={false}
            disablePortal={true}
            id="combo-box-demo"
            freeSolo
            options={labels}
            sx={{ width: 300 }}
            onMouseClick={(event) => {
                console.log(event.key);
                if (event.key === 'Click') {
                  // Prevent's default 'Enter' behavior.
                  event.defaultMuiPrevented = true;
                
                }
            }}
            renderInput={(params) => <TextField {...params} label="SKU" />}
          />

I'm trying to use Material-UI (MUI) Autocomplete component but more like a search bar. So i am trying to disable the popup of values when you click in the search area/text field. I have tried using the onkeyDown event but this is just keyboard, and i've tried free-solo but it keeps opening the dropdown list. Any ideas chaps?

Sample code ive tried below.

      return ( 
            <Autocomplete
            openOnFocus={false}
            disablePortal={true}
            id="combo-box-demo"
            freeSolo
            options={labels}
            sx={{ width: 300 }}
            onMouseClick={(event) => {
                console.log(event.key);
                if (event.key === 'Click') {
                  // Prevent's default 'Enter' behavior.
                  event.defaultMuiPrevented = true;
                
                }
            }}
            renderInput={(params) => <TextField {...params} label="SKU" />}
          />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情痴 2025-02-10 18:20:09

为了实现所需的行为,您可以使用open prop并设置条件,例如:

    <Autocomplete
      open={true && searchInputValue.length>0}
      ...
    />

where searchInputValue将是负责搜索输入的国家的价值。

To achieve the desired behavior you can use the open prop and set condition to it like:

    <Autocomplete
      open={true && searchInputValue.length>0}
      ...
    />

where searchInputValue will be the value of the state responsible for the search input.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文