Javascript 反应选择禁用选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48789307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:14:52  来源:igfitidea点击:

React Select disable options

javascriptreactjs

提问by David Jarrin

I'm having issues disabling certain options within a large list within a React Select element. I have around 6,500 options that get loaded into the select. At first I was having issues with the search functionality lagging but then I started using react-select-fast-filter-options which took care of that problem. Now the issue is that I need to disable certain options depending on the propType "picks". Here is the code:

我在禁用 React Select 元素的大列表中的某些选项时遇到问题。我有大约 6,500 个选项可以加载到选择中。起初我遇到了搜索功能滞后的问题,但后来我开始使用 react-select-fast-filter-options 来解决这个问题。现在的问题是我需要根据 propType“选择”禁用某些选项。这是代码:

import React, {Component} from 'react'
import PropTypes from 'prop-types';
import 'react-select/dist/react-select.css'
import 'react-virtualized/styles.css'
import 'react-virtualized-select/styles.css'
import Select from 'react-virtualized-select'
import createFilterOptions from 'react-select-fast-filter-options';

let options = [];
if(typeof stockSearchStocks !== 'undefined') {
    //loads in all available options from backend by laying down a static js var
    options = stockSearchStocks
}
const filterOptions =  createFilterOptions({options});

class StockSearch extends Component {
    static propTypes = {
        exchanges: PropTypes.array.isRequired,
        onSelectChange: PropTypes.func.isRequired,
        searchDisabled: PropTypes.bool.isRequired,
        picks: PropTypes.array.isRequired,
        stock_edit_to_show: PropTypes.number
    }

    /**
     * Component Bridge Function
     * @param stock_id stocks id in the database
     */
    stockSearchChange = (stock_id) => {
        this.props.onSelectChange(stock_id);
    }

     //this is my current attempt to at least 
     //disable options on component mount but this doesn't seem to be working
    componentWillMount = () => {
        console.log('picks!: ' + JSON.stringify(this.props.picks));
        let pickIDs = this.props.picks.map((p) => p.stock_id);
        options = options.map((o) => {
            // console.log(pickIDs.indexOf(o.value));
            if(pickIDs.indexOf(o.value)) {
                // console.log('here is the option: ' + JSON.stringify(o));
                // console.log('here is the option: ' + o.disabled);
                o.disabled = true;
            }
        })

    }

    /**
     * handles selected option from the stock select
     * @param selectedOption
     */
    handleSelect = (selectedOption) => {
        this.stockSearchChange(selectedOption.value);
    }

    render() {
        return (
            <div className="stock-search-container">
                <Select
                    name="stock-search"
                    options={options}
                    placeholder="Type or select a stock here..."
                    onChange={this.handleSelect}
                    disabled={this.props.searchDisabled}
                    value={this.props.stock_edit_to_show}
                    filterOptions={filterOptions}
                />
            </div>
        )
    }
}

export default StockSearch;

I have tried filtering through the picks props and changing that options variable to include disabled:truebut this lags the application and I'm not sure if that will work now that I'm using react-select-fast-filter-options as it seems to be doing some sort of indexing. Is there a way to filter through the options var to find all instances of the picks prop and disable those options quickly?

我已经尝试通过选择道具过滤并更改该选项变量以包含,disabled:true但这会使应用程序滞后,而且我不确定现在我正在使用 react-select-fast-filter-options 是否可行,因为它似乎是做某种索引。有没有办法过滤选项 var 以查找 picks 道具的所有实例并快速禁用这些选项?

回答by Saurabh Suman

isDisabled={this.props.disabled}

You are passing a wrong prop.. For v2, the prop is isDisabled.

您传递了错误的道具。对于 v2,道具是禁用的。

https://github.com/JedWatson/react-select/issues/145

https://github.com/JedWatson/react-select/issues/145

回答by Alla Sorokina

In react-select v2:

在反应选择 v2 中:

1) add to your array of options a property 'disabled': 'yes' (or any other pair to identify disabled options)

1) 在您的选项数组中添加一个属性 'disabled': 'yes' (或任何其他用于标识禁用选项的对)

2) use isOptionDisabled props of react-select component to filter options based on 'disabled' property

2) 使用 react-select 组件的 isOptionDisabled 属性来过滤基于 'disabled' 属性的选项

Here's an example:

下面是一个例子:

import Select from 'react-select';

const options = [
{label: "one", value: 1, disabled: 'yes'},
{label: "two", value: 2]

render() {

 <Select id={'dropdown'}
         options={options}
         isOptionDisabled={(option) => option.disabled === 'yes'}>
 </Select>

}

More information about react-select props is in the docsand here's an examplethey reference.

有关 react-select 道具的更多信息在文档中,这是他们引用的示例

回答by Beu

use the following to disable an option.

使用以下命令禁用一个选项。

import Select from 'react-select';

render() {
  const options = [
    {label: "a", value: "a", isDisabled: true},
    {label: "b", value: "b"}
  ];

  return (
    <Select
      name="myselect"
      options={options}
    </Select>
  )
}