ant-design/components/select/demo/coordinate.md

58 lines
1.3 KiB
Markdown
Raw Normal View History

2015-08-06 12:06:05 +08:00
# 联动
- order: 6
省市联动是典型的例子。
---
````jsx
import { Select } from 'antd';
const Option = Select.Option;
2015-08-06 12:06:05 +08:00
const provinceData = ['浙江', '江苏'];
const cityData = {
2015-08-06 12:06:05 +08:00
'浙江': ['杭州', '宁波', '温州'],
'江苏': ['南京', '苏州', '镇江']
};
const App = React.createClass({
2015-08-06 12:06:05 +08:00
getInitialState() {
return {
2015-08-20 17:27:40 +08:00
cities: cityData[provinceData[0]],
secondCity:cityData[provinceData[0]][0]
2015-08-06 12:06:05 +08:00
};
},
handleProvinceChange(value) {
this.setState({
2015-08-20 17:27:40 +08:00
cities: cityData[value],
secondCity:cityData[value][0]
});
},
onSecondCityChange(value) {
this.setState({
secondCity: value
2015-08-06 12:06:05 +08:00
});
},
render() {
const provinceOptions = provinceData.map(function(province) {
2015-08-21 14:54:37 +08:00
return <Option key={province}>{province}</Option>;
2015-08-06 12:06:05 +08:00
});
const cityOptions = this.state.cities.map(function(city) {
2015-08-21 14:54:37 +08:00
return <Option key={city}>{city}</Option>;
2015-08-06 12:06:05 +08:00
});
return <div>
<Select defaultValue={provinceData[0]} style={{width:150}} onChange={this.handleProvinceChange}>
{provinceOptions}
</Select>
2015-08-20 17:27:40 +08:00
<Select value={this.state.secondCity} style={{width:150}} onChange={this.onSecondCityChange}>
2015-08-06 12:06:05 +08:00
{cityOptions}
</Select>
</div>;
}
});
2015-10-20 16:47:55 +08:00
ReactDOM.render(<App />, document.getElementById('components-select-demo-coordinate'));
2015-08-06 12:06:05 +08:00
````