【问题标题】:React-Select Creatable hide only create new.. messageReact-Select Creatable hide only create new.. 消息
【发布时间】:2018-08-06 11:34:36
【问题描述】:

我可以在使用 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

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

希望这对现在有帮助。

【问题讨论】:

  • 您能否在问题中包含您目前编写的代码?
  • 你有代码吗
  • 只是为了综合你想要的,如果我理解得很好,你想要Add: 选项没有用户输入的单词?

标签: javascript reactjs react-select


【解决方案1】:

如果你想替换 Add: myValue 选项,你可以使用 formatCreateLabel 中的 formatCreateLabel 属性,如下所示:

formatCreateLabel={() => `Add`}

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

【讨论】:

  • 谢谢@Laura 我今天看到了你的评论。但是我已经使用您提到的方法来实现我的结果。感谢您的回答,也许它会帮助其他有同样问题的人。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-07-14
  • 2017-10-08
  • 2022-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-28
相关资源
最近更新 更多