You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

44 lines
1.2 KiB

import React, { useState } from 'react';
import { Select, Space } from 'antd';
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 (
<Space wrap>
<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 }))}
/>
</Space>
);
};
export default App;