2019-05-07 17:10:42 +08:00
|
|
|
---
|
|
|
|
order: 6
|
|
|
|
title:
|
|
|
|
zh-CN: 树穿梭框
|
|
|
|
en-US: Tree Transfer
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
使用 Tree 组件作为自定义渲染列表。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
|
|
|
Customize render list with Tree component.
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
```tsx
|
2019-05-07 17:10:42 +08:00
|
|
|
import { Transfer, Tree } from 'antd';
|
2022-05-19 09:46:26 +08:00
|
|
|
import type { TransferDirection, TransferItem } from 'antd/es/transfer';
|
|
|
|
import type { DataNode } from 'antd/es/tree';
|
2022-05-21 22:14:15 +08:00
|
|
|
import React, { useState } from 'react';
|
2022-05-19 09:46:26 +08:00
|
|
|
|
|
|
|
interface TreeTransferProps {
|
|
|
|
dataSource: DataNode[];
|
|
|
|
targetKeys: string[];
|
|
|
|
onChange: (targetKeys: string[], direction: TransferDirection, moveKeys: string[]) => void;
|
|
|
|
}
|
2019-05-07 17:10:42 +08:00
|
|
|
|
|
|
|
// Customize Table Transfer
|
2022-05-19 09:46:26 +08:00
|
|
|
const isChecked = (selectedKeys: (string | number)[], eventKey: string | number) =>
|
|
|
|
selectedKeys.includes(eventKey);
|
2019-05-07 17:10:42 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const generateTree = (treeNodes: DataNode[] = [], checkedKeys: string[] = []): DataNode[] =>
|
2020-08-22 17:55:09 +08:00
|
|
|
treeNodes.map(({ children, ...props }) => ({
|
2020-05-13 19:15:40 +08:00
|
|
|
...props,
|
2022-05-19 09:46:26 +08:00
|
|
|
disabled: checkedKeys.includes(props.key as string),
|
2020-05-13 19:15:40 +08:00
|
|
|
children: generateTree(children, checkedKeys),
|
|
|
|
}));
|
2019-05-07 17:10:42 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const TreeTransfer = ({ dataSource, targetKeys, ...restProps }: TreeTransferProps) => {
|
|
|
|
const transferDataSource: TransferItem[] = [];
|
|
|
|
function flatten(list: DataNode[] = []) {
|
2019-05-07 17:22:47 +08:00
|
|
|
list.forEach(item => {
|
2022-05-19 09:46:26 +08:00
|
|
|
transferDataSource.push(item as TransferItem);
|
2019-05-07 17:10:42 +08:00
|
|
|
flatten(item.children);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
flatten(dataSource);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Transfer
|
|
|
|
{...restProps}
|
|
|
|
targetKeys={targetKeys}
|
|
|
|
dataSource={transferDataSource}
|
|
|
|
className="tree-transfer"
|
2022-05-19 09:46:26 +08:00
|
|
|
render={item => item.title!}
|
2019-05-07 17:10:42 +08:00
|
|
|
showSelectAll={false}
|
|
|
|
>
|
|
|
|
{({ direction, onItemSelect, selectedKeys }) => {
|
|
|
|
if (direction === 'left') {
|
|
|
|
const checkedKeys = [...selectedKeys, ...targetKeys];
|
|
|
|
return (
|
|
|
|
<Tree
|
|
|
|
blockNode
|
|
|
|
checkable
|
|
|
|
checkStrictly
|
|
|
|
defaultExpandAll
|
|
|
|
checkedKeys={checkedKeys}
|
2020-05-13 19:15:40 +08:00
|
|
|
treeData={generateTree(dataSource, targetKeys)}
|
|
|
|
onCheck={(_, { node: { key } }) => {
|
2022-05-19 09:46:26 +08:00
|
|
|
onItemSelect(key as string, !isChecked(checkedKeys, key));
|
2019-05-07 17:10:42 +08:00
|
|
|
}}
|
2020-05-13 19:15:40 +08:00
|
|
|
onSelect={(_, { node: { key } }) => {
|
2022-05-19 09:46:26 +08:00
|
|
|
onItemSelect(key as string, !isChecked(checkedKeys, key));
|
2019-05-07 17:10:42 +08:00
|
|
|
}}
|
2020-05-13 19:15:40 +08:00
|
|
|
/>
|
2019-05-07 17:10:42 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
</Transfer>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const treeData: DataNode[] = [
|
2019-05-07 17:10:42 +08:00
|
|
|
{ key: '0-0', title: '0-0' },
|
2019-05-07 17:22:47 +08:00
|
|
|
{
|
|
|
|
key: '0-1',
|
|
|
|
title: '0-1',
|
2020-05-13 19:15:40 +08:00
|
|
|
children: [
|
|
|
|
{ key: '0-1-0', title: '0-1-0' },
|
|
|
|
{ key: '0-1-1', title: '0-1-1' },
|
|
|
|
],
|
2019-05-07 17:22:47 +08:00
|
|
|
},
|
2019-05-07 17:10:42 +08:00
|
|
|
{ key: '0-2', title: '0-3' },
|
|
|
|
];
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const App: React.FC = () => {
|
|
|
|
const [targetKeys, setTargetKeys] = useState<string[]>([]);
|
|
|
|
const onChange = (keys: string[]) => {
|
2020-08-22 17:55:09 +08:00
|
|
|
setTargetKeys(keys);
|
2019-05-07 17:22:47 +08:00
|
|
|
};
|
2020-08-22 17:55:09 +08:00
|
|
|
return <TreeTransfer dataSource={treeData} targetKeys={targetKeys} onChange={onChange} />;
|
|
|
|
};
|
2019-05-07 17:10:42 +08:00
|
|
|
|
2022-04-15 16:20:56 +08:00
|
|
|
export default App;
|
2019-05-07 17:22:47 +08:00
|
|
|
```
|
2019-05-07 17:10:42 +08:00
|
|
|
|
|
|
|
<style>
|
|
|
|
.tree-transfer .ant-transfer-list:first-child {
|
|
|
|
flex: none;
|
2020-08-22 17:55:09 +08:00
|
|
|
width: 50%;
|
2019-05-07 17:10:42 +08:00
|
|
|
}
|
|
|
|
</style>
|