How to make a filter in react-select using the value of another react-select

前端 未结 2 684
轮回少年
轮回少年 2021-01-01 07:00

I\'m creating a solution using a react-select and that a select should contain only the options it does not contain in the other. Ex

import React from \'reac         


        
2条回答
  •  清歌不尽
    2021-01-01 07:29

    The answer above from Laura breaks the filtering as you're writing a custom filterOption function (If you don't want your Select to be filterable then it's probably ok).

    A simpler solution would be to just apply a simple filter on the options you pass through instead to the second Select instead.

    const options = [
      { label: "foo", value: 1 },
      { label: "bar", value: 2 },
      { label: "bin", value: 3 }
    ];
    
    class App extends Component {
      constructor(props) {
        super(props);
        this.state = {
          value1: false,
          value2: false
        };
      }
    
      onChange = option => {
        if (this.state.value2.value === option.value) {
          this.setState({
            value1: option,
            value2: false
          });
        } else {
          this.setState({ value1: option });
        }
      };
    
      onChange2 = option => {
        this.setState({ value2: option });
      };
      render() {
        return (
          
    option.value !== this.state.value1.value )} value={this.state.value2} />
    ); } }

    Working Example

提交回复
热议问题