reactjs - 当没有选项与用户搜索值匹配时 React-select 默认值
问题描述
我有一个用于 react-select 的数组,如下所示
[{label:"blue",value:"blue"},{label:"green",value:"green"},{label:"other",value:"other"}]
当用户尝试搜索蓝色和绿色以外的其他选项(如棕色、紫色)时,选项列表中应显示其他选项,因此用户可以选择“其他”
有没有办法在 react-select 中实现这个功能
解决方案
我认为您正在寻找的内容与这篇文章很接近。
基本上使用这样的filterOption
道具:
filterOption = (option, inputValue) => {
if (option.label === "Other") {
const { options } = this.state;
const result = options.filter(opt => opt.label.includes(inputValue));
this.setState({ hasExtraValue: !result.length });
return !result.length;
}
return option.label.includes(inputValue);
};
这是一个符合您要求的实时示例。
推荐阅读
- javascript - 如何在角度 6 的单个图像中使用多个裁剪
- r - 围绕值的日期的 groupby 问题:二进制运算符的非数字参数
- angular - 是否不可能在订阅中捕获错误?
- linux - 当接收到带有空格和双引号的字符串时,如何正确解析在 Linux 上运行的 Jenkins 中的构建参数?
- batch-file - 检查已安装程序是 64 位还是 32 位的批处理文件?
- mongodb - MongoDB mergeObjects 求和而不是替换
- java - 如何在java中的通用列表中添加两个相同类型的项目列表
- postgresql - Spring boot jpa - 数据库中的实体如何更新?
- java - 画布不出现
- python - 如何修复关闭文件错误的 I/O 操作?