React-Select 支持搜索中的 maxlength 吗?

Pab*_*ote 6 react-select

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

我可以通过向输入元素添加属性来在 Chrome 开发工具中成功尝试此操作maxlength,但我不确定如何告诉反应选择组件执行此操作。

谢谢

pa7*_*ryk 7

由于react-select v2 inputPropsprop不再存在。在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 }}
    ...
  />
);
Run Code Online (Sandbox Code Playgroud)


Lal*_*dav 5

这就是应用 maxLength 的方法

作为 prop传递maxLength或在文件中定义

<Select
...
onInputChange={inputValue =>
            (inputValue.length <= maxLength ? inputValue : inputValue.substr(0, maxLength))
          }
....
/>
Run Code Online (Sandbox Code Playgroud)


Pab*_*ote 0

我自己来回答吧 您可以使用inputProps它将传递到输入元素。