有没有办法拥有 select 和可创建的混合体?

Is there a way to have a select and creatable hybrid?

我已经使用来自 react-select 的 select 有一段时间了。到目前为止,只有预定义的选项,没有任何添加任何其他功能的功能 option/s.

据我所知,还有一个叫做 creatable 的东西就是这样做的。但据我所知,它不允许我列出 predefined/static 列表。

我假设两者都必须实现才能获得任一功能,或者是否有方法或支持只坚持其中一个?

编辑: 也许我没有足够强调静态部分。基本上,我想要一个组件,它有一个像这样的道具:“canAddOptions: bool”。要么允许添加值,要么不允许。据我所知,我必须同时实现两者才能得到这个..?

我也在我的应用程序中使用类似的方法,我将静态列表呈现为选项,它应该作为 creatable select.

根据问题陈述,您可以使用 isValidNewOption={() => false} 使 creatable 正常 select。

尝试如下操作:

import CreatableSelect from 'react-select/creatable';
 
 <CreatableSelect
    isMulti
    isValidNewOption={() => false} // You can apply condition here as per the prop you will get.
    options={[
      { value: 'one', label: 'One' },
      { value: 'two', label: 'Two' },
      { value: 'three', label: 'Three' },
    ]}
  />