Transfer
通过在两个托管列表之间传输来选择记录。
何时使用
转移通过在两个列表之间转移来选择一组参与者、权限或任务。用户首先标记条目,然后一键传输它们。两个列表为搜索、描述和比较可用的和已选择的条目留出了空间。对于紧凑的标记字段,请使用 Select。
在此页面上,广泛的列表在卡片内滚动。标头和操作标签应解释换行的含义,例如“可用 → 命令”。
传输 CSS 前缀 显示全局 ConfigProvider.prefixCls 和本地 prefixCls,无需重置状态。 Transfer 面板使用本地前缀,而 Checkbox、Input、Button 和 Pagination 使用独立的公共前缀。搜索、标记和传输的条目都会被保存。
结果与选择
targetKeys 包含选择的结果。 selectedKeys 包含两侧临时标记的行。复选框本身不会向结果添加条目。
| 问题 | 设置 | 示例 |
|---|---|---|
| 接受模型转移 | targetKeys、onChange |
列表之间转移 |
| 管理临时分配 | selectedKeys、onSelectChange |
列表页脚 |
| 删除后退按钮 | oneWay |
单程 |
| 将结果存储在 Form | 中form.setFieldValue |
在表单中选择参与者 |
结果始终由应用程序控制:传递 targetKeys 并在 onChange 中分配一个新数组。该处理程序还接收 direction 和 moveKeys - 操作的方向和移动记录的键。不分配 targetKeys,右侧不会改变。没有单独的 defaultTargetKeys 或 bind:targetKeys。
选择可以保留在本地或通过 selectedKeys 进行控制。 onSelectChange 报告两个数组:源列表标记和目标列表标记。指导示例将它们组合成一个 selectedKeys。清除选择不会清除结果,并且外部 reset 结果不会替换各个标记状态。
在 oneWay 中,通过右侧的删除按钮返回选中的记录。它从 targetKeys 中排除该键,并将该条目存储在 dataSource 中。disabled 块完全传输或针对单个条目传输。
数据、搜索和页面
每条记录都需要一个稳定、唯一的 key。如果 ID 被称为其他名称,则传递 rowKey。 参与者示例 使用数字 id,而不将模型转换为字符串。排序或更新数据时不要使用数组位置作为标识符。
render 是常规录制功能。它返回文本、Svelte 片段或带有 label 和文本 value 的对象。在这样的对象中,value 描述要显示的元素,并且不会替换其 key。 Snippet 接收一个条目作为参数;示例参与者显示姓名和角色。
showSearch 向每一侧添加单独的搜索。 settings对象指定 placeholder 和初始 defaultValue;后续请求报告 onSearch(direction, value)。 filterOption 决定显示哪些本地条目。 搜索示例 匹配标题和描述。
分页将本地列表分成页面; pageSize 指定它们的大小。 页面示例 切换页面时保留标记。删除的请求、服务器页码和响应处理仍保留在应用程序中。将已选择的键的记录存储在 dataSource 中,以便目标列表可以显示其签名。
自己的列表并形成
可以分别使用 Transfer.Search、Transfer.List 和 Transfer.Operation 构建自定义界面。在独立组件示例中,应用管理搜索文本、勾选项和穿梭结果。
children 接受带有 TransferListBodyProps 的 Svelte 代码段:filteredItems、selectedKeys、disabled、onItemSelect 和 onItemSelectAll。 Table Transfer 将 selectedRowKeys/onSelectionChange 表绑定到这些处理程序。 Tree Transfer 链接树的独立复选框。请注意 disabled 并传递真实的行键而不是整个原始数组。
children 内置 pagination 不起作用。应用程序组织其自身主体的页面和导航。对于标记的组替换,提供了 onItemSelectAll(keys, 'replace')。
你的行动 接收 TransferActionInfo。将 info.disabled 和 info.onClick 传递给您的按钮; info.icon 已经考虑了界面的方向。footer 也接受片段。这些函数不会被 React 元素或 cloneElement 取代。
classNames styles 并支持公共槽和单独的 source/target; styles 接受 CSS 字符串和对象。 ConfigProvider.transfer继承了design和 selectionsIcon。 theme.components.Transfer 组件设计令牌(design tokens)应用于列表;本地设置显示在 语义示例 和 令牌部分 中。
在表单中,结果存储在 form.values 中,onChange 调用 form.setFieldValue。required 规则检查目标集。 重置表格分别恢复 initialValues 和清除 selectedKeys。可通过键盘访问本机复选框和按钮; bind:nativeElement 返回根 div; Transfer 没有单独的 focus/blur 方法。
示例
列表之间转移
行选择和转移键是不同的状态。 targetKeys 始终由应用程序控制; onChange 报告方向和传输的键。
<script lang="ts">
import { Transfer, type TransferDirection, type TransferKey } from '@woodger/antd';
const dataSource = Array.from({ length: 8 }, (_, index) => ({
key: String(index + 1),
title: `段落 ${index + 1}`,
description: `描述 ${index + 1}`,
disabled: index === 3
}));
let targetKeys = $state<TransferKey[]>(['2']);
let lastMove = $state(("无转乘服务"));
function handleChange(next: TransferKey[], direction: TransferDirection, moved: TransferKey[]) {
targetKeys = next;
lastMove = ("" + String((direction)) + ": " + String((moved.join(', '))) + "");
}
</script>
<style>
.lists {
max-width: 100%;
overflow-x: auto;
}
.lists:focus-visible {
outline: 2px solid var(--ant-color-primary);
outline-offset: 2px;
}
output { overflow-wrap: anywhere; }
</style>
<div
class="lists"
role="region"
aria-label={("列表:列表之间传输")}
tabindex="0"
>
<Transfer dataSource={(dataSource)} {targetKeys} titles={([
"可用的",
"已选择"
])} actions={(["添加", "返回"])}
render={item => item.title} onChange={handleChange} />
</div>
<p><output aria-label={("转账结果")}>{(targetKeys.join(', ')
|| ("空的"))}; {lastMove}</output></p>
- 段落 2
- 段落 3
单程
oneWay 留下一个转接按钮。使用单独的按钮删除右侧列表中的元素。
<script lang="ts">
import { Transfer, type TransferKey } from '@woodger/antd';
const dataSource = Array.from({ length: 8 }, (_, index) => ({
key: String(index + 1),
title: `段落 ${index + 1}`,
description: `描述 ${index + 1}`,
disabled: index === 3
}));
let targetKeys = $state<TransferKey[]>(['2', '3']);
</script>
<style>
.lists {
max-width: 100%;
overflow-x: auto;
}
.lists:focus-visible {
outline: 2px solid var(--ant-color-primary);
outline-offset: 2px;
}
output { overflow-wrap: anywhere; }
</style>
<div class="lists" role="region" aria-label={("列表:一种方式")} tabindex="0">
<Transfer dataSource={(dataSource)} {targetKeys} oneWay actions={([
"添加"
])} titles={(["可用的", "已选择"])}
render={item => item.title} onChange={next => { targetKeys = next; }} />
</div>
<p><output aria-label={("单向选择")}>{targetKeys.join(', ')}</output></p>
独立检索
每个列表都存储自己的查询。 filterOption 按名称和描述进行搜索; onSearch 报告边和字符串。
<script lang="ts">
import { Transfer, type TransferKey } from '@woodger/antd';
const dataSource = Array.from({ length: 8 }, (_, index) => ({
key: String(index + 1),
title: `段落 ${index + 1}`,
description: `描述 ${index + 1}`,
disabled: index === 3
}));
let targetKeys = $state<TransferKey[]>(['2', '5']);
let query = $state('');
</script>
<style>
.lists {
max-width: 100%;
overflow-x: auto;
}
.lists:focus-visible {
outline: 2px solid var(--ant-color-primary);
outline-offset: 2px;
}
output { overflow-wrap: anywhere; }
</style>
<div
class="lists"
role="region"
aria-label={("列表:独立搜索")}
tabindex="0"
>
<Transfer
dataSource={(dataSource)}
{targetKeys}
showSearch={{ placeholder: "标题或描述" }}
titles={(["可用的", "已选择"])}
actions={(["添加", "返回"])}
render={item => item.title}
filterOption={(search, item) => `${item.title} ${item.description}`.toLowerCase().includes(
search.toLowerCase()
)}
onSearch={(direction, search) => { query = ("" + String((direction)) + ": " + String((search)) + ""); }}
onChange={next => { targetKeys = next; }}
/>
</div>
<p><output
aria-label={("搜索转移")}
>{(query || ("未指定搜索"))}</output></p>
列表页脚
footer 接收列表和 direction 参数。该按钮取消选择其一侧;一般 disabled 阻止操作。
<script lang="ts">
import {
Button,
Transfer,
type TransferDirection,
type TransferKey,
type TransferListProps
} from '@woodger/antd';
const dataSource = Array.from({ length: 8 }, (_, index) => ({
key: String(index + 1),
title: `段落 ${index + 1}`,
description: `描述 ${index + 1}`,
disabled: index === 3
}));
let targetKeys = $state<TransferKey[]>(['2']);
let disabled = $state(false);
type Row = typeof dataSource[number];
</script>
<style>
.lists {
max-width: 100%;
overflow-x: auto;
}
.lists:focus-visible {
outline: 2px solid var(--ant-color-primary);
outline-offset: 2px;
}
.footer { padding: 8px 12px; }
</style>
<p>
<label><input type="checkbox" bind:checked={disabled} />{(" 阻止两个列表")}</label>
</p>
{#snippet footer(list: TransferListProps<Row>, info: { direction: TransferDirection })}
<div class="footer"><Button
size="small"
disabled={list.disabled}
onclick={() => list.onItemSelectAll(list.checkedKeys, false)}
>{("取消选择 ")}{(info.direction)}</Button></div>
{/snippet}
<div class="lists" role="region" aria-label={("列表:列表页脚")} tabindex="0">
<Transfer
dataSource={(dataSource)}
{targetKeys}
{disabled}
{footer}
titles={(["可用的", "已选择"])}
render={item => item.title}
actions={(["添加", "返回"])}
onChange={next => { targetKeys = next; }}
/>
</div>
您的数据和签名
rowKey 读取数字 id。render 返回文本元数据的 label 片段和 value;键仍然是数字。
<script lang="ts">
import { Transfer, type TransferKey } from '@woodger/antd';
interface Person { id: number; name: string; role: string; }
const dataSource: Person[] = [
{ id: 10, name: "安娜", role: "开发" },
{ id: 20, name: "鲍里斯", role: "文档" }
];
let targetKeys = $state<TransferKey[]>([]);
</script>
<style>
.lists {
max-width: 100%;
overflow-x: auto;
}
.lists:focus-visible {
outline: 2px solid var(--ant-color-primary);
outline-offset: 2px;
}
output { overflow-wrap: anywhere; }
</style>
{#snippet label(person: Person)}
<strong>
{person.name}
</strong>
<small>
{(person.role)}
</small>
{/snippet}
<div
class="lists"
role="region"
aria-label={("列表:您的数据和签名")}
tabindex="0"
>
<Transfer
dataSource={(dataSource)}
{targetKeys}
rowKey={person => person.id}
titles={(["参加者", "团队"])}
actions={(["致团队", "返回"])}
render={person => ({ label, value: person.name })}
onChange={next => { targetKeys = next; }}
/>
</div>
<p><output aria-label={("会员钥匙")}>{JSON.stringify(targetKeys)}</output></p>
<script lang="ts">
import { Button, Transfer, type TransferActionInfo, type TransferKey } from '@woodger/antd';
const dataSource = Array.from({ length: 8 }, (_, index) => ({
key: String(index + 1),
title: `段落 ${index + 1}`,
description: `描述 ${index + 1}`,
disabled: index === 3
}));
let targetKeys = $state<TransferKey[]>([]);
</script>
<style>
.lists {
max-width: 100%;
overflow-x: auto;
}
.lists:focus-visible {
outline: 2px solid var(--ant-color-primary);
outline-offset: 2px;
}
output { overflow-wrap: anywhere; }
</style>
{#snippet action(info: TransferActionInfo)}
<Button
size="small"
disabled={info.disabled}
onclick={info.onClick}
aria-label={(info.direction === 'right' ? "向右发送" : "向左返回")}
>{@render info.icon()}</Button>
{/snippet}
<div
class="lists"
role="region"
aria-label={("列表:自定义传输按钮")}
tabindex="0"
>
<Transfer dataSource={(dataSource)} {targetKeys} actions={([
action,
action
])} titles={(["可用的", "已选择"])}
render={item => item.title} onChange={next => { targetKeys = next; }} />
</div>
<p><output aria-label={("用你的行动来选择")}>{(targetKeys.join(', ')
|| ("空的"))}</output></p>
Transfer 独立组件
Transfer.Search 筛选可用记录,Transfer.List 显示记录,Transfer.Operation 移动勾选项。应用管理两组键。
<script lang="ts">
import { Transfer, type TransferKey } from '@woodger/antd';
const items = ['Button', 'Input', 'Select', 'Tabs'].map(title => ({ key: title, title }));
const border = '1px solid var(--ant-color-border, #d9d9d9)';
let query = $state('');
let checkedKeys = $state<TransferKey[]>([]);
let targetKeys = $state<TransferKey[]>([]);
const available = $derived(items.filter(item =>
!targetKeys.includes(item.key) && item.title.toLowerCase().includes(query.trim().toLowerCase())
));
function handleFilter(event: Event & { currentTarget: HTMLInputElement }) {
query = event.currentTarget.value;
}
function handleClear() {
query = '';
}
function selectItem(key: TransferKey, selected: boolean) {
checkedKeys = selected
? [...checkedKeys, key]
: checkedKeys.filter(checked => checked !== key);
}
function move() {
targetKeys = [...targetKeys, ...checkedKeys];
checkedKeys = [];
}
</script>
<div class="example">
<Transfer.Search
aria-label={("查找组件")}
placeholder={("查找组件")}
value={query}
onChange={handleFilter}
{handleClear}
/>
<Transfer.List
prefixCls="available"
direction="left"
dataSource={available}
{checkedKeys}
titleText={("可用的")}
notFoundContent={("无数据")}
showSelectAll={false}
classNames={{}}
styles={{
section: {
border,
borderRadius: '8px',
minHeight: '172px'
},
header: {
display: 'flex',
flexDirection: 'row-reverse',
justifyContent: 'space-between',
padding: '8px 12px',
borderBottom: border
},
list: {
margin: 0,
padding: 0,
listStyle: 'none'
},
item: {
display: 'flex',
alignItems: 'center',
gap: '8px',
padding: '6px 12px'
}
}}
{handleFilter}
{handleClear}
onItemSelect={selectItem}
onItemSelectAll={() => {}}
onScroll={() => {}}
render={item => item.title}
/>
<Transfer.Operation
oneWay
actions={[("添加")]}
rightActive={checkedKeys.length > 0}
moveToRight={move}
/>
<p><output aria-label={("已选择")}>{targetKeys.join(', ') || ("空的")}</output></p>
</div>
<style>
.example {
display: grid;
gap: 12px;
}
p {
margin: 0;
}
</style>
页面列表
pagination 限制了每一侧的可见线。搜索并滑动保存选定的键。
<script lang="ts">
import { Transfer, type TransferKey } from '@woodger/antd';
const dataSource = Array.from({ length: 8 }, (_, index) => ({
key: String(index + 1),
title: `段落 ${index + 1}`,
description: `描述 ${index + 1}`,
disabled: index === 3
}));
let targetKeys = $state<TransferKey[]>([]);
</script>
<style>
.lists {
max-width: 100%;
overflow-x: auto;
}
.lists:focus-visible {
outline: 2px solid var(--ant-color-primary);
outline-offset: 2px;
}
output { overflow-wrap: anywhere; }
</style>
<div
class="lists"
role="region"
aria-label={("列表:页面列表")}
tabindex="0"
>
<Transfer dataSource={(dataSource)} {targetKeys} pagination={({
pageSize: 3,
simple: false,
showSizeChanger: true
})}
titles={(["可用的", "已选择"])} actions={([
"添加",
"返回"
])} render={item => item.title}
onChange={next => { targetKeys = next; }} />
</div>
<p><output aria-label={("页面选择")}>{(targetKeys.join(', ')
|| ("空的"))}</output></p>
姓名 | 描述 | |
|---|---|---|
| 段落 1 | 描述 1 | |
| 段落 2 | 描述 2 | |
| 段落 3 | 描述 3 | |
| 段落 4 | 描述 4 | |
| 段落 5 | 描述 5 | |
| 段落 6 | 描述 6 | |
| 段落 7 | 描述 7 | |
| 段落 8 | 描述 8 |
姓名 | 描述 | |
|---|---|---|
无数据 | ||
带桌子转移
children 用公共表替换列表主体。 selectedRowKeys 和 onSelectionChange 与 onItemSelectAll 关联;pagination 传输不适用于 children。
<script lang="ts">
import {
Table,
Transfer,
type TableColumn,
type TransferKey,
type TransferListBodyProps
} from '@woodger/antd';
const dataSource = Array.from({ length: 8 }, (_, index) => ({
key: String(index + 1),
title: `段落 ${index + 1}`,
description: `描述 ${index + 1}`,
disabled: index === 3
}));
interface Row { key: string; title: string; description: string; disabled: boolean; }
const columns: TableColumn<Row>[] = [
{ key: 'title', title: "姓名", dataIndex: 'title' },
{ key: 'description', title: "描述", dataIndex: 'description' }
];
let targetKeys = $state<TransferKey[]>([]);
</script>
<style>
.lists {
max-width: 100%;
overflow-x: auto;
}
.lists:focus-visible {
outline: 2px solid var(--ant-color-primary);
outline-offset: 2px;
}
output { overflow-wrap: anywhere; }
</style>
<div
class="lists"
role="region"
aria-label={("列表:与表格一起传输")}
tabindex="0"
>
<Transfer dataSource={(dataSource)} {targetKeys} titles={([
"可用的",
"已选择"
])} showSelectAll={false}
styles={{ section: `
width: 360px;
height: 300px;
` }} actions={([
"添加",
"返回"
])}
onChange={next => { targetKeys = next; }}>
{#snippet children(list: TransferListBodyProps<Row>)}
<Table
dataSource={(list.filteredItems)}
columns={(columns)}
rowKey="key"
pagination={(false)}
selectedRowKeys={list.selectedKeys.map(String)}
disabled={list.disabled}
rowSelection={{
getCheckboxProps: row => ({
disabled: row.disabled,
'aria-label': row.title
})
}}
onSelectionChange={keys => list.onItemSelectAll(keys, 'replace')}
/>
{/snippet}
</Transfer>
</div>
<p><output aria-label={("餐桌选择")}>{(targetKeys.join(', ')
|| ("空的"))}</output></p>
从目录中选择项目
<script lang="ts">
import {
Checkbox,
Transfer,
Tree,
type TransferKey,
type TransferListBodyProps,
type TreeDataNode
} from '@woodger/antd';
const dataSource = Array.from({ length: 8 }, (_, index) => ({
key: String(index + 1),
title: `段落 ${index + 1}`,
description: `描述 ${index + 1}`,
disabled: index === 3
}));
interface Row { key: string; title: string; description: string; disabled: boolean; }
let targetKeys = $state<TransferKey[]>([]);
function asTree(records: readonly Row[]): TreeDataNode[] {
return [{ key: 'group', title: "目录", checkable: false, selectable: false,
children: records.map(row => ({ key: row.key, title: row.title, disabled: row.disabled }))
}];
}
</script>
<style>
.lists {
max-width: 100%;
overflow-x: auto;
}
.lists:focus-visible {
outline: 2px solid var(--ant-color-primary);
outline-offset: 2px;
}
output { overflow-wrap: anywhere; }
.target {
display: grid;
gap: 12px;
padding: 12px;
}
</style>
<div
class="lists"
role="region"
aria-label={("清单:用木头携带")}
tabindex="0"
>
<Transfer dataSource={(dataSource)} {targetKeys} titles={([
"目录",
"已选择"
])} showSelectAll={false}
styles={{ section: `
width: 260px;
height: 280px;
` }} actions={([
"添加",
"返回"
])}
onChange={next => { targetKeys = next; }}>
{#snippet children(list: TransferListBodyProps<Row>)}
{#if list.direction === 'left'}
<Tree aria-label={("可用项目树")} treeData={asTree(
list.filteredItems
)} checkable checkStrictly
defaultExpandAll checkedKeys={list.selectedKeys.map(String)} disabled={list.disabled}
onCheck={(_keys, info) => list.onItemSelect(info.node.key, info.checked)} />
{:else}
<div class="target">
{#each list.filteredItems as row (row.key)}
<Checkbox checked={list.selectedKeys.includes(row.key)} disabled={list.disabled
|| row.disabled}
onChange={event => list.onItemSelect(row.key, event.target.checked)}>{(row.title)}</Checkbox>
{:else}<p>{("从目录中选择项目")}</p>{/each}
</div>
{/if}
{/snippet}
</Transfer>
</div>
<p><output aria-label={("木材选择")}>{(targetKeys.join(', ')
|| ("空的"))}</output></p>
<script lang="ts">
import { Transfer, type TransferKey } from '@woodger/antd';
const dataSource = Array.from({ length: 8 }, (_, index) => ({
key: String(index + 1),
title: `段落 ${index + 1}`,
description: `描述 ${index + 1}`,
disabled: index === 3
}));
let targetKeys = $state<TransferKey[]>(['2']);
let warning = $state(true);
</script>
<style>
.lists {
max-width: 100%;
overflow-x: auto;
}
.lists:focus-visible {
outline: 2px solid var(--ant-color-primary);
outline-offset: 2px;
}
</style>
<div
class="lists"
role="region"
aria-label={("列表:检查状态")}
tabindex="0"
>
<Transfer dataSource={(dataSource)} {targetKeys} status="error" titles={(["错误", "已选择"])}
render={item => item.title} onChange={next => { targetKeys = next; }} />
</div>
<p><label><input
type="checkbox"
bind:checked={warning}
/>{(" 显示警告")}</label></p>
<div
class="lists"
role="region"
aria-label={("列表:检查状态")}
tabindex="0"
>
<Transfer
dataSource={(dataSource)}
{targetKeys}
status={warning ? 'warning' : ''}
titles={(["考试", "已选择"])}
render={item => item.title}
onChange={next => { targetKeys = next; }}
/>
</div>
Semantic slots
styles 和 classNames 支持公共槽和单独的 source/target。该函数接收 info.props。
<script lang="ts">
import { Transfer, type TransferKey } from '@woodger/antd';
const dataSource = Array.from({ length: 8 }, (_, index) => ({
key: String(index + 1),
title: `段落 ${index + 1}`,
description: `描述 ${index + 1}`,
disabled: index === 3
}));
let targetKeys = $state<TransferKey[]>(['2']);
</script>
<style>
.lists {
max-width: 100%;
overflow-x: auto;
}
.lists:focus-visible {
outline: 2px solid var(--ant-color-primary);
outline-offset: 2px;
}
</style>
<div class="lists" role="region" aria-label={("列表:语义槽")} tabindex="0">
<Transfer dataSource={(dataSource)} {targetKeys} titles={([
"来源",
"目的"
])} actions={(["添加", "返回"])}
render={item => item.title} classNames={{ title: 'documented-transfer-title' }}
styles={({ props }) => ({
section: 'border-style:dashed',
itemContent: 'font-weight:600',
source: { header: 'background:var(--ant-color-fill-secondary)' },
target: { header: props.disabled ? '' : 'background:var(--ant-color-primary-bg)' }
})} onChange={next => { targetKeys = next; }} />
</div>
传输受阻
disabled 块选择和操作;无法访问的线路不会与可用线路一起转移。
<script lang="ts">
import { Transfer, type TransferKey } from '@woodger/antd';
const dataSource = Array.from({ length: 8 }, (_, index) => ({
key: String(index + 1),
title: `段落 ${index + 1}`,
description: `描述 ${index + 1}`,
disabled: index === 3
}));
let targetKeys = $state<TransferKey[]>(['2']);
</script>
<style>
.lists {
max-width: 100%;
overflow-x: auto;
}
.lists:focus-visible {
outline: 2px solid var(--ant-color-primary);
outline-offset: 2px;
}
</style>
<div
class="lists"
role="region"
aria-label={("列表:被阻止的传输")}
tabindex="0"
>
<Transfer dataSource={(dataSource)} {targetKeys} disabled titles={([
"可用的",
"已选择"
])}
render={item => item.title} onChange={next => { targetKeys = next; }} />
</div>
<script lang="ts">
import { Button, Form, FormItem, Transfer, createForm, type TransferKey } from '@woodger/antd';
const dataSource = Array.from({ length: 8 }, (_, index) => ({
key: String(index + 1),
title: `段落 ${index + 1}`,
description: `描述 ${index + 1}`,
disabled: index === 3
}));
const form = createForm<{ members: string[] }>({
initialValues: { members: ['2'] },
rules: ({ members: [{ required: true, message: "选择参与者。" }] })
});
let selectedKeys = $state<TransferKey[]>([]);
let result = $state('');
</script>
<style>
.lists {
max-width: 100%;
overflow-x: auto;
}
.lists:focus-visible {
outline: 2px solid var(--ant-color-primary);
outline-offset: 2px;
}
output { overflow-wrap: anywhere; }
.actions {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
</style>
<Form {form} onFinish={values => { result = values.members.join(', '); }}>
<FormItem hasFeedback name="members" label={("团队构成")}>
<div
class="lists"
role="region"
aria-label={("列表:在表单中选择成员")}
tabindex="0"
>
<Transfer
dataSource={(dataSource)}
targetKeys={form.values.members}
{selectedKeys}
titles={(["可用的", "团队"])}
actions={(["添加", "返回"])}
render={item => item.title}
onChange={next => form.setFieldValue('members', next.map(String))}
onSelectChange={(left, right) => { selectedKeys = [...left, ...right]; }}
/>
</div>
</FormItem>
<div
class="actions"
><Button
type="primary"
htmlType="submit"
>{("保存命令")}</Button><Button
htmlType="reset"
onclick={() => { selectedKeys = []; }}
>{("复位命令")}</Button></div>
</Form>
<p><output aria-label={("保存的命令")}>{(result
|| ("未发送"))}</output></p>
- /2
- /1
工作任务
评论中的原始示例:引导选择、搜索、页面、oneWay 和转储两个列表。
<script lang="ts">
import { Button, Transfer, type TransferKey } from '@woodger/antd';
const dataSource = Array.from({ length: 12 }, (_, index) => ({
key: String(index + 1),
title: `任务 ${index + 1}`,
description: `任务描述 ${index + 1}`,
disabled: index === 3
}));
let targetKeys = $state<TransferKey[]>(['2', '5']);
let selectedKeys = $state<TransferKey[]>([]);
let oneWay = $state(false);
</script>
<style>
.controls {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 12px;
margin-bottom: 16px;
}
.controls label {
display: flex;
align-items: center;
gap: 8px;
}
.lists {
max-width: 100%;
overflow-x: auto;
}
output {
display: block;
margin-top: 16px;
overflow-wrap: anywhere;
}
</style>
<div class="controls">
<label><input
type="checkbox"
bind:checked={oneWay}
/>{(" 只有一种方式")}</label>
<Button onclick={() => {
targetKeys = ['2', '5'];
selectedKeys = [];
}}>{("重置列表")}</Button>
</div>
<div class="lists">
<Transfer
dataSource={(dataSource)}
{targetKeys}
{selectedKeys}
{oneWay}
titles={(["可用的", "已选择"])}
actions={["选择任务", "返回任务"]}
showSearch={{ placeholder: "寻找任务" }}
render={item => item.title}
pagination={({ pageSize: 5 })}
styles={{ section: `
width: 240px;
height: 300px;
` }}
onChange={next => { targetKeys = next; }}
onSelectChange={(source, target) => { selectedKeys = [...source, ...target]; }}
/>
</div>
<output aria-label={("转移任务")}>{targetKeys.length ? targetKeys.map(
key => `任务 ${key}`
).join(', ') : ("未选择任务")}</output>
Transfer 面板使用本地前缀,而 Checkbox、Input、Button 和 Pagination 使用独立的公共前缀。搜索、标记和传输的条目都会被保存。
传输 CSS 前缀
通用命名空间ConfigProvider和本地 prefixCls;当名称更改时,组件的状态将被保留。
<script lang="ts">
import { Checkbox, ConfigProvider, Transfer, type TransferKey } from '@woodger/antd';
let custom = $state(true);
let local = $state(true);
const prefix = $derived(custom ? 'suite' : 'ant');
const localPrefix = $derived(local ? 'project-transfer' : '');
let targetKeys = $state<TransferKey[]>(['docs']);
const dataSource = [
{ key: 'docs', title: "文档" },
{ key: 'examples', title: "示例" }
];
</script>
<style>
.example,
.prefix-stage {
display: grid;
grid-template-columns: minmax(0, 1fr);
gap: 16px;
min-width: 0;
}
.controls {
display: flex;
flex-wrap: wrap;
gap: 12px;
}
p { margin: 0; }
output { overflow-wrap: anywhere; }
.transfer-scroll {
min-width: 0;
max-width: 100%;
overflow-x: auto;
}
</style>
<div class="example">
<div class="controls">
<Checkbox bind:checked={custom}>{("传输套件公共前缀")}</Checkbox>
<Checkbox bind:checked={local}>{("转移本地前缀")}</Checkbox>
</div>
<ConfigProvider prefixCls={prefix} theme={{ cssVar: { prefix: 'transfer-theme' } }}>
<div class="prefix-stage">
<div class="transfer-scroll">
<Transfer
prefixCls={localPrefix}
dataSource={(dataSource)}
{targetKeys}
showSearch
render={item => item.title}
titles={(["可用的", "已选择"])}
onChange={keys => targetKeys = keys}
/>
</div>
<output aria-label={("选择带前缀的传输")}>{(targetKeys.join(', ')
|| '—')}</output>
</div>
</ConfigProvider>
<output
aria-label={("传输 CSS 前缀")}
>{("一般的: ")}{prefix}{(";当地的: ")}{(localPrefix
|| ("来自提供商"))}</output>
<p>
{("Transfer 面板使用本地前缀,而 Checkbox、Input、Button 和 Pagination 使用独立的公共前缀。搜索、标记和传输的条目都会被保存。")}
</p>
</div>
API
上面的卡片中展示了 ant.design 实时传输页面的 11 个主题:基本和单向传输、搜索、页脚、自定义数据和操作、页面、表格/树、状态和语义槽。此外,还会显示概述中的 disabled、表单和任务。
| 属性 | 描述 | 类型 | 默认 |
|---|---|---|---|
prefixCls | 组件及其部件的完整 CSS 前缀。非空本地 prop 优先;空字符串和 undefined 继承ConfigProvider。主题的命名空间是单独设置的。 | string | ConfigProvider.prefixCls + "-transfer" |
dataSource | 双方的所有参赛作品,包括已经选定的参赛作品。键必须是唯一的。 | readonly RecordType[] | [] |
targetKeys | 管理结果。接受 onChange;本地没有 defaultTargetKeys 和 bind:targetKeys。 | readonly TransferKey[] | [] |
selectedKeys | 双方临时选择。使用 onSelectChange 您可以显式控制它。 | readonly TransferKey[] | 本地选择 |
rowKey | 录音键。 TransferKey 存储 string、number 或 bigint。 | (record) => TransferKey | record.key |
render | 带有条目或对象的字符串、Svelte 片段。 value 指定文本元数据,label 指定内容。 | (record) => content | { label, value } | — |
onChange | 新结果、传输方向和移动记录的密钥。 | (targetKeys, direction, moveKeys) => void | — |
onSelectChange | 将源列表和目标列表的临时分配分开。 | (sourceSelectedKeys, targetSelectedKeys) => void | — |
showSearch | 每侧都有独立的搜索字符串。创建列表时读取 defaultValue。 | boolean | { placeholder, defaultValue } | false |
filterOption | 你自己的过滤器。 onSearch 报告物理面和搜索字符串。 | (search, record, direction) => boolean | 通过文本渲染 |
oneWay | 1个转接按钮;从目标端出现的所选按钮中删除。 | boolean | false |
actions | 常规按钮或带有 onClick、disabled、direction 和 icon 的片段的行。 | readonly TransferAction[] | 箭头 |
pagination | pageSize、简单、showSizeChanger 和 showLessItems。不适用于 children。 | boolean | TransferPagination | false |
footer | 您自己的页脚,包含列表的上下文及其操作。 | Snippet<[TransferListProps, { direction }]> | — |
children | 替换两侧的本体。应用程序将其控件连接到 onItemSelect/onItemSelectAll。 | Snippet<[TransferListBodyProps<RecordType>]> | 标准清单 |
titles | 两个列表的标题。locale 还会更改搜索、计数器和命令。 | readonly TransferContent[] | 从语言环境 |
showSelectAll | 显示总体选择。 selectAllLabels 替换计数器标签。 | boolean | true |
disabled | 阻止所有操作。disabled 的条目被单独阻止。 | boolean | 来自表单/ConfigProvider |
status | 要检查的 error 或 warning。空字符串继承字段状态。 | TransferStatus | 从表单项 |
styles | 公共部位以及 source 和 target 列表的 CSS 字符串和对象。classNames 使用相同结构设置类名。 | TransferSemanticValue<CSSStyle> | — |
nativeElement | bind:nativeElement 返回根。单独的 focus()/blur() 不会导出。 | HTMLDivElement | — |
完整的 Props 定义属于包。 Svelte 中的 ReactNode 和 cloneElement 被带有显式传递处理程序的代码片段所取代。语义 styles 包含 CSS 字符串; styles.section 设置两列的样式。
您的列表主体
children 接收 TransferListBodyProps:filteredItems、selectedKeys、disabled、direction、onItemSelect 和 onItemSelectAll。direction - 物理侧 left/right 以及 RTL 的变化。通用标题、搜索和传输按钮仍保留在 Transfer 中;正文的内容归应用程序所有。
Table Transfer 将公共 selectedRowKeys/onSelectionChange 表连接到列表选择。在 Svelte 中,它们位于 Table 上,而不是在 React rowSelection 内。
Tree Transfer 使用独立的树形复选框和右侧的复选框列表。标记一行调用 onItemSelect;组表选择 - onItemSelectAll(keys, 'replace')。 Transfer 的本机分页不适用于 children:应用程序组织自己的正文页面。
Semantic DOM
在常规正文中,行使用本机复选框,操作使用按钮,搜索使用输入。此页面上的宽列表在可通过键盘访问的命名区域内滚动。编写时,保留可访问的名称和 disabled 的控件。
| Slots | 目的 |
|---|---|
root / actions | Root 和传输按钮区域。 |
section / header / title / body | 列表、标题、文本和正文。 |
list / item / itemIcon / itemContent / footer | 行列表、行、图标、内容和页脚。 |
source / target | 源端和目标端的嵌套节槽。本地时隙与通用时隙重叠。 |
classNames/styles 可以是 info.props 中的对象或函数。 ConfigProvider.transfer继承了 class、style、classNames、styles 和 selectionsIcon。 bind:nativeElement 返回根 div; Transfer 没有单独的聚焦方法。
注册令牌
根据 ant.design 和 ComponentTokenMap['Transfer'] 检查六个组件令牌。 theme.components.Transfer 应用列表的大小和填充。语义 styles 和 ConfigProvider.transfer 指定其他本地设置。
<script lang="ts">
import { ConfigProvider, Transfer, type TransferKey } from '@woodger/antd';
let targetKeys = $state<TransferKey[]>(['b']);
</script>
<style>
.lists {
max-width: 100%;
overflow-x: auto;
}
</style>
<ConfigProvider transfer={{
styles: {
section: `
width: 220px;
height: 220px;
`,
title: 'font-weight:600',
target: { header: 'background:var(--ant-color-primary-bg)' }
}
}}>
<div
class="lists"
role="region"
aria-label={("提供商的转让登记")}
tabindex="0"
>
<Transfer dataSource={([{ key: 'a', title: "开发" }, {
key: 'b',
title: "文档"
}])}
{targetKeys} titles={(["可用的", "已选择"])} actions={([
"添加",
"返回"
])}
render={item => item.title} onChange={next => { targetKeys = next; }} />
</div>
</ConfigProvider>
| 代币 | 目的 |
|---|---|
headerHeight | 列表标题高度。 |
itemHeight | 最小行高。 |
itemPaddingBlock | 垂直线缩进。 |
listHeight | 列表高度。 |
listWidth | 常规列表的宽度。 |
listWidthLG | 展开列表的宽度。 |
当前主题的常用标记
这些值是通过 theme.useToken() 反应式读取的。
| 代币 | 当前值 |
|---|---|
colorBgContainer | #ffffff |
colorBgContainerDisabled | rgba(0,0,0,0.04) |
colorBorder | #d9d9d9 |
colorError | #ff4d4f |
colorLink | #1677ff |
colorLinkActive | #0958d9 |
colorLinkHover | #69b1ff |
colorPrimaryBorder | #adc6ff |
colorSplit | rgba(5,5,5,0.06) |
colorText | rgba(0,0,0,0.88) |
colorTextDisabled | rgba(0,0,0,0.25) |
colorTextSecondary | rgba(0,0,0,0.65) |
colorWarning | #faad14 |
borderRadiusLG | 8 |
controlHeightLG | 40 |
controlItemBgActive | #f0f5ff |
controlItemBgActiveHover | #d6e4ff |
controlItemBgHover | rgba(0,0,0,0.04) |
fontFamily | -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial,
'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol',
'Noto Color Emoji' |
fontSize | 14 |
fontSizeIcon | 12 |
lineHeight | 1.5714285714285714 |
lineType | solid |
lineWidth | 1 |
lineWidthFocus | 3 |
linkDecoration | none |
linkFocusDecoration | none |
linkHoverDecoration | none |
marginXS | 8 |
marginXXS | 4 |
motionDurationSlow | 0.3s |
paddingSM | 12 |
paddingXS | 8 |
FAQ
为什么标记线还没有在右侧?
标记该行,按“移动”并在 onChange 中接受新的 targetKeys。选择和结果分开存储。
oneWay 中删除是什么意思?
从目标集中删除一个密钥。该条目返回到原始列表;服务器上的数据不会被删除。
如何连接远程数据?
管理应用程序内的加载和服务器页面。在 dataSource 中留下所选key的记录,以显示目标端;内置搜索过滤本地数据。
如何从具有不同名称的字段中选择键?
例如,将 rowKey 传递给返回成员 id 的函数。密钥在双方和数组更新时都必须保持唯一。