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

我的未来我决定 提交于 2019-12-01 10:28:26

问题


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 'react'
import Select from 'react-select'

const list = [
  { label: 'foo', value: 1 },
  { label: 'bar', value: 2 },
  { label: 'bin', value: 3 },
]

export default Test extends React.Component {
  render = () => (
    <>
      <Select ref="a" options={list} />
      <Select ref="b" options={list} filterOption={o => /* devo omitir "a" */} />
    </>
  )
}

What do I need to do to have a <Select ref="b" /> filtered by the value of another <Select ref="a" /> ?


回答1:


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 (
      <div className="App">
        <Select
          onChange={this.onChange}
          options={options}
          value={this.state.value1}
        />
        <Select
          onChange={this.onChange2}
          options={options.filter(
            option => option.value !== this.state.value1.value
          )}
          value={this.state.value2}
        />
      </div>
    );
  }
}

Working Example




回答2:


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 (
      <div className="App">
        <Select
          onChange={this.onChange}
          options={options}
          value={this.state.value1}
        />
        <Select
          filterOption={this.filterOption}
          onChange={this.onChange2}
          options={options}
          value={this.state.value2}
        />
      </div>
    );
  }
}

Here a live example.



来源:https://stackoverflow.com/questions/56669201/how-to-make-a-filter-in-react-select-using-the-value-of-another-react-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!