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

前端 未结 2 675
轮回少年
轮回少年 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:37

    To achieve your goal you need to store your select values in the state in order to compare it with the options in filterOption like the following example:

    class App extends Component {
      constructor(props) {
        super(props);
        this.state = {
          value1: false,
          value2: false
        };
      }
    
      filterOption = ({ label, value, data }, string) => {
        if (this.state.value1 === data) {
          return false;
        } else if (string) {
          return label.includes(string) || value.toString().includes(string);
        } else {
          return true;
        }
      };
    
      onChange = option => {
        this.setState({ value1: option });
      };
    
      onChange2 = option => {
        this.setState({ value2: option });
      };
      render() {
        return (
          
    ); } }

    Here a live example.

提交回复
热议问题