【问题标题】:React Select disable options反应选择禁用选项
【发布时间】:2018-02-14 14:14:40
【问题描述】:

我在禁用 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;

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

【问题讨论】:

    标签: javascript reactjs


    【解决方案1】:
    isDisabled={this.props.disabled}
    

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

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

    【讨论】:

    • 为什么没有记录在任何地方?
    • 记录在案here
    【解决方案2】:

    在 react-select v2 中:

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

    2. 使用 react-select 组件的 isOptionDisabled 属性根据 'disabled' 属性过滤选项

    这是一个例子:

    import Select from 'react-select';
    
    const options = [
      {label: "one", value: 1, disabled: true},
      {label: "two", value: 2}
    ]
    
    render() {
    
     <Select id={'dropdown'}
             options={options}
             isOptionDisabled={(option) => option.disabled}>
     </Select>
    
    }
    

    有关 react-select 道具的更多信息在 docs 中,这是他们引用的 example

    【讨论】:

      【解决方案3】:

      使用以下命令禁用选项。

      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>
        )
      }
      

      【讨论】:

      • 在当前版本的react-select中,key是isDisabled,而不是disabled
      【解决方案4】:

      人们正在把它变成一个 JavaScript 问题。我说让它成为一个 CSS 并简化。 使用这个

      style={{display: month === '2' ? 'none' : 'block'}} 
      

      像这样... 二月只有28天

      
      const ComponentName =() => {
      
        const [month, setMonth] = useState("")
        const [day, setDay] = useState("")
      
      
      return (
      <>
      <select 
      onChange={(e) => {
          const selectedMonth = e.target.value;
          setMonth(selectedMonth)> 
         <option selected disabled>Month</option>
         <option value= '1'>January</option>
         <option value= '2'>February</option>
      </select>
      
      <select
      onChange={(e) => {
          const selectedDay = e.target.value;
          setDay(selectedDay)> 
         <option selected disabled>Day</option>
         <option value= '28'>28</option>
         <option value= '29' style={{display: month === '2' ? 'none' : 'block'}}>29</option>
         <option value= '30'>30</option>
      </select>
      
      </>
      )
      
      }
      
      export default ComponentName;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-19
        • 2021-08-16
        相关资源
        最近更新 更多