reactjs PrimeReact自动完成-选择后禁用选项

gfttwv5a  于 2023-01-30  发布在  React
关注(0)|答案(1)|浏览(123)

如何禁用自动完成PrimeReact中选项列表中的选定选项?
例如,选择“阿富汗”选项后,应立即禁用。

代码沙盒:

代码示例:

export const AutoCompleteDemo = () => {
//...

  const searchCountry = (event) => {
    setTimeout(() => {
      let _filteredCountries;
      if (!event.query.trim().length) {
        _filteredCountries = [...countries];
      } else {
        _filteredCountries = countries.filter((country) => {
          return country.name
            .toLowerCase()
            .startsWith(event.query.toLowerCase());
        });
      }

      setFilteredCountries(_filteredCountries);
    }, 250);
  };

  const itemTemplate = (item) => {
    return (
      <div className="country-item">
        <div>{item.name}</div>
      </div>
    );
  };

  return (
    <div className="card">
      <AutoComplete
        value={selectedCountry2}
        suggestions={filteredCountries}
        completeMethod={searchCountry}
        field="name"
        dropdown
        forceSelection
        itemTemplate={itemTemplate}
        onChange={(e) => setSelectedCountry2(e.value)}
        aria-label="Countries"
      />
    </div>
  );
};
ltskdhd1

ltskdhd11#

这已在PrimeReact 9.0.0-beta1中部署,目前在NPM中。
下面是一个有效的代码沙盒:https://codesandbox.io/s/lingering-darkness-vyhp33

const onChange = (e) => {
    e.value.disabled = true;
    setSelectedCountry2(e.value);
};

相关问题