首页 > 解决方案 > react-select:禁用下拉列表

问题描述

有没有办法禁用下拉列表?我没有找到任何可以帮助我的道具。

特别是,当用户选择超过 5 个元素时,我想禁用下拉列表。

我创建了这个代码框。它并没有真正起作用,因为它没有与状态相关联:

const limit = 3;
const defaults = [colourOptions[2], colourOptions[3]];

export default () => (
  <Select
    defaultValue={defaults}
    isMulti
    name="colors"
    options={colourOptions}
    className="basic-multi-select"
    classNamePrefix="select"
    isSearchable={defaults.length < limit}
    // disableDropdown={defaults.length > limit} // <-- something like this
  />
)

标签: reactjsreact-select

解决方案


您可以通过删除组件来“禁用/删除”下拉列表;

components={{
    Menu: () => null,               // Remove menu
    MenuList: () => null,           // Remove menu list
    DropdownIndicator: () => null,  // Remove dropdown icon
    IndicatorSeparator: () => null  // Remove separator
}}
<Select
    defaultValue={defaults}
    isMulti
    name="colors"
    options={colourOptions}
    className="basic-multi-select"
    classNamePrefix="select"
    isSearchable={defaults.length < limit}
    components={{
        Menu: () => null,
        MenuList: () => null,
        DropdownIndicator: () => null,
        IndicatorSeparator: () => null
    }}
/>

更新的代码沙箱

文档


推荐阅读