34 lines
904 B
TypeScript
34 lines
904 B
TypeScript
|
|
import areaData from 'china-area-data';
|
||
|
|
|
||
|
|
export interface CascadeOption {
|
||
|
|
value: string;
|
||
|
|
label: string;
|
||
|
|
children?: CascadeOption[];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 将 china-area-data 原始数据转换为 antd Cascader 所需的 options 格式
|
||
|
|
*
|
||
|
|
* 原始数据格式: { '86': { '110000': '北京' }, '110000': { '110100': '市辖区' }, ... }
|
||
|
|
* 目标格式: [{ value: '110000', label: '北京', children: [...] }]
|
||
|
|
*/
|
||
|
|
function buildTree(parentCode: string): CascadeOption[] {
|
||
|
|
const nodes = areaData[parentCode];
|
||
|
|
if (!nodes) return [];
|
||
|
|
|
||
|
|
return Object.entries(nodes).map(([code, name]) => {
|
||
|
|
const node: CascadeOption = {
|
||
|
|
value: code,
|
||
|
|
label: name as string,
|
||
|
|
};
|
||
|
|
const children = buildTree(code);
|
||
|
|
if (children.length > 0) {
|
||
|
|
node.children = children;
|
||
|
|
}
|
||
|
|
return node;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 省市区三级联动数据 */
|
||
|
|
export const regionOptions: CascadeOption[] = buildTree('86');
|