2022-11-09 12:28:04 +08:00
|
|
|
import React, { useState } from 'react';
|
2023-09-07 10:15:58 +08:00
|
|
|
import { theme, Transfer, Tree } from 'antd';
|
2024-01-11 15:55:58 +08:00
|
|
|
import type { GetProp, TransferProps, TreeDataNode } from 'antd';
|
|
|
|
|
|
|
|
type TransferItem = GetProp<TransferProps, 'dataSource'>[number];
|
2022-11-09 12:28:04 +08:00
|
|
|
|
|
|
|
interface TreeTransferProps {
|
2024-01-11 15:55:58 +08:00
|
|
|
dataSource: TreeDataNode[];
|
2022-11-09 12:28:04 +08:00
|
|
|
targetKeys: string[];
|
2024-01-11 15:55:58 +08:00
|
|
|
onChange: TransferProps['onChange'];
|
2022-11-09 12:28:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Customize Table Transfer
|
2023-09-20 11:01:49 +08:00
|
|
|
const isChecked = (selectedKeys: React.Key[], eventKey: React.Key) =>
|
2022-11-09 12:28:04 +08:00
|
|
|
selectedKeys.includes(eventKey);
|
|
|
|
|
2024-01-11 15:55:58 +08:00
|
|
|
const generateTree = (treeNodes: TreeDataNode[] = [], checkedKeys: string[] = []): TreeDataNode[] =>
|
2022-11-09 12:28:04 +08:00
|
|
|
treeNodes.map(({ children, ...props }) => ({
|
|
|
|
...props,
|
|
|
|
disabled: checkedKeys.includes(props.key as string),
|
|
|
|
children: generateTree(children, checkedKeys),
|
|
|
|
}));
|
|
|
|
|
2023-09-07 10:15:58 +08:00
|
|
|
const TreeTransfer: React.FC<TreeTransferProps> = ({ dataSource, targetKeys, ...restProps }) => {
|
2022-12-13 17:35:48 +08:00
|
|
|
const { token } = theme.useToken();
|
|
|
|
|
2022-11-09 12:28:04 +08:00
|
|
|
const transferDataSource: TransferItem[] = [];
|
2024-01-11 15:55:58 +08:00
|
|
|
function flatten(list: TreeDataNode[] = []) {
|
2022-11-19 13:47:33 +08:00
|
|
|
list.forEach((item) => {
|
2022-11-09 12:28:04 +08:00
|
|
|
transferDataSource.push(item as TransferItem);
|
|
|
|
flatten(item.children);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
flatten(dataSource);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Transfer
|
|
|
|
{...restProps}
|
|
|
|
targetKeys={targetKeys}
|
|
|
|
dataSource={transferDataSource}
|
|
|
|
className="tree-transfer"
|
2022-11-19 13:47:33 +08:00
|
|
|
render={(item) => item.title!}
|
2022-11-09 12:28:04 +08:00
|
|
|
showSelectAll={false}
|
|
|
|
>
|
|
|
|
{({ direction, onItemSelect, selectedKeys }) => {
|
|
|
|
if (direction === 'left') {
|
|
|
|
const checkedKeys = [...selectedKeys, ...targetKeys];
|
|
|
|
return (
|
2022-12-13 17:35:48 +08:00
|
|
|
<div style={{ padding: token.paddingXS }}>
|
|
|
|
<Tree
|
|
|
|
blockNode
|
|
|
|
checkable
|
|
|
|
checkStrictly
|
|
|
|
defaultExpandAll
|
|
|
|
checkedKeys={checkedKeys}
|
|
|
|
treeData={generateTree(dataSource, targetKeys)}
|
|
|
|
onCheck={(_, { node: { key } }) => {
|
|
|
|
onItemSelect(key as string, !isChecked(checkedKeys, key));
|
|
|
|
}}
|
|
|
|
onSelect={(_, { node: { key } }) => {
|
|
|
|
onItemSelect(key as string, !isChecked(checkedKeys, key));
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
2022-11-09 12:28:04 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
</Transfer>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-01-11 15:55:58 +08:00
|
|
|
const treeData: TreeDataNode[] = [
|
2022-11-09 12:28:04 +08:00
|
|
|
{ key: '0-0', title: '0-0' },
|
|
|
|
{
|
|
|
|
key: '0-1',
|
|
|
|
title: '0-1',
|
|
|
|
children: [
|
|
|
|
{ key: '0-1-0', title: '0-1-0' },
|
|
|
|
{ key: '0-1-1', title: '0-1-1' },
|
|
|
|
],
|
|
|
|
},
|
2022-12-13 17:35:48 +08:00
|
|
|
{ key: '0-2', title: '0-2' },
|
|
|
|
{ key: '0-3', title: '0-3' },
|
|
|
|
{ key: '0-4', title: '0-4' },
|
2022-11-09 12:28:04 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
const App: React.FC = () => {
|
|
|
|
const [targetKeys, setTargetKeys] = useState<string[]>([]);
|
|
|
|
const onChange = (keys: string[]) => {
|
|
|
|
setTargetKeys(keys);
|
|
|
|
};
|
|
|
|
return <TreeTransfer dataSource={treeData} targetKeys={targetKeys} onChange={onChange} />;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default App;
|