如何禁用自动完成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>
);
};
1条答案
按热度按时间ltskdhd11#
这已在PrimeReact 9.0.0-beta1中部署,目前在NPM中。
下面是一个有效的代码沙盒:https://codesandbox.io/s/lingering-darkness-vyhp33