react-select 是否支持搜索中的最大长度?

Does react-select support maxlength in search?

文档中似乎没有提到它,我想将搜索限制为 n 个字符。

我可以通过向输入元素添加 maxlength 属性在 Chrome 开发工具中成功地尝试此操作,但我不确定如何告诉 react-select 组件这样做。

谢谢

我要自己回答。您可以使用 inputProps 将传递给输入元素。

这就是您可以应用 maxLength 的方式

maxLength作为道具传递或在您的文件中定义

<Select
...
onInputChange={inputValue =>
            (inputValue.length <= maxLength ? inputValue : inputValue.substr(0, maxLength))
          }
....
/>

因为 react-select v2 inputProps prop 不再存在。在 react-select v2+ 中你应该使用 Components API。在这种情况下,代码可能如下所示:

import React from "react";
import Select, { components } from "react-select";

const Input = props => <components.Input {...props} maxLength={5} />;

export default () => (
  <Select
    ...
    components={{ Input }}
    ...
  />
);