Last active
December 14, 2023 01:47
-
-
Save afc163/7582f35654fd03d5be7009444345ea17 to your computer and use it in GitHub Desktop.
Address options for antd cascader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import provinces from 'china-division/dist/provinces.json'; | |
import cities from 'china-division/dist/cities.json'; | |
import areas from 'china-division/dist/areas.json'; | |
areas.forEach((area) => { | |
const matchCity = cities.filter(city => city.code === area.cityCode)[0]; | |
if (matchCity) { | |
matchCity.children = matchCity.children || []; | |
matchCity.children.push({ | |
label: area.name, | |
value: area.code, | |
}); | |
} | |
}); | |
cities.forEach((city) => { | |
const matchProvince = provinces.filter(province => province.code === city.provinceCode)[0]; | |
if (matchProvince) { | |
matchProvince.children = matchProvince.children || []; | |
matchProvince.children.push({ | |
label: city.name, | |
value: city.code, | |
children: city.children, | |
}); | |
} | |
}); | |
const options = provinces.map(province => ({ | |
label: province.name, | |
value: province.code, | |
children: province.children, | |
})); | |
export default options; |
这个拿到了code怎么回显啊,不在cascader组件上回显。
这个过时了,没办法拉下来就使用,我写了个新的
import React from "react";
import ReactDOM from "react-dom";
import {Cascader} from "antd";
import areas from "china-division/dist/areas.json";
import cities from "china-division/dist/cities.json";
import provinces from "china-division/dist/provinces.json";
// 定义接口
interface Area {
code: string;
name: string;
cityCode?: string;
}
interface City {
code: string;
name: string;
provinceCode: string;
children?: AreaOption[];
}
interface Province {
code: string;
name: string;
children?: CityOption[];
}
interface AreaOption {
label: string;
value: string;
}
interface CityOption extends AreaOption {
children?: AreaOption[];
}
// 将原始数据转换为带有 children 属性的类型
const typedCities: City[] = cities.map((city) => ({...city, children: []}));
const typedProvinces: Province[] = provinces.map((province) => ({
...province,
children: []
}));
const typedAreas: Area[] = areas.map((area) => ({...area}));
typedAreas.forEach((area) => {
const matchCity = typedCities.find((city) => city.code === area.cityCode);
if (matchCity) {
matchCity.children!.push({
label: area.name,
value: area.code
});
}
});
typedCities.forEach((city) => {
const matchProvince = typedProvinces.find(
(province) => province.code === city.provinceCode
);
if (matchProvince) {
matchProvince.children!.push({
label: city.name,
value: city.code,
children: city.children
});
}
});
const options = typedProvinces.map((province) => ({
label: province.name,
value: province.code,
children: province.children
}));
ReactDOM.render(
<div style={{margin: 24}}>
<p style={{marginBottom: 24}}>Antd Cascader Demo</p>
<Cascader
options={options}
showSearch
placeholder="请选择地址"
style={{width: 400}}
/>
</div>,
document.getElementById("root")
);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/pansyjs/china-division 提供了
json
和js
两种格式的数据,包含了香港、澳门、台湾,基于china-division提供的数据