mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-12-15 17:31:25 +08:00
51200e7732
Demo 有问题,会在 VSCode中 引用 `loadData`、`onChange` 时 会报错
57 lines
1.3 KiB
TypeScript
57 lines
1.3 KiB
TypeScript
import React, { useState } from 'react';
|
|
import { Cascader } from 'antd';
|
|
|
|
interface Option {
|
|
value?: string | number | null;
|
|
label: React.ReactNode;
|
|
children?: Option[];
|
|
isLeaf?: boolean;
|
|
loading?: boolean;
|
|
}
|
|
|
|
const optionLists: Option[] = [
|
|
{
|
|
value: 'zhejiang',
|
|
label: 'Zhejiang',
|
|
isLeaf: false,
|
|
},
|
|
{
|
|
value: 'jiangsu',
|
|
label: 'Jiangsu',
|
|
isLeaf: false,
|
|
},
|
|
];
|
|
|
|
const App: React.FC = () => {
|
|
const [options, setOptions] = useState<Option[]>(optionLists);
|
|
|
|
const onChange = (value: (string | number)[], selectedOptions: Option[]) => {
|
|
console.log(value, selectedOptions);
|
|
};
|
|
|
|
const loadData = (selectedOptions: Option[]) => {
|
|
const targetOption = selectedOptions[selectedOptions.length - 1];
|
|
targetOption.loading = true;
|
|
|
|
// load options lazily
|
|
setTimeout(() => {
|
|
targetOption.loading = false;
|
|
targetOption.children = [
|
|
{
|
|
label: `${targetOption.label} Dynamic 1`,
|
|
value: 'dynamic1',
|
|
},
|
|
{
|
|
label: `${targetOption.label} Dynamic 2`,
|
|
value: 'dynamic2',
|
|
},
|
|
];
|
|
setOptions([...options]);
|
|
}, 1000);
|
|
};
|
|
|
|
return <Cascader options={options} loadData={loadData} onChange={onChange} changeOnSelect />;
|
|
};
|
|
|
|
export default App;
|