mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-24 15:46:10 +08:00
134 lines
3.6 KiB
TypeScript
134 lines
3.6 KiB
TypeScript
import React, { useState } from 'react';
|
|
import { Flex, Switch, Table, Tag, Transfer } from 'antd';
|
|
import type { GetProp, TableColumnsType, TableProps, TransferProps } from 'antd';
|
|
|
|
type TransferItem = GetProp<TransferProps, 'dataSource'>[number];
|
|
type TableRowSelection<T extends object> = TableProps<T>['rowSelection'];
|
|
|
|
interface DataType {
|
|
key: string;
|
|
title: string;
|
|
description: string;
|
|
tag: string;
|
|
}
|
|
|
|
interface TableTransferProps extends TransferProps<TransferItem> {
|
|
dataSource: DataType[];
|
|
leftColumns: TableColumnsType<DataType>;
|
|
rightColumns: TableColumnsType<DataType>;
|
|
}
|
|
|
|
// Customize Table Transfer
|
|
const TableTransfer: React.FC<TableTransferProps> = (props) => {
|
|
const { leftColumns, rightColumns, ...restProps } = props;
|
|
return (
|
|
<Transfer style={{ width: '100%' }} {...restProps}>
|
|
{({
|
|
direction,
|
|
filteredItems,
|
|
onItemSelect,
|
|
onItemSelectAll,
|
|
selectedKeys: listSelectedKeys,
|
|
disabled: listDisabled,
|
|
}) => {
|
|
const columns = direction === 'left' ? leftColumns : rightColumns;
|
|
const rowSelection: TableRowSelection<TransferItem> = {
|
|
getCheckboxProps: () => ({ disabled: listDisabled }),
|
|
onChange(selectedRowKeys) {
|
|
onItemSelectAll(selectedRowKeys, 'replace');
|
|
},
|
|
selectedRowKeys: listSelectedKeys,
|
|
selections: [Table.SELECTION_ALL, Table.SELECTION_INVERT, Table.SELECTION_NONE],
|
|
};
|
|
|
|
return (
|
|
<Table
|
|
rowSelection={rowSelection}
|
|
columns={columns}
|
|
dataSource={filteredItems}
|
|
size="small"
|
|
style={{ pointerEvents: listDisabled ? 'none' : undefined }}
|
|
onRow={({ key, disabled: itemDisabled }) => ({
|
|
onClick: () => {
|
|
if (itemDisabled || listDisabled) {
|
|
return;
|
|
}
|
|
onItemSelect(key, !listSelectedKeys.includes(key));
|
|
},
|
|
})}
|
|
/>
|
|
);
|
|
}}
|
|
</Transfer>
|
|
);
|
|
};
|
|
|
|
const mockTags = ['cat', 'dog', 'bird'];
|
|
|
|
const mockData = Array.from({ length: 20 }).map<DataType>((_, i) => ({
|
|
key: i.toString(),
|
|
title: `content${i + 1}`,
|
|
description: `description of content${i + 1}`,
|
|
tag: mockTags[i % 3],
|
|
}));
|
|
|
|
const columns: TableColumnsType<DataType> = [
|
|
{
|
|
dataIndex: 'title',
|
|
title: 'Name',
|
|
},
|
|
{
|
|
dataIndex: 'tag',
|
|
title: 'Tag',
|
|
render: (tag: string) => (
|
|
<Tag style={{ marginInlineEnd: 0 }} color="cyan">
|
|
{tag.toUpperCase()}
|
|
</Tag>
|
|
),
|
|
},
|
|
{
|
|
dataIndex: 'description',
|
|
title: 'Description',
|
|
},
|
|
];
|
|
|
|
const filterOption = (input: string, item: DataType) =>
|
|
item.title?.includes(input) || item.tag?.includes(input);
|
|
|
|
const App: React.FC = () => {
|
|
const [targetKeys, setTargetKeys] = useState<TransferProps['targetKeys']>([]);
|
|
const [disabled, setDisabled] = useState(false);
|
|
|
|
const onChange: TableTransferProps['onChange'] = (nextTargetKeys) => {
|
|
setTargetKeys(nextTargetKeys);
|
|
};
|
|
|
|
const toggleDisabled = (checked: boolean) => {
|
|
setDisabled(checked);
|
|
};
|
|
|
|
return (
|
|
<Flex align="start" gap="middle" vertical>
|
|
<TableTransfer
|
|
dataSource={mockData}
|
|
targetKeys={targetKeys}
|
|
disabled={disabled}
|
|
showSearch
|
|
showSelectAll={false}
|
|
onChange={onChange}
|
|
filterOption={filterOption}
|
|
leftColumns={columns}
|
|
rightColumns={columns}
|
|
/>
|
|
<Switch
|
|
unCheckedChildren="disabled"
|
|
checkedChildren="disabled"
|
|
checked={disabled}
|
|
onChange={toggleDisabled}
|
|
/>
|
|
</Flex>
|
|
);
|
|
};
|
|
|
|
export default App;
|