mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-16 01:29:11 +08:00
91821d4d3a
* feat: select edit demo to data driven * feat: edit demo to data driven Co-authored-by: 二货机器人 <smith3816@gmail.com>
1.5 KiB
1.5 KiB
order | title | ||||
---|---|---|---|---|---|
7 |
|
zh-CN
省市联动是典型的例子。
推荐使用 Cascader 组件。
en-US
Coordinating the selection of provinces and cities is a common use case and demonstrates how selection can be coordinated.
Using the Cascader component is strongly recommended instead as it is more flexible and capable.
import { Select } from 'antd';
import React, { useState } from 'react';
const provinceData = ['Zhejiang', 'Jiangsu'];
const cityData = {
Zhejiang: ['Hangzhou', 'Ningbo', 'Wenzhou'],
Jiangsu: ['Nanjing', 'Suzhou', 'Zhenjiang'],
};
type CityName = keyof typeof cityData;
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]);
};
const onSecondCityChange = (value: CityName) => {
setSecondCity(value);
};
return (
<>
<Select
defaultValue={provinceData[0]}
style={{ width: 120 }}
onChange={handleProvinceChange}
options={provinceData.map(province => ({ label: province, value: province }))}
/>
<Select
style={{ width: 120 }}
value={secondCity}
onChange={onSecondCityChange}
options={cities.map(city => ({ label: city, value: city }))}
/>
</>
);
};
export default App;