简体   繁体   English

如何禁用所选值显示在 react-select 的输入栏中?

[英]How to disable selected values from getting displayed in input bar in react-select?

const options = [
  { value: 'chocolate', label: 'Chocolate' },
  { value: 'strawberry', label: 'Strawberry' },
  { value: 'vanilla', label: 'Vanilla' }
];

const Option = props => { 
  //const temp = "some";
  //
  return ( 
  <div> 
    <components.Option {...props}> 
      <input type="checkbox" checked={props.isSelected} onChange={() => null} /> 
      <label>{props.value}</label> 
    </components.Option>
  </div> 
  ); 
};

Currently my code looks like above, what it does is, it is displaying something like this ScreenShot目前我的代码看起来像上面一样,它的作用是,它显示类似这样的ScreenShot

I don't want to display selected values in search input bar.Is there any way to disable selected option from getting displayed in input bar ?我不想在搜索输入栏中显示选定的值。有什么办法可以禁止在输入栏中显示选定的选项?

Code for Select Bar选择栏代码

<Select components={{ Option }} isMulti closeMenuOnSelect={false} hideSelectedOptions={false} options={options} />

there is a new props controlShouldRenderValue = { false } then it will not display selected option in input bar, even after selecting option from drop down list as mention below有一个新的道具controlShouldRenderValue = { false }那么它不会在输入栏中显示选定的选项,即使从下拉列表中选择选项后,如下所述

<Select
 components={{ Option }}
 isMulti closeMenuOnSelect={false}
 hideSelectedOptions={false}
 controlShouldRenderValue = { false }
 options={options} />

So, in the search box it will display placeholder因此,在搜索框中它将显示占位符

I suspect it's to do with the hideSelectedOptions prop you're passing into the Select component here:我怀疑这与您传入Select组件的hideSelectedOptions道具有关:

<Select hideSelectedOptions={false} />

I would try setting it to true (or removing it) and see if that solves your issue.我会尝试将其设置为true (或删除它),看看是否能解决您的问题。

One option is to define the custom component with an empty div一种选择是使用空 div 定义自定义组件

 const MultiValueContainer = props => { return ( <div></div> ); }; class PriceFilter extends React.Component { constructor(props) { super(props); } render() { return [ <Select isMulti components={{ MultiValueContainer }} options={this.props.options} className="basic-multi-select" classNamePrefix="select" hideSelectedOptions={false} closeMenuOnSelect={false} /> ]; } }

You could simply render out your options only if their isSelected is false, and in the other case, send out an empty div using a ternary condition :只有当它们的isSelected为 false 时,您才能简单地呈现您的选项,在另一种情况下,使用三元条件发送一个空的div

const Option = props => {
    return !props.isSelected ?
        <div>
            <components.Option {...props}>
                <input type="checkbox" checked={props.isSelected} onChange={() => null} />
                <label>{props.value}</label>
            </components.Option>
        </div>
        :
        <div/>
};

You will have to return a JSX node in every cases, returning null (or an equivalent) will generate a warning/error.在每种情况下,您都必须返回一个 JSX 节点,返回null (或等效项)将生成警告/错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM