React选择禁用选项

Dav*_*rin 17 javascript reactjs

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

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;
Run Code Online (Sandbox Code Playgroud)

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

小智 17

isDisabled={this.props.disabled}
Run Code Online (Sandbox Code Playgroud)

您传递的是错误的道具。对于v2,该道具为isDisabled。

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

  • 为什么这没有任何地方记录? (2认同)

All*_*ina 12

在react-select v2中:

1)在选项数组中添加一个属性“ disabled”:“ yes”(或任何其他对以标识已禁用选项)

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

这是一个例子:

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>

}
Run Code Online (Sandbox Code Playgroud)

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


Beu*_*Beu 5

使用以下命令禁用选项。

import Select from 'react-select';

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

  return (
    <Select
      name="myselect"
      options={options}
    </Select>
  )
}
Run Code Online (Sandbox Code Playgroud)

  • 在当前版本的“ react-select”中,键为“ isDisabled”,而不是“ disabled”。 (2认同)