2019-05-07 17:10:42 +08:00
|
|
|
---
|
|
|
|
order: 5
|
|
|
|
title:
|
|
|
|
zh-CN: 表格穿梭框
|
|
|
|
en-US: Table Transfer
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
使用 Table 组件作为自定义渲染列表。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
|
|
|
Customize render list with Table component.
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
```tsx
|
2022-05-21 22:14:15 +08:00
|
|
|
import { Switch, Table, Tag, Transfer } from 'antd';
|
2022-05-19 09:46:26 +08:00
|
|
|
import type { ColumnsType, TableRowSelection } from 'antd/es/table/interface';
|
2022-05-21 22:14:15 +08:00
|
|
|
import type { TransferItem, TransferProps } from 'antd/es/transfer';
|
2019-05-07 17:10:42 +08:00
|
|
|
import difference from 'lodash/difference';
|
2022-05-21 22:14:15 +08:00
|
|
|
import React, { useState } from 'react';
|
2022-05-19 09:46:26 +08:00
|
|
|
|
|
|
|
interface RecordType {
|
|
|
|
key: string;
|
|
|
|
title: string;
|
|
|
|
description: string;
|
|
|
|
disabled: boolean;
|
|
|
|
tag: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface DataType {
|
|
|
|
title: string;
|
|
|
|
tag: string;
|
|
|
|
description: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface TableTransferProps extends TransferProps<TransferItem> {
|
|
|
|
dataSource: DataType[];
|
|
|
|
leftColumns: ColumnsType<DataType>;
|
|
|
|
rightColumns: ColumnsType<DataType>;
|
|
|
|
}
|
2019-05-07 17:10:42 +08:00
|
|
|
|
|
|
|
// Customize Table Transfer
|
2022-05-19 09:46:26 +08:00
|
|
|
const TableTransfer = ({ leftColumns, rightColumns, ...restProps }: TableTransferProps) => (
|
2021-08-10 19:20:53 +08:00
|
|
|
<Transfer {...restProps}>
|
2019-05-07 17:10:42 +08:00
|
|
|
{({
|
2019-05-07 17:22:47 +08:00
|
|
|
direction,
|
|
|
|
filteredItems,
|
|
|
|
onItemSelectAll,
|
|
|
|
onItemSelect,
|
|
|
|
selectedKeys: listSelectedKeys,
|
|
|
|
disabled: listDisabled,
|
2019-05-07 17:10:42 +08:00
|
|
|
}) => {
|
|
|
|
const columns = direction === 'left' ? leftColumns : rightColumns;
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const rowSelection: TableRowSelection<TransferItem> = {
|
2019-05-07 17:22:47 +08:00
|
|
|
getCheckboxProps: item => ({ disabled: listDisabled || item.disabled }),
|
2019-05-07 17:10:42 +08:00
|
|
|
onSelectAll(selected, selectedRows) {
|
2019-05-07 17:22:47 +08:00
|
|
|
const treeSelectedKeys = selectedRows
|
|
|
|
.filter(item => !item.disabled)
|
|
|
|
.map(({ key }) => key);
|
|
|
|
const diffKeys = selected
|
|
|
|
? difference(treeSelectedKeys, listSelectedKeys)
|
|
|
|
: difference(listSelectedKeys, treeSelectedKeys);
|
2022-05-19 09:46:26 +08:00
|
|
|
onItemSelectAll(diffKeys as string[], selected);
|
2019-05-07 17:10:42 +08:00
|
|
|
},
|
|
|
|
onSelect({ key }, selected) {
|
2022-05-19 09:46:26 +08:00
|
|
|
onItemSelect(key as string, selected);
|
2019-05-07 17:10:42 +08:00
|
|
|
},
|
|
|
|
selectedRowKeys: listSelectedKeys,
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Table
|
|
|
|
rowSelection={rowSelection}
|
|
|
|
columns={columns}
|
|
|
|
dataSource={filteredItems}
|
|
|
|
size="small"
|
2022-05-19 09:46:26 +08:00
|
|
|
style={{ pointerEvents: listDisabled ? 'none' : undefined }}
|
2019-05-07 17:10:42 +08:00
|
|
|
onRow={({ key, disabled: itemDisabled }) => ({
|
|
|
|
onClick: () => {
|
|
|
|
if (itemDisabled || listDisabled) return;
|
2022-05-19 09:46:26 +08:00
|
|
|
onItemSelect(key as string, !listSelectedKeys.includes(key as string));
|
2019-05-07 17:10:42 +08:00
|
|
|
},
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</Transfer>
|
|
|
|
);
|
|
|
|
|
2019-05-07 17:22:47 +08:00
|
|
|
const mockTags = ['cat', 'dog', 'bird'];
|
2019-05-07 17:10:42 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const mockData: RecordType[] = Array.from({ length: 20 }).map((_, i) => ({
|
|
|
|
key: i.toString(),
|
|
|
|
title: `content${i + 1}`,
|
|
|
|
description: `description of content${i + 1}`,
|
|
|
|
disabled: i % 4 === 0,
|
|
|
|
tag: mockTags[i % 3],
|
|
|
|
}));
|
2019-05-07 17:10:42 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const originTargetKeys = mockData.filter(item => Number(item.key) % 3 > 1).map(item => item.key);
|
2019-05-07 17:10:42 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const leftTableColumns: ColumnsType<DataType> = [
|
2019-05-07 17:10:42 +08:00
|
|
|
{
|
|
|
|
dataIndex: 'title',
|
|
|
|
title: 'Name',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
dataIndex: 'tag',
|
|
|
|
title: 'Tag',
|
|
|
|
render: tag => <Tag>{tag}</Tag>,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
dataIndex: 'description',
|
|
|
|
title: 'Description',
|
|
|
|
},
|
|
|
|
];
|
2022-05-19 09:46:26 +08:00
|
|
|
|
|
|
|
const rightTableColumns: ColumnsType<Pick<DataType, 'title'>> = [
|
2019-05-07 17:10:42 +08:00
|
|
|
{
|
|
|
|
dataIndex: 'title',
|
|
|
|
title: 'Name',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const App: React.FC = () => {
|
|
|
|
const [targetKeys, setTargetKeys] = useState<string[]>(originTargetKeys);
|
|
|
|
const [disabled, setDisabled] = useState(false);
|
|
|
|
const [showSearch, setShowSearch] = useState(false);
|
2019-05-07 17:10:42 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const onChange = (nextTargetKeys: string[]) => {
|
|
|
|
setTargetKeys(nextTargetKeys);
|
2019-05-07 17:22:47 +08:00
|
|
|
};
|
2019-05-07 17:10:42 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const triggerDisable = (checked: boolean) => {
|
|
|
|
setDisabled(checked);
|
2019-05-07 17:10:42 +08:00
|
|
|
};
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const triggerShowSearch = (checked: boolean) => {
|
|
|
|
setShowSearch(checked);
|
2019-05-07 17:10:42 +08:00
|
|
|
};
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<TableTransfer
|
|
|
|
dataSource={mockData}
|
|
|
|
targetKeys={targetKeys}
|
|
|
|
disabled={disabled}
|
|
|
|
showSearch={showSearch}
|
|
|
|
onChange={onChange}
|
|
|
|
filterOption={(inputValue, item) =>
|
|
|
|
item.title!.indexOf(inputValue) !== -1 || item.tag.indexOf(inputValue) !== -1
|
|
|
|
}
|
|
|
|
leftColumns={leftTableColumns}
|
|
|
|
rightColumns={rightTableColumns}
|
|
|
|
/>
|
|
|
|
<Switch
|
|
|
|
unCheckedChildren="disabled"
|
|
|
|
checkedChildren="disabled"
|
|
|
|
checked={disabled}
|
|
|
|
onChange={triggerDisable}
|
|
|
|
style={{ marginTop: 16 }}
|
|
|
|
/>
|
|
|
|
<Switch
|
|
|
|
unCheckedChildren="showSearch"
|
|
|
|
checkedChildren="showSearch"
|
|
|
|
checked={showSearch}
|
|
|
|
onChange={triggerShowSearch}
|
|
|
|
style={{ marginTop: 16 }}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
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
|
|
|
```
|
2020-01-02 15:47:30 +08:00
|
|
|
|
|
|
|
```css
|
|
|
|
#components-transfer-demo-table-transfer .ant-table td {
|
|
|
|
background: transparent;
|
|
|
|
}
|
|
|
|
```
|