代码之家  ›  专栏  ›  技术社区  ›  Nnp

在react select中隐藏/删除“新建”菜单

  •  0
  • Nnp  · 技术社区  · 6 年前

    我正在使用可创建选择我想隐藏的“创建新”菜单选项。这是我的 CodeSandbox

    promptTextCreator={() => false}
    Image


    谢谢你,谢谢你的帮助
    1 回复  |  直到 6 年前
        1
  •  3
  •   Liu Lei    6 年前
    // try this way
    return (
          <CreatableSelect
            isClearable
            onChange={this.handleChange}
            onInputChange={this.handleInputChange}
            options={colourOptions}
            noOptionsMessage={() => null}
            // isValidNewOption={() => true}
            // or `isValidNewOption={() => false}`
            promptTextCreator={() => false}
          />
        );
    
        2
  •  1
  •   NearHuscarl    6 年前

    禁用通过创建标签 formatCreateLabel={() => undefined} 是正确的方向,但菜单列表显示空白,而不是完全不显示您可能更喜欢的内容。

    none

      // Remember to define a unique id for your component in the constructor
      // so you can target the right menu list element to hide it
      id = "";
      constructor(props) {
        super(props);
        this.id = "react-select_" + Math.random().toFixed(8).slice(2);
      }
    
      handleInputChange = (inputValue: any, actionMeta: any) => {
        setTimeout(() => {
          const menuEl = document.querySelector(`#${this.id} [class*="-menu"]`);
          const menuListEl = document.querySelector(
            `#${this.id} [class*="MenuList"]`
          );
    
          if (
            menuListEl.children.length === 1 &&
            menuListEl.children[0].innerHTML === ""
          ) {
            menuEl.style.display = "none";
          } else {
            menuEl.style.display = "block";
          }
        });
      };
    
    ...
    
      <CreatableSelect
            id={this.id}
            onInputChange={this.handleInputChange}
            formatCreateLabel={() => undefined}
            ...
          />
    

    现场演示

    Edit crazy-saha-n9dhe

        3
  •  0
  •   frangaren    6 年前

    formatCreateLabel 如下 formatCreateLabel={() => undefined} CreatableSelect .

        4
  •  0
  •   Efriandika Pratama    5 年前

    只需添加以下道具:

    menuIsOpen={false} 和 components={{ DropdownIndicator: null }}

    然后处理 onKeyDown onInputChange 事件,如中所述=> https://react-select.com/creatable

    以下是完整的示例:

    import React, { Component } from 'react';
    
    import CreatableSelect from 'react-select/creatable';
    
    const components = {
      DropdownIndicator: null,
    };
    
    const createOption = (label: string) => ({
      label,
      value: label,
    });
    
    export default class CreatableInputOnly extends Component<*, State> {
      state = {
        inputValue: '',
        value: [],
      };
    
      handleChange = (value: any, actionMeta: any) => {
        this.setState({ value });
      };
    
      handleInputChange = (inputValue: string) => {
        this.setState({ inputValue });
      };
    
      handleKeyDown = (event: SyntheticKeyboardEvent<HTMLElement>) => {
        const { inputValue, value } = this.state;
    
        if (!inputValue) return;
    
        switch (event.key) {
          case 'Enter':
          case 'Tab':
            this.setState({
              inputValue: '',
              value: [...value, createOption(inputValue)],
            });
            event.preventDefault();
        }
      };
    
      render() {
        const { inputValue, value } = this.state;
    
        return (
          <CreatableSelect
            components={components}
            inputValue={inputValue}
            isClearable
            isMulti
            menuIsOpen={false}
            onChange={this.handleChange}
            onInputChange={this.handleInputChange}
            onKeyDown={this.handleKeyDown}
            placeholder="Type something and press enter..."
            value={value}
          />
        );
      }
    }
    
        5
  •  0
  •   Pasindu    5 年前

    加上

     <CreatableSelect
      components={{
        ...components,
        Menu: () => null
      }}
    />
    
    推荐文章