2022-11-09 12:28:04 +08:00
|
|
|
import React, { useState } from 'react';
|
|
|
|
import { Tree } from 'antd';
|
2024-04-01 15:49:45 +08:00
|
|
|
import type { TreeDataNode, TreeProps } from 'antd';
|
2022-11-09 12:28:04 +08:00
|
|
|
|
2024-01-11 15:55:58 +08:00
|
|
|
const treeData: TreeDataNode[] = [
|
2022-11-09 12:28:04 +08:00
|
|
|
{
|
|
|
|
title: '0-0',
|
|
|
|
key: '0-0',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
title: '0-0-0',
|
|
|
|
key: '0-0-0',
|
|
|
|
children: [
|
|
|
|
{ title: '0-0-0-0', key: '0-0-0-0' },
|
|
|
|
{ title: '0-0-0-1', key: '0-0-0-1' },
|
|
|
|
{ title: '0-0-0-2', key: '0-0-0-2' },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: '0-0-1',
|
|
|
|
key: '0-0-1',
|
|
|
|
children: [
|
|
|
|
{ title: '0-0-1-0', key: '0-0-1-0' },
|
|
|
|
{ title: '0-0-1-1', key: '0-0-1-1' },
|
|
|
|
{ title: '0-0-1-2', key: '0-0-1-2' },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: '0-0-2',
|
|
|
|
key: '0-0-2',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: '0-1',
|
|
|
|
key: '0-1',
|
|
|
|
children: [
|
|
|
|
{ title: '0-1-0-0', key: '0-1-0-0' },
|
|
|
|
{ title: '0-1-0-1', key: '0-1-0-1' },
|
|
|
|
{ title: '0-1-0-2', key: '0-1-0-2' },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: '0-2',
|
|
|
|
key: '0-2',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const App: React.FC = () => {
|
|
|
|
const [expandedKeys, setExpandedKeys] = useState<React.Key[]>(['0-0-0', '0-0-1']);
|
|
|
|
const [checkedKeys, setCheckedKeys] = useState<React.Key[]>(['0-0-0']);
|
|
|
|
const [selectedKeys, setSelectedKeys] = useState<React.Key[]>([]);
|
|
|
|
const [autoExpandParent, setAutoExpandParent] = useState<boolean>(true);
|
|
|
|
|
2024-04-01 15:49:45 +08:00
|
|
|
const onExpand: TreeProps['onExpand'] = (expandedKeysValue) => {
|
2022-11-09 12:28:04 +08:00
|
|
|
console.log('onExpand', expandedKeysValue);
|
|
|
|
// if not set autoExpandParent to false, if children expanded, parent can not collapse.
|
|
|
|
// or, you can remove all expanded children keys.
|
|
|
|
setExpandedKeys(expandedKeysValue);
|
|
|
|
setAutoExpandParent(false);
|
|
|
|
};
|
|
|
|
|
2024-04-01 15:49:45 +08:00
|
|
|
const onCheck: TreeProps['onCheck'] = (checkedKeysValue) => {
|
2022-11-09 12:28:04 +08:00
|
|
|
console.log('onCheck', checkedKeysValue);
|
2024-04-01 15:49:45 +08:00
|
|
|
setCheckedKeys(checkedKeysValue as React.Key[]);
|
2022-11-09 12:28:04 +08:00
|
|
|
};
|
|
|
|
|
2024-04-01 15:49:45 +08:00
|
|
|
const onSelect: TreeProps['onSelect'] = (selectedKeysValue, info) => {
|
2022-11-09 12:28:04 +08:00
|
|
|
console.log('onSelect', info);
|
|
|
|
setSelectedKeys(selectedKeysValue);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Tree
|
|
|
|
checkable
|
|
|
|
onExpand={onExpand}
|
|
|
|
expandedKeys={expandedKeys}
|
|
|
|
autoExpandParent={autoExpandParent}
|
|
|
|
onCheck={onCheck}
|
|
|
|
checkedKeys={checkedKeys}
|
|
|
|
onSelect={onSelect}
|
|
|
|
selectedKeys={selectedKeys}
|
|
|
|
treeData={treeData}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default App;
|