用antd select做一个级联选择器
时间: 2024-01-30 17:03:05 浏览: 106
select级联
好的,下面是一个antd select级联选择器的示例代码:
```
import React, { useState } from 'react';
import { Select } from 'antd';
const { Option } = Select;
const CascaderSelect = () => {
const [cities, setCities] = useState([]);
const [regions, setRegions] = useState([]);
const handleCountryChange = (value) => {
setCities([]);
setRegions([]);
// 根据选中的国家,设置城市选项
if (value === 'china') {
setCities(['beijing', 'shanghai', 'guangzhou']);
} else if (value === 'usa') {
setCities(['new york', 'washington', 'los angeles']);
} else if (value === 'japan') {
setCities(['tokyo', 'osaka', 'kyoto']);
}
}
const handleCityChange = (value) => {
setRegions([]);
// 根据选中的城市,设置地区选项
if (value === 'beijing') {
setRegions(['haidian', 'chaoyang', 'dongcheng']);
} else if (value === 'shanghai') {
setRegions(['pudong', 'minhang', 'xuhui']);
} else if (value === 'guangzhou') {
setRegions(['yuexiu', 'tianhe', 'haizhu']);
} else if (value === 'new york') {
setRegions(['manhattan', 'brooklyn', 'queens']);
} else if (value === 'washington') {
setRegions(['northwest', 'northeast', 'southeast']);
} else if (value === 'los angeles') {
setRegions(['hollywood', 'beverly hills', 'santa monica']);
} else if (value === 'tokyo') {
setRegions(['shibuya', 'shinjuku', 'harajuku']);
} else if (value === 'osaka') {
setRegions(['umeda', 'namba', 'tennoji']);
} else if (value === 'kyoto') {
setRegions(['higashiyama', 'nakagyo', 'shimogyo']);
}
}
return (
<div>
<Select style={{ width: 200 }} onChange={handleCountryChange}>
<Option value="china">China</Option>
<Option value="usa">USA</Option>
<Option value="japan">Japan</Option>
</Select>
<Select style={{ width: 200, marginLeft: 10 }} onChange={handleCityChange}>
{cities.map(city => (
<Option key={city} value={city}>{city}</Option>
))}
</Select>
<Select style={{ width: 200, marginLeft: 10 }}>
{regions.map(region => (
<Option key={region} value={region}>{region}</Option>
))}
</Select>
</div>
);
}
export default CascaderSelect;
```
这个示例代码中,我们使用了React的useState Hook来管理组件的状态,根据选中的国家和城市设置城市和地区选项。在Select组件中,我们使用了antd的Option组件来设置选项,onChange事件用来处理选项的变化,根据选中的值来动态更新组件的状态。
阅读全文