React-Select v2 逗号分隔搜索

React-Select v2 comma separated search

react-select v2 有没有办法一次搜索多个值?就像我可以让我的用户粘贴到逗号列表或 space 分隔值和匹配这些结果的项目将显示在下拉列表中。

示例反应-select(我们称之为项目选择器):

<StyledFormItemPicker
          className="emailPicker"
          filterKeys={['label']}
          label="email picker"
          value={emailPickerValue}
          onChange={value => onEmailPickerChange(value)}
          items={users}
          isMulti={true}
        />

on更改代码:

// 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?
};

因此,为了实现您的功能,我会使用道具 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;
};

想法是用 space 或逗号分隔输入值(在上面的示例中,我结合了这两个选项)并应用 react-select 正在执行的常规过滤每个实例。

可用的实例 here