car*_*rke 5 javascript multi-select reactjs react-select
react-select v2是否有办法一次搜索多个值?就像我可以让用户粘贴在逗号或空格分隔的值列表中,并且与这些结果匹配的项目将显示在下拉列表中。
示例react-select(我们称为项目选择器):
<StyledFormItemPicker
className="emailPicker"
filterKeys={['label']}
label="email picker"
value={emailPickerValue}
onChange={value => onEmailPickerChange(value)}
items={users}
isMulti={true}
/>
Run Code Online (Sandbox Code Playgroud)
onChange代码:
// allow user to pass in comma separated list to search
export const onEmailPickerChange = props => event => {
event.persist();
// split event (value) on space or comma
// push to an array
// set that array of strings as the value and see all results?
};
Run Code Online (Sandbox Code Playgroud)
因此,为了实现您的功能,我将使用 props filterOption。
const filterOption = ({ label, value }, string) => {
if(string === "") return true;
const parsedString = string.split(/[, ]+/);
for (const string of parsedString) {
// Need to check of string is not empty after the split
if (string !== "" && (label.includes(string) || value.includes(string)))
return true;
}
return false;
};
Run Code Online (Sandbox Code Playgroud)
这个想法是用空格或逗号分割输入值(在上面的示例中,我组合了这两个选项)并react-select在每个实例上应用常规过滤。
此处提供了实例。