mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-22 14:44:47 +08:00
14a1e6bd51
* feat: tsconfig enable strict * feat: add no-explicit-any * feat: strict * feat: as THEME * feat: 优化 keys 类型写法 * feat: demo remove any * feat: as number * feat: this any * feat: add eslint * feat: cascader * feat: props any * feat: remove any * feat: remove any * feat: any 提示错误 * feat: remove any * feat: add eslint * feat: 允许 T = any 存在 * feat: color funciton * feat: 恢复 lint * feat: merge master * feat: as ReactElement * feat: type
103 lines
2.8 KiB
TypeScript
103 lines
2.8 KiB
TypeScript
import React, { useState } from 'react';
|
|
import { theme, Transfer, Tree } from 'antd';
|
|
import type { GetProp, TransferProps, TreeDataNode } from 'antd';
|
|
|
|
type TransferItem = GetProp<TransferProps, 'dataSource'>[number];
|
|
|
|
interface TreeTransferProps {
|
|
dataSource: TreeDataNode[];
|
|
targetKeys: TransferProps['targetKeys'];
|
|
onChange: TransferProps['onChange'];
|
|
}
|
|
|
|
// Customize Table Transfer
|
|
const isChecked = (selectedKeys: React.Key[], eventKey: React.Key) =>
|
|
selectedKeys.includes(eventKey);
|
|
|
|
const generateTree = (
|
|
treeNodes: TreeDataNode[] = [],
|
|
checkedKeys: TreeTransferProps['targetKeys'] = [],
|
|
): TreeDataNode[] =>
|
|
treeNodes.map(({ children, ...props }) => ({
|
|
...props,
|
|
disabled: checkedKeys.includes(props.key as string),
|
|
children: generateTree(children, checkedKeys),
|
|
}));
|
|
|
|
const TreeTransfer: React.FC<TreeTransferProps> = ({
|
|
dataSource,
|
|
targetKeys = [],
|
|
...restProps
|
|
}) => {
|
|
const { token } = theme.useToken();
|
|
|
|
const transferDataSource: TransferItem[] = [];
|
|
function flatten(list: TreeDataNode[] = []) {
|
|
list.forEach((item) => {
|
|
transferDataSource.push(item as TransferItem);
|
|
flatten(item.children);
|
|
});
|
|
}
|
|
flatten(dataSource);
|
|
|
|
return (
|
|
<Transfer
|
|
{...restProps}
|
|
targetKeys={targetKeys}
|
|
dataSource={transferDataSource}
|
|
className="tree-transfer"
|
|
render={(item) => item.title!}
|
|
showSelectAll={false}
|
|
>
|
|
{({ direction, onItemSelect, selectedKeys }) => {
|
|
if (direction === 'left') {
|
|
const checkedKeys = [...selectedKeys, ...targetKeys];
|
|
return (
|
|
<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>
|
|
);
|
|
}
|
|
}}
|
|
</Transfer>
|
|
);
|
|
};
|
|
|
|
const treeData: TreeDataNode[] = [
|
|
{ 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' },
|
|
],
|
|
},
|
|
{ key: '0-2', title: '0-2' },
|
|
{ key: '0-3', title: '0-3' },
|
|
{ key: '0-4', title: '0-4' },
|
|
];
|
|
|
|
const App: React.FC = () => {
|
|
const [targetKeys, setTargetKeys] = useState<TreeTransferProps['targetKeys']>([]);
|
|
const onChange: TreeTransferProps['onChange'] = (keys) => {
|
|
setTargetKeys(keys);
|
|
};
|
|
return <TreeTransfer dataSource={treeData} targetKeys={targetKeys} onChange={onChange} />;
|
|
};
|
|
|
|
export default App;
|