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

64 lines
1.6 KiB
Markdown
Raw Normal View History

2016-03-31 09:40:55 +08:00
---
2018-05-21 17:07:39 +08:00
order: 7
title:
2016-07-10 08:55:43 +08:00
zh-CN: 联动
en-US: coordinate
2016-03-31 09:40:55 +08:00
---
2015-08-06 12:06:05 +08:00
2016-07-10 08:55:43 +08:00
## zh-CN
2015-08-06 12:06:05 +08:00
省市联动是典型的例子。
推荐使用 [Cascader](/components/cascader/) 组件。
2016-03-31 09:40:55 +08:00
2016-07-10 08:55:43 +08:00
## en-US
Coordinating the selection of provinces and cities is a common use case and demonstrates how selection can be coordinated.
Using the [Cascader](/components/cascader) component is strongly recommended instead as it is more flexible and capable.
```tsx
import React, { useState } from 'react';
import { Select } from 'antd';
2018-06-27 15:55:04 +08:00
const { Option } = Select;
const provinceData = ['Zhejiang', 'Jiangsu'];
const cityData = {
Zhejiang: ['Hangzhou', 'Ningbo', 'Wenzhou'],
Jiangsu: ['Nanjing', 'Suzhou', 'Zhenjiang'],
2015-08-06 12:06:05 +08:00
};
type CityName = keyof typeof cityData;
2018-06-27 15:55:04 +08:00
const App: React.FC = () => {
const [cities, setCities] = useState(cityData[provinceData[0] as CityName]);
const [secondCity, setSecondCity] = useState(cityData[provinceData[0] as CityName][0]);
const handleProvinceChange = (value: CityName) => {
setCities(cityData[value]);
setSecondCity(cityData[value][0]);
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
const onSecondCityChange = (value: CityName) => {
setSecondCity(value);
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
return (
<>
<Select defaultValue={provinceData[0]} style={{ width: 120 }} onChange={handleProvinceChange}>
{provinceData.map(province => (
<Option key={province}>{province}</Option>
))}
</Select>
<Select style={{ width: 120 }} value={secondCity} onChange={onSecondCityChange}>
{cities.map(city => (
<Option key={city}>{city}</Option>
))}
</Select>
</>
);
};
export default App;
2019-05-07 14:57:32 +08:00
```