javascript - React-Select Creatable hide only create new.. 消息

标签 javascript reactjs react-select

我可以在使用 React-Select Creatable 时隐藏下拉菜单中的仅创建新选项吗?我想像往常一样展示其他建议,并希望保留创建新标签的可创建功能。只是不想显示用户在下拉菜单中输入的内容。

编辑:为 React-Select 添加示例代码:

import React, { Component } from 'react';

import CreatableSelect from 'react-select/lib/Creatable';
const colourOptions = [
  { value: 'chocolate', label: 'Chocolate' },
  { value: 'strawberry', label: 'Strawberry' },
  { value: 'vanilla', label: 'Vanilla' }
]


export default class CreatableMulti extends Component<*, State> {
  handleChange = (newValue: any, actionMeta: any) => {
    console.group('Value Changed');
    console.log(newValue);
    console.log(`action: ${actionMeta.action}`);
    console.groupEnd();
  };

  formatCreate = (inputValue) => {
    return (<p> Add: {inputValue}</p>); 
  };

  render() {
    return (
      <CreatableSelect
        isMulti
        onChange={this.handleChange}
        options={colourOptions}
        formatCreateLabel={this.formatCreate}
        createOptionPosition={"first"}
      />
    );
  }
}

这里还有一个沙箱链接:https://codesandbox.io/s/wqm0z1wlxl

当您键入“我想要”时,它会显示“添加:”以及所有其他已过滤的建议。我想从中删除添加部分以保留其余功能。

希望这对现在有所帮助。

最佳答案

如果您想替换选项 Add: myValue,您可以在 CreatableSelect 中使用 Prop formatCreateLabel,如下所示:

formatCreateLabel={() => `Add`}

Here a live example将前面的代码应用于您给出的示例。 可以查阅文档权here了解这个特定 Prop 的行为。

关于javascript - React-Select Creatable hide only create new.. 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51706702/

相关文章:

javascript - 在 Meteor 中使用 React Layout 将 Prop 传递给曾孙组件

javascript - 如何在 Vert.x3 中编写自定义处理程序?

javascript - 取消订阅 Pusher channel

javascript - 从一个 AngularJS 应用程序切换到另一个——页面内容未更新

reactjs - 如何在 react 中顺序调用函数?

reactjs - 如何在react-countdown中调用onStart函数?

javascript - 反跳 react 选择 graphql 查询

javascript - react-select 中的 valueComponent 和 valueRenderer 有什么区别?

reactjs - 在react-select 2中显示带有选项的图像

javascript - 封闭类与经典类