ant-design/components/tree/demo/line.md

137 lines
3.5 KiB
Markdown
Raw Normal View History

---
order: 5
title:
zh-CN: 连接线
en-US: Tree with line
---
## zh-CN
2019-12-11 18:22:06 +08:00
节点之间带连接线的树,常用于文件目录结构展示。使用 `showLine` 开启,可以用 `switcherIcon` 修改默认图标。
## en-US
2019-12-11 18:22:06 +08:00
Tree with connected line between nodes, turn on by `showLine`, customize the preseted icon by `switcherIcon`.
```tsx
2022-09-28 12:01:20 +08:00
import { CarryOutOutlined, CheckOutlined, FormOutlined } from '@ant-design/icons';
import { Select, Switch, Tree } from 'antd';
2022-07-04 22:09:54 +08:00
import type { DataNode } from 'antd/es/tree';
2022-05-23 14:37:16 +08:00
import React, { useState } from 'react';
2018-06-27 15:55:04 +08:00
const treeData: DataNode[] = [
{
title: 'parent 1',
key: '0-0',
2019-12-25 19:09:17 +08:00
icon: <CarryOutOutlined />,
children: [
{
title: 'parent 1-0',
key: '0-0-0',
2019-12-25 19:09:17 +08:00
icon: <CarryOutOutlined />,
children: [
2019-12-25 19:09:17 +08:00
{ title: 'leaf', key: '0-0-0-0', icon: <CarryOutOutlined /> },
{
title: (
<>
<div>multiple line title</div>
<div>multiple line title</div>
</>
),
key: '0-0-0-1',
icon: <CarryOutOutlined />,
},
2019-12-25 19:09:17 +08:00
{ title: 'leaf', key: '0-0-0-2', icon: <CarryOutOutlined /> },
],
},
{
title: 'parent 1-1',
key: '0-0-1',
2019-12-25 19:09:17 +08:00
icon: <CarryOutOutlined />,
children: [{ title: 'leaf', key: '0-0-1-0', icon: <CarryOutOutlined /> }],
},
{
title: 'parent 1-2',
key: '0-0-2',
2019-12-25 19:09:17 +08:00
icon: <CarryOutOutlined />,
children: [
{ title: 'leaf', key: '0-0-2-0', icon: <CarryOutOutlined /> },
{
title: 'leaf',
key: '0-0-2-1',
icon: <CarryOutOutlined />,
switcherIcon: <FormOutlined />,
},
],
},
],
},
{
title: 'parent 2',
key: '0-1',
icon: <CarryOutOutlined />,
children: [
{
title: 'parent 2-0',
key: '0-1-0',
icon: <CarryOutOutlined />,
children: [
{ title: 'leaf', key: '0-1-0-0', icon: <CarryOutOutlined /> },
{ title: 'leaf', key: '0-1-0-1', icon: <CarryOutOutlined /> },
],
},
],
},
];
const App: React.FC = () => {
2022-09-28 12:01:20 +08:00
const [showLine, setShowLine] = useState<boolean>(true);
const [showIcon, setShowIcon] = useState<boolean>(false);
2022-09-28 12:01:20 +08:00
const [showLeafIcon, setShowLeafIcon] = useState<boolean | React.ReactNode>(true);
2019-12-11 18:22:06 +08:00
const onSelect = (selectedKeys: React.Key[], info: any) => {
console.log('selected', selectedKeys, info);
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
2022-09-28 12:01:20 +08:00
const handleLeafIconChange = (value: 'true' | 'false' | 'custom') => {
if (value === 'custom') {
return setShowLeafIcon(<CheckOutlined />);
}
if (value === 'true') {
return setShowLeafIcon(true);
}
2022-09-28 12:01:20 +08:00
return setShowLeafIcon(false);
};
2019-12-25 19:09:17 +08:00
return (
<div>
<div style={{ marginBottom: 16 }}>
2022-09-28 12:01:20 +08:00
showLine: <Switch checked={!!showLine} onChange={setShowLine} />
2019-12-25 19:09:17 +08:00
<br />
<br />
showIcon: <Switch checked={showIcon} onChange={setShowIcon} />
<br />
<br />
2022-09-28 12:01:20 +08:00
showLeafIcon:{' '}
<Select defaultValue="true" onChange={handleLeafIconChange}>
<Select.Option value="true">True</Select.Option>
<Select.Option value="false">False</Select.Option>
<Select.Option value="custom">Custom icon</Select.Option>
</Select>
2019-12-11 18:22:06 +08:00
</div>
2019-12-25 19:09:17 +08:00
<Tree
2022-09-28 12:01:20 +08:00
showLine={showLine ? { showLeafIcon } : false}
2019-12-25 19:09:17 +08:00
showIcon={showIcon}
defaultExpandedKeys={['0-0-0']}
onSelect={onSelect}
treeData={treeData}
/>
</div>
);
};
export default App;
2019-05-07 14:57:32 +08:00
```