i use react-select on my dependent dropdownlist. Options2 will be dependent on Options1. i use filter() to display which Options2 will be showed up. Then i want to clear / reset the selectedOption2 eachtime selectedOption changed. I add my value props in order to understandable for you. When selectedOption is changed, selectedOption2 will be "Select..". i have tried to solve this code, but i can't.
import React, { Component } from "react";
import Select from "react-select";
import { options1, options2 } from "./data";
class Esensial extends Component {
constructor() {
super();
this.state = {
selectedOption: {},
selectedOption2: {}
};
}
handleChange1 = selectedOption => {
this.setState({ selectedOption });
this.setState({ selectedOption2: null });
};
handleChange2 = selectedOption => {
this.setState({ selectedOption2: selectedOption });
};
render() {
const options1 = [
{ value: "One",
label: "One"
},
{
value: "Two",
label: "Two"
}];
const options2 = [
{
value: "One-A",
label: "One-A",
link: "One"
},
{
value: "One-B",
label: "One-B",
link: "One"
},
{
value: "Two-A",
label: "Two-A",
link: "Two"
},
{
value: "Two-B",
label: "Two-B",
link: "Two"
}];
const filteredOptions = options2.filter(
o => o.link === this.state.selectedOption.value
);
return (
<div>
<h1>UKM Esensial</h1>
<div className="form-group">
<label>Pilih Jenis Upaya Pelayanan Kesehatan</label>
<Select
className="form-control"
isClearable={false}
onChange={this.handleChange1}
options={options1}
/>
</div>
<div className="form-group">
<label>Pilih Variable</label>
<Select
className="form-control"
isClearable
onChange={this.handleChange2}
options={filteredOptions}
/>
</div>
</div>
);
}
}
export default Esensial;