Created
March 17, 2017 16:25
-
-
Save r2dev/14da7513a5f0fa1eef54bdca20d4d52e to your computer and use it in GitHub Desktop.
test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const SelectProvince = ({ provinces, value, onChange }) => ( | |
<select value={value} onChange={onChange}> | |
{provinces.map((pro, index) => { | |
return ( | |
<option value={pro} key={index}> | |
{pro} | |
</option> | |
); | |
})} | |
</select> | |
); | |
const SelectCity = ({ cities, value, onChange }) => ( | |
<select value={value} onChange={onChange}> | |
{cities.map((city, index) => { | |
return ( | |
<option value={city} key={index}> | |
{city} | |
</option> | |
); | |
})} | |
</select> | |
); | |
class AwesomeSelect extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
provinceValue: this.props.data.province[0], | |
cityValue: this.props.data[this.props.data.province[0]][0], | |
cities: this.props.data[this.props.data.province[0]] | |
}; | |
} | |
render() { | |
return ( | |
<div> | |
<lable> 省 </lable> | |
<SelectProvince | |
provinces={this.props.data.province} | |
value={this.state.provinceValue} | |
onChange={e => { | |
this.setState({ | |
provinceValue: e.target.value, | |
cityValue: this.props.data[e.target.value][0], | |
cities: this.props.data[e.target.value] | |
}); | |
}} | |
/> | |
<lable> 城市 </lable> | |
<SelectCity | |
cities={this.state.cities} | |
value={this.state.cityValue} | |
onChange={e => { | |
this.setState({ cityValue: e.target.value }); | |
}} | |
/> | |
{this.state.provinceValue} ---- {this.state.cityValue} | |
</div> | |
); | |
} | |
} | |
const data = { | |
on: ["toronto", "markham", "kingston"], | |
bc: ["richmond", "vancouver", "trail"], | |
ab: ["edmonton", "leduc"], | |
province: ["on", "bc", "ab"] | |
}; | |
ReactDOM.render(<AwesomeSelect data={data} />, document.getElementById("root")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment