Material UI AutoComplete 组件上的即时搜索

huangapple go评论73阅读模式
英文:

Instant Search on Material UI AutoComplete Component

问题

被迫在使用AutoComplete Material UI组件提交表单时按两次回车。我希望只需选择一次选项就能导航到另一个网页。

提交处理程序

组件代码

我尝试过更改提交处理程序和纸张/框组件,但问题仍然存在。一旦我从AutoComplete的下拉菜单中选择一个选项,用户必须再次按回车。

英文:

Forced to press enter twice on form submission with AutoComplete Material UI component. I want to be able to only select the options once to navigate to another web page.

Submission Handler

Component Code

I attempted to change around the submission handler and the paper/box components, but the problem persists. Once I select an option from the dropdown menu from AutoComplete the user has to hit enter again.

答案1

得分: 1

你在AutoComplete组件上实现了即时搜索,使用了useEffect ReactJS

import React, { useState, useEffect } from 'react';
import { Autocomplete } from '@material-ui/lab';
import { TextField } from '@material-ui/core';

function searchAutocomplete({ option, onSelection }) {
  const [searchTermValue, setSearchTermValue] = useState('');
  const [filteredOptionValue, setFilteredOptionValues] = useState([]);

  useEffect(() => {
    setFilteredOptionValues(option.filter((option) =>
      option.label.toLowerCase().includes(searchTermValue.toLowerCase())
    ));
  }, [searchTermValue, option]);

  return (
    <Autocomplete
      option={filteredOptionValue}
      getOptionLabel={(option) => option.label}
      renderInput={(param) => (
        <TextField
          {...param}
          label="Search"
          variant="outlined"
          onChange={(event) => setSearchTermValue(event.target.value)}
        />
      )}
      onChange={onSelection}
    />
  );
}

希望这段代码对你有帮助。

英文:

You implement instant search on the AutoComplete component, using useEffect ReactJS.

import React, { useState, useEffect } from &#39;react&#39;;
import { Autocomplete } from &#39;@material-ui/lab&#39;;
import { TextField } from &#39;@material-ui/core&#39;;

function searchAutocomplete({ option, onSelection }) {
  const [searchTermValue, setSearchTermValue] = useState(&#39;&#39;);
  const [filteredOptionValue, setFilteredOptionValues] = useState([]);

  useEffect(() =&gt; {
    setFilteredOptionValues(option.filter((option) =&gt;
      option.label.toLowerCase().includes(searchTermValue.toLowerCase())
    ));
  }, [searchTermValue, option]);

  return (
    &lt;Autocomplete
      option={filteredOptionValue}
      getOptionLabel={(option) =&gt; option.label}
      renderInput={(param) =&gt; (
        &lt;TextField
          {...param}
          label=&quot;Search&quot;
          variant=&quot;outlined&quot;
          onChange={(event) =&gt; setSearchTermValue(event.target.value)}
        /&gt;
      )}
      onChange={onSelection}
    /&gt;
  );
}

Hope this code is useful for you.

huangapple
  • 本文由 发表于 2023年2月19日 15:18:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498565.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定