Demos
How to import
import { Transfer } from '@fibrous/semi-ui-react';
Basic Usage
dataSource should have value、label、key.
Code
import { Transfer } from '@fibrous/semi-ui-react';
export default function TransferDemo() {
const data = Array.from({ length: 100 }, (_v, i) => {
return {
label: `选项名称 ${i}`,
value: i,
disabled: false,
key: `key-${i}`,
};
});
return <Transfer style={{ width: 568, height: 416 }} dataSource={data} onChange={(values, items) => console.log(values, items)} />;
}Grouped
Set type to groupList
For grouped dataSource, the first-level child elements must have title and children attributes, structure reference <GroupItem>
Does not support multi-level nesting
Code
import { Transfer } from '@fibrous/semi-ui-react';
export default function TransferDemo() {
const dataWithGroup = [
{
title: '类别A',
children: [
{ label: 'A-1', value: 1, disabled: false, key: 1 },
{ label: 'A-2', value: 2, disabled: false, key: 2 },
{ label: 'A-3', value: 3, disabled: false, key: 3 },
],
},
{
title: '类别B',
children: [
{ label: 'B-1', value: 4, disabled: false, key: 4 },
{ label: 'B-2', value: 5, disabled: false, key: 5 },
{ label: 'B-3(disabled)', value: 6, disabled: true, key: 6 },
],
},
{
title: '类别C',
children: [
{ label: 'C-1', value: 7, disabled: false, key: 7 },
{ label: 'C-2', value: 8, disabled: false, key: 8 },
{ label: 'C-3', value: 9, disabled: false, key: 9 },
{ label: 'C-4', value: 10, disabled: false, key: 10 },
{ label: 'C-5', value: 11, disabled: false, key: 11 },
{ label: 'C-6', value: 12, disabled: false, key: 12 },
{ label: 'C-7', value: 13, disabled: false, key: 13 },
],
},
];
return <Transfer type="groupList" defaultValue={[6]} style={{ width: 568 }} dataSource={dataWithGroup} onChange={(values, items) => console.log(values, items)} />;
}Custom filtering logic, custom option data rendering
Use filter to customize the search logic. When it returns true, it means that the current item meets the filter rules and keeps the display of the current item in the list. If it returns false, it means it does not match, and the current item will be hidden.
When type is treeList, if you need to customize search logic, you need to set filter to true and set a custom search function through filterTreeNode of treeProps.
Using renderSourceItem, you can customize the rendering structure of each source data on the left.
Using renderSelectedItem you can customize the rendering structure of each selected item on the right.
Code
import { useState } from 'react';
import { IconClose } from '@fibrous/semi-icons-react';
import { Transfer, Button, Checkbox, Avatar, Highlight, type TransferDataItem, type TransferRenderSelectedItemProps, type TransferRenderSourceItemProps } from '@fibrous/semi-ui-react';
export default function TransferDemo() {
const [searchText, setSearchText] = useState('');
const renderSourceItem = (item: TransferRenderSourceItemProps) => {
return (
<div className="components-transfer-demo-source-item" key={item.key}>
<Checkbox
onChange={() => {
item.onChange?.();
}}
checked={item.checked}
style={{ height: 52, alignItems: 'center' }}
>
<Avatar color={item.color} size="small">
{item.abbr}
</Avatar>
<div className="info">
<div className="name">
<Highlight sourceString={String(item.label)} searchWords={[searchText]} />
</div>
<div className="email">
<Highlight sourceString={String(item.value)} searchWords={[searchText]} />
</div>
</div>
</Checkbox>
</div>
);
};
const renderSelectedItem = (item: TransferRenderSelectedItemProps) => {
return (
<div className="components-transfer-demo-selected-item" key={item.key}>
<Avatar color={item.color} size="small">
{item.abbr}
</Avatar>
<div className="info">
<div className="name">{item.label}</div>
<div className="email">{item.value}</div>
</div>
<Button className="remove" theme="borderless" type="tertiary" size="small" icon={<IconClose />} aria-label={`移除 ${item.label}`} onClick={item.onRemove} />
</div>
);
};
const customFilter = (searchValue: string, item: TransferDataItem) => {
return String(item.value).includes(searchValue) || String(item.label).includes(searchValue);
};
const data = [
{ label: '夏可漫', value: 'xiakeman@example.com', abbr: '夏', color: 'amber', area: 'US', key: 1 },
{ label: '申悦', value: 'shenyue@example.com', abbr: '申', color: 'indigo', area: 'UK', key: 2 },
{ label: '文嘉茂', value: 'wenjiamao@example.com', abbr: '文', color: 'cyan', area: 'HK', key: 3 },
{ label: '曲晨一', value: 'quchenyi@example.com', abbr: '曲', color: 'blue', area: 'India', key: 4 },
{ label: '曲晨二', value: 'quchener@example.com', abbr: '二', color: 'blue', area: 'India', key: 5 },
{ label: '曲晨三', value: 'quchensan@example.com', abbr: '三', color: 'blue', area: 'India', key: 6 },
];
return (
<Transfer
style={{ width: 568 }}
dataSource={data}
filter={customFilter}
defaultValue={['xiakeman@example.com', 'shenyue@example.com']}
renderSelectedItem={renderSelectedItem}
renderSourceItem={renderSourceItem}
inputProps={{ placeholder: '搜索姓名或邮箱' }}
onSearch={(searchText) => setSearchText(searchText)}
onChange={(values, items) => console.log(values, items)}
/>
);
}.components-transfer-demo-selected-item {
.remove {
opacity: 0;
}
&:hover,
&:focus-within {
.remove {
opacity: 1;
}
}
}
.components-transfer-demo-selected-item,
.components-transfer-demo-source-item {
height: 52px;
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: space-between;
padding: 10px 12px;
&:hover {
background-color: var(--semi-color-fill-0);
}
.info {
margin-left: 8px;
flex-grow: 1;
}
.name {
font-size: 14px;
line-height: 20px;
}
.email {
font-size: 12px;
line-height: 16px;
color: var(--semi-color-text-2);
}
}
Disabled
Code
import { Transfer } from '@fibrous/semi-ui-react';
export default function TransferDemo() {
const data = Array.from({ length: 20 }, (_v, i) => {
return {
label: `选项名称 ${i}`,
value: i,
disabled: false,
key: `key-${i}`,
};
});
return <Transfer style={{ width: 568, height: 416 }} disabled dataSource={data} defaultValue={[2, 4]} onChange={(values, items) => console.log(values, items)} />;
}Drag and drop sort
Set draggable to true to enable the drag sort function.
Code
import { Transfer } from '@fibrous/semi-ui-react';
export default function TransferDemo() {
const data = Array.from({ length: 30 }, (_v, i) => {
return {
label: `选项名称 ${i}`,
value: i,
disabled: false,
key: `key-${i}`,
};
});
return <Transfer style={{ width: 568, height: 416 }} dataSource={data} defaultValue={[2, 4]} draggable onChange={(values, items) => console.log(values, items)} />;
}Left panel pagination
When there are many options on the left, you can enable pagination through the pagination prop.
pagination accepts an object with the following properties:
pageSize: Number of items per page, default is 10currentPage: Current page number (controlled mode)defaultCurrentPage: Default current page number (uncontrolled mode)onPageChange: Callback when page changes
Code
import { Transfer } from '@fibrous/semi-ui-react';
export default function TransferDemo() {
const data = Array.from({ length: 100 }, (_v, i) => {
return {
label: `选项名称 ${i}`,
value: i,
disabled: false,
key: `key-${i}`,
};
});
return (
<Transfer
style={{ width: 568, height: 416 }}
dataSource={data}
pagination={{
pageSize: 10,
}}
onChange={(values, items) => console.log(values, items)}
/>
);
}Left panel pagination + controlled page
You can control the current page number through pagination.currentPage.
Code
import { useState } from 'react';
import { Transfer, ButtonGroup, Button } from '@fibrous/semi-ui-react';
export default function TransferDemo() {
const [currentPage, setCurrentPage] = useState(1);
const data = Array.from({ length: 100 }, (_v, i) => {
return {
label: `选项名称 ${i}`,
value: i,
disabled: false,
key: `key-${i}`,
};
});
return (
<div>
<ButtonGroup style={{ marginBottom: 12 }}>
<Button onClick={() => setCurrentPage(1)}>第1页</Button>
<Button onClick={() => setCurrentPage(2)}>第2页</Button>
<Button onClick={() => setCurrentPage(5)}>第5页</Button>
<Button onClick={() => setCurrentPage(10)}>第10页</Button>
</ButtonGroup>
<div>当前页码: {currentPage}</div>
<Transfer
style={{ width: 568, height: 416 }}
dataSource={data}
pagination={{
pageSize: 10,
currentPage,
onPageChange: (page) => setCurrentPage(page),
}}
onChange={(values, items) => console.log(values, items)}
/>
</div>
);
}Drag and drop + custom selected rendering
Set draggable to true to enable the drag and drop sorting function; use renderSelectedItem to customize the rendering of the selected items on the right; You can define the trigger as any ReactNode you want and add styles. Drag the trigger and use sortableHandle to wrap it
Code
import { IconHandle, IconClose } from '@fibrous/semi-icons-react';
import { Transfer, Button, Checkbox, Avatar, type TransferDataItem, type TransferRenderSelectedItemProps, type TransferRenderSourceItemProps } from '@fibrous/semi-ui-react';
export default function TransferDemo() {
const renderSourceItem = (item: TransferRenderSourceItemProps) => {
return (
<div className="components-transfer-demo-source-item" key={item.key}>
<Checkbox
onChange={() => {
item.onChange?.();
}}
checked={item.checked}
style={{ height: 52, alignItems: 'center' }}
>
<Avatar color={item.color} size="small">
{item.abbr}
</Avatar>
<div className="info">
<div className="name">{item.label}</div>
<div className="email">{item.value}</div>
</div>
</Checkbox>
</div>
);
};
const renderSelectedItem = (item: TransferRenderSelectedItemProps) => {
const { sortableHandle } = item;
if (!sortableHandle) {
return null;
}
const DragHandle = sortableHandle(() => <IconHandle className="semi-right-item-drag-handler" />);
return (
<div className="components-transfer-demo-selected-item" key={item.key}>
<DragHandle />
<Avatar color={item.color} size="small">
{item.abbr}
</Avatar>
<div className="info">
<div className="name">{item.label}</div>
<div className="email">{item.value}</div>
</div>
<Button className="remove" theme="borderless" type="tertiary" size="small" icon={<IconClose />} aria-label={`移除 ${item.label}`} onClick={item.onRemove} />
</div>
);
};
const customFilter = (searchValue: string, item: TransferDataItem) => {
return String(item.value).includes(searchValue) || String(item.label).includes(searchValue);
};
const data = [
{ label: '夏可漫', value: 'xiakeman@example.com', abbr: '夏', color: 'amber', area: 'US', key: 1 },
{ label: '申悦', value: 'shenyue@example.com', abbr: '申', color: 'indigo', area: 'UK', key: 2 },
{ label: '文嘉茂', value: 'wenjiamao@example.com', abbr: '文', color: 'cyan', area: 'HK', key: 3 },
{ label: '曲晨一', value: 'quchenyi@example.com', abbr: '曲', color: 'blue', area: 'India', key: 4 },
{ label: '曲晨二', value: 'quchener@example.com', abbr: '二', color: 'blue', area: 'India', key: 5 },
{ label: '曲晨三', value: 'quchensan@example.com', abbr: '三', color: 'blue', area: 'India', key: 6 },
];
return (
<Transfer
draggable
style={{ width: 568 }}
dataSource={data}
filter={customFilter}
defaultValue={['xiakeman@example.com', 'shenyue@example.com']}
renderSelectedItem={renderSelectedItem}
renderSourceItem={renderSourceItem}
inputProps={{ placeholder: '搜索姓名或邮箱' }}
onChange={(values, items) => console.log(values, items)}
/>
);
}Custom rendering header information in panel
Semi has provided renderSourceHeader and renderSelectedHeader parameter allows users to customize the header information of the left and right panels.
renderSourceHeader: (props: SourceHeaderProps) => ReactNode
renderSelectedHeader: (props: SelectedHeaderProps) => ReactNode
The parameter types are as follows:
type SourceHeaderProps = {
num: number; // The total number of data or the number of filtered results
showButton: boolean; // Whether to show select all/unselect all buttons
allChecked: boolean; // Whether the current data has been selected
onAllClick: () => void; // Function that should be called after clicking the select/unselect all button
leafOnlyNum?: number; // Only valid when type is treeList, represents the number of leaf nodes
};
type SelectedHeaderProps = {
num: number; // The total number of selected data
showButton: boolean; // Whether to show the clear button
onClear: () => void; // Function that should be called after clicking the clear button
};
The example is as follows:
Code
import { Transfer, Button, type TransferProps } from '@fibrous/semi-ui-react';
export default function TransferDemo() {
const data = Array.from({ length: 30 }, (_v, i) => {
return {
label: `选项名称 ${i}`,
value: i,
disabled: false,
key: `key-${i}`,
};
});
const renderSourceHeader: TransferProps['renderSourceHeader'] = ({ num, showButton, allChecked, onAllClick }) => {
return (
<div style={{ margin: '10px 0 0 10px', height: 24, display: 'flex', alignItems: 'center' }}>
<span>共 {num} 项</span>
{showButton && (
<Button theme="borderless" type="tertiary" size="small" onClick={onAllClick}>
{allChecked ? '取消全选' : '全选'}
</Button>
)}
</div>
);
};
const renderSelectedHeader: TransferProps['renderSelectedHeader'] = ({ num, showButton, onClear }) => {
return (
<div style={{ margin: '10px 0 0 10px', height: 24, display: 'flex', alignItems: 'center' }}>
<span>{num} 项已选</span>
{showButton && (
<Button theme="borderless" type="tertiary" size="small" onClick={onClear}>
清空
</Button>
)}
</div>
);
};
return <Transfer style={{ width: 568, height: 416 }} dataSource={data} renderSourceHeader={renderSourceHeader} renderSelectedHeader={renderSelectedHeader} />;
}Fully custom rendering
Semi provides renderSourcePanel and renderSelectedPanel input parameters, allowing you to completely customize the rendering structure of the left and right panels. With this function, you can directly reuse the logic capabilities inside the Transfer to implement the Transfer component with a highly customized style structure renderSourcePanel: (sourcePanelProps: SourcePanelProps) => ReactNode SourcePanelProps contains the following parameters, from which you can get data to render your Panel structure
interface SourcePanelProps {
value: Array<string | number>; // value of all selected items
loading?: boolean; // Whether loading
noMatch: boolean; // Whether there is no matching item that matches the current search value
filterData: Array<Item>; // items that match the current search value
sourceData: Array<Item>; // All items
propsDataSource: Array<Item> | Array<GroupItem> | Array<TreeItem>; // The dataSource of Transfer
allChecked: boolean; // Whether to select all
showNumber: number; // Filter the number of results
inputValue: string; // the value of the input search box
onSearch: (searchString: string) => void; // The function that should be called when the search box changes, the input parameter is the search value
onAllClick: () => void; // The function that should be called when all the buttons on the left are clicked
onSelectOrRemove: (item: Item) => void; // The function that should be called when selecting or deleting a single option, the input should be the current operation item
onSelect: (value: Array<string | number>) => void; // controlled batch selection, the input is the values of the selected items
selectedItems: Map<string | number, Item>; // collection of all selected items, the key of the Map is item.key
}
renderSelectedPanel: (selectedPanelProps: SelectedPanelProps) => ReactNode SelectedPanelProps contains the following parameters
interface SelectedPanelProps {
length: number; // number of selected options
onClear: () => void; // The callback function that should be called when clicking to clear
onRemove: (item: Item) => void; // The function that should be called when deleting a single option
onSortEnd: ({ oldIndex, newIndex }) => void; // The function that should be called when reordering the results
selectedData: Array<Item>; // All selected items collection
}
海底捞门店 0
海底捞门店 1
海底捞门店 2
海底捞门店 3
海底捞门店 4
海底捞门店 5
海底捞门店 6
海底捞门店 7
海底捞门店 8
海底捞门店 9
海底捞门店 10
海底捞门店 11
海底捞门店 12
海底捞门店 13
海底捞门店 14
海底捞门店 15
海底捞门店 16
海底捞门店 17
海底捞门店 18
海底捞门店 19
海底捞门店 20
海底捞门店 21
海底捞门店 22
海底捞门店 23
海底捞门店 24
海底捞门店 25
海底捞门店 26
海底捞门店 27
海底捞门店 28
海底捞门店 29
海底捞门店 30
海底捞门店 31
海底捞门店 32
海底捞门店 33
海底捞门店 34
海底捞门店 35
海底捞门店 36
海底捞门店 37
海底捞门店 38
海底捞门店 39
海底捞门店 40
海底捞门店 41
海底捞门店 42
海底捞门店 43
海底捞门店 44
海底捞门店 45
海底捞门店 46
海底捞门店 47
海底捞门店 48
海底捞门店 49
海底捞门店 50
海底捞门店 51
海底捞门店 52
海底捞门店 53
海底捞门店 54
海底捞门店 55
海底捞门店 56
海底捞门店 57
海底捞门店 58
海底捞门店 59
海底捞门店 60
海底捞门店 61
海底捞门店 62
海底捞门店 63
海底捞门店 64
海底捞门店 65
海底捞门店 66
海底捞门店 67
海底捞门店 68
海底捞门店 69
海底捞门店 70
海底捞门店 71
海底捞门店 72
海底捞门店 73
海底捞门店 74
海底捞门店 75
海底捞门店 76
海底捞门店 77
海底捞门店 78
海底捞门店 79
海底捞门店 80
海底捞门店 81
海底捞门店 82
海底捞门店 83
海底捞门店 84
海底捞门店 85
海底捞门店 86
海底捞门店 87
海底捞门店 88
海底捞门店 89
海底捞门店 90
海底捞门店 91
海底捞门店 92
海底捞门店 93
海底捞门店 94
海底捞门店 95
海底捞门店 96
海底捞门店 97
海底捞门店 98
海底捞门店 99
Code
import { type ReactNode } from 'react';
import { IconSearch } from '@fibrous/semi-icons-react';
import { Transfer, Input, Spin, Button, type TransferDataItem, type TransferSelectedPanelProps as SelectedPanelProps, type TransferSourcePanelProps as SourcePanelProps } from '@fibrous/semi-ui-react';
const dataSource = Array.from({ length: 100 }, (_v, i) => ({
label: `海底捞门店 ${i}`,
value: i,
disabled: false,
key: `key-${i}`,
}));
function renderItem(type: 'source' | 'selected', item: TransferDataItem, onItemAction: (item: TransferDataItem) => void, selectedItems?: SourcePanelProps['selectedItems']) {
let buttonText = '删除';
if (type === 'source') {
buttonText = selectedItems?.has(item.key) ? '删除' : '添加';
}
return (
<div className="semi-transfer-item panel-item" key={item.key}>
<p>{item.label}</p>
<Button theme="borderless" type="primary" onClick={() => onItemAction(item)} className="panel-item-remove" size="small">
{buttonText}
</Button>
</div>
);
}
function renderSourcePanel({ loading, noMatch, filterData, selectedItems, allChecked, onAllClick, inputValue, onSearch, onSelectOrRemove }: SourcePanelProps) {
let content: ReactNode;
if (loading) {
content = <Spin />;
} else if (noMatch) {
content = <div className="empty sp-font">{inputValue ? '无搜索结果' : '暂无内容'}</div>;
} else {
content = filterData.map((item) => renderItem('source', item, onSelectOrRemove, selectedItems));
}
return (
<section className="source-panel">
<div className="panel-header sp-font">门店列表</div>
<div className="panel-main">
<Input aria-label="搜索门店" style={{ width: 454, margin: '12px 14px' }} prefix={<IconSearch />} onChange={onSearch} showClear />
<div className="panel-controls sp-font">
<span>待选门店: {filterData.length}</span>
<Button onClick={onAllClick} theme="borderless" size="small">
{allChecked ? '取消全选' : '全选'}
</Button>
</div>
<div className="panel-list">{content}</div>
</div>
</section>
);
}
function renderSelectedPanel({ selectedData, onClear, onRemove }: SelectedPanelProps) {
let mainContent: ReactNode = selectedData.map((item) => renderItem('selected', item, onRemove));
if (!selectedData.length) {
mainContent = <div className="empty sp-font">暂无数据,请从左侧筛选</div>;
}
return (
<section className="selected-panel">
<div className="panel-header sp-font">
<div>已选同步门店: {selectedData.length}</div>
<Button theme="borderless" type="primary" onClick={onClear} size="small">
清空
</Button>
</div>
<div className="panel-main">{mainContent}</div>
</section>
);
}
export default function TransferDemo() {
return <Transfer onChange={(values) => console.log(values)} className="component-transfer-demo-custom-panel" renderSourcePanel={renderSourcePanel} renderSelectedPanel={renderSelectedPanel} dataSource={dataSource} />;
}.component-transfer-demo-custom-panel {
.sp-font {
color: var(--semi-grey-9);
font-size: 12px;
font-weight: 500;
line-height: 20px;
}
.empty {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.panel-item {
flex-shrink: 0;
height: 56px;
border-radius: 4px;
padding: 8px 12px;
flex-wrap: wrap;
background-color: rgba(22, 24, 35, 0.03);
&-main {
flex-grow: 1;
}
p {
margin: 0 12px;
flex-basis: 100%;
}
.panel-item-remove {
cursor: pointer;
color: var(--semi-color-primary);
}
}
.panel-header {
padding: 10px 12px;
border: 1px solid rgba(22, 24, 35, 0.16);
border-radius: 4px 4px 0 0;
height: 38px;
box-sizing: border-box;
background-color: var(--semi-color-tertiary-light-default);
display: flex;
align-items: center;
justify-content: space-between;
.clear {
cursor: pointer;
color: var(--semi-color-primary);
}
}
.source-panel {
display: flex;
flex-direction: column;
width: 482px;
height: 353px;
.panel-main {
border: 1px solid var(--semi-color-border);
border-top: none;
.panel-list {
display: flex;
flex-wrap: wrap;
row-gap: 8px;
column-gap: 8px;
overflow-y: auto;
height: 214px;
margin-left: 12px;
margin-right: 12px;
padding-bottom: 8px;
}
}
.panel-controls {
margin: 10px 12px;
font-size: 12px;
line-height: 20px;
.semi-button {
margin-left: 8px;
font-size: 12px;
}
}
.panel-item {
width: 176px;
}
margin-right: 16px;
}
.selected-panel {
width: 200px;
height: 353px;
.panel-main {
display: flex;
flex-direction: column;
overflow-y: auto;
padding: 12px;
border: 1px solid var(--semi-color-border);
border-top: none;
height: 323px;
box-sizing: border-box;
row-gap: 8px;
}
}
}
Fully custom rendering, drag and drop sorting
In a completely custom rendering scene, since the rendering of the drag area has also been completely taken over by you, you do not need to declare draggable. But you need to implement the drag and drop logic yourself, You can use the drag-and-drop tool library dnd-kit to quickly realize the function.
Besides, To support drag sorting, you need to call onSortEnd with oldIndex and newIndex as the input parameters after the drag sorting is over
Example using dnd-kit,The core dependency that needs to be used is @dnd-kit/react. DragDropProvider provides the drag and drop context, and the core hook is useSortable exported from @dnd-kit/react/sortable. The usage instructions of useSortable are as follows
1. Function: Obtain the necessary information during the drag and drop process through the unique id and the current index
2. Core input parameters:
- id: unique identifier, which can be a number or a string
- index: the current position of the item in the list
- handle: optional, the drag handle element; the whole node is draggable when omitted
3. Core return value description:
- ref: Associate the dom node to make it a draggable item
- handleRef: Associate the dom node of the drag handle
- isDragging: whether the node is being dragged
After dragging ends, in the onDragEnd of DragDropProvider, use initialIndex and index of event.operation.source to get the positions before and after the drag; event.canceled being true means the drag was canceled.
海底捞门店 0
海底捞门店 1
海底捞门店 2
海底捞门店 3
海底捞门店 4
海底捞门店 5
海底捞门店 6
海底捞门店 7
海底捞门店 8
海底捞门店 9
海底捞门店 10
海底捞门店 11
海底捞门店 12
海底捞门店 13
海底捞门店 14
海底捞门店 15
海底捞门店 16
海底捞门店 17
海底捞门店 18
海底捞门店 19
海底捞门店 20
海底捞门店 21
海底捞门店 22
海底捞门店 23
海底捞门店 24
海底捞门店 25
海底捞门店 26
海底捞门店 27
海底捞门店 28
海底捞门店 29
海底捞门店 30
海底捞门店 31
海底捞门店 32
海底捞门店 33
海底捞门店 34
海底捞门店 35
海底捞门店 36
海底捞门店 37
海底捞门店 38
海底捞门店 39
海底捞门店 40
海底捞门店 41
海底捞门店 42
海底捞门店 43
海底捞门店 44
海底捞门店 45
海底捞门店 46
海底捞门店 47
海底捞门店 48
海底捞门店 49
海底捞门店 50
海底捞门店 51
海底捞门店 52
海底捞门店 53
海底捞门店 54
海底捞门店 55
海底捞门店 56
海底捞门店 57
海底捞门店 58
海底捞门店 59
海底捞门店 60
海底捞门店 61
海底捞门店 62
海底捞门店 63
海底捞门店 64
海底捞门店 65
海底捞门店 66
海底捞门店 67
海底捞门店 68
海底捞门店 69
海底捞门店 70
海底捞门店 71
海底捞门店 72
海底捞门店 73
海底捞门店 74
海底捞门店 75
海底捞门店 76
海底捞门店 77
海底捞门店 78
海底捞门店 79
海底捞门店 80
海底捞门店 81
海底捞门店 82
海底捞门店 83
海底捞门店 84
海底捞门店 85
海底捞门店 86
海底捞门店 87
海底捞门店 88
海底捞门店 89
海底捞门店 90
海底捞门店 91
海底捞门店 92
海底捞门店 93
海底捞门店 94
海底捞门店 95
海底捞门店 96
海底捞门店 97
海底捞门店 98
海底捞门店 99
海底捞门店 2
海底捞门店 4
Code
import type { ReactNode } from 'react';
import { DragDropProvider, type DragEndEvent } from '@dnd-kit/react';
import { isSortable, useSortable } from '@dnd-kit/react/sortable';
import { IconSearch, IconHandle } from '@fibrous/semi-icons-react';
import { Transfer, Input, Spin, Button, type TransferDataItem, type TransferOnSortEnd as OnSortEnd, type TransferSelectedPanelProps as SelectedPanelProps, type TransferSourcePanelProps as SourcePanelProps } from '@fibrous/semi-ui-react';
type HandleRef = (element: Element | null) => void;
type RenderSortableItem = (props: { id: string | number; handleRef: HandleRef }) => ReactNode;
interface SortableListProps {
items: Array<string | number>;
onSortEnd: OnSortEnd;
renderItem: RenderSortableItem;
}
function SortableList({ items, onSortEnd, renderItem }: SortableListProps) {
const onDragEnd = (event: DragEndEvent) => {
const { source } = event.operation;
if (!event.canceled && isSortable(source) && source.initialIndex !== source.index) {
onSortEnd({ oldIndex: source.initialIndex, newIndex: source.index });
}
};
return (
<DragDropProvider onDragEnd={onDragEnd}>
<div style={{ overflow: 'auto', display: 'flex', flexDirection: 'column', rowGap: '8px' }}>
{items.map((value, index) => (
<SortableItem key={value} id={value} index={index} renderItem={renderItem} />
))}
</div>
</DragDropProvider>
);
}
interface SortableItemProps {
id: string | number;
index: number;
renderItem: RenderSortableItem;
}
function SortableItem({ id, index, renderItem }: SortableItemProps) {
// Only the handle drags; the Accessibility plugin gives it a button role and Tab stop for keyboard sorting
const { ref, handleRef } = useSortable({ id, index });
return <div ref={ref}>{renderItem({ id, handleRef })}</div>;
}
const dataSource = Array.from({ length: 100 }, (_v, i) => ({
label: `海底捞门店 ${i}`,
value: i,
disabled: false,
key: `key-${i}`,
}));
function renderItem(type: 'source' | 'selected', item: TransferDataItem, onItemAction: (item: TransferDataItem) => void, selectedItems?: SourcePanelProps['selectedItems'], handleRef?: HandleRef) {
let buttonText = '删除';
if (type === 'source') {
let checked = selectedItems?.has(item.key);
buttonText = checked ? '删除' : '添加';
}
return (
<div className="semi-transfer-item panel-item" key={item.key}>
{handleRef && (
<span ref={handleRef} aria-label={String(item.label)} style={{ lineHeight: 0 }}>
<IconHandle className="pane-item-drag-handler" />
</span>
)}
<div className="panel-item-main" style={{ flexGrow: 1 }}>
<p style={{ margin: '0 12px' }}>{item.label}</p>
<Button theme="borderless" type="primary" onClick={() => onItemAction(item)} className="panel-item-remove" size="small">
{buttonText}
</Button>
</div>
</div>
);
}
function renderSourcePanel({ loading, noMatch, filterData, selectedItems, allChecked, onAllClick, inputValue, onSearch, onSelectOrRemove }: SourcePanelProps) {
let content: ReactNode;
if (loading) {
content = <Spin />;
} else if (noMatch) {
content = <div className="empty sp-font">{inputValue ? '无搜索结果' : '暂无内容'}</div>;
} else {
content = filterData.map((item) => renderItem('source', item, onSelectOrRemove, selectedItems));
}
return (
<section className="source-panel">
<div className="panel-header sp-font">门店列表</div>
<div className="panel-main">
<Input aria-label="搜索门店" style={{ width: 454, margin: '12px 14px' }} prefix={<IconSearch />} onChange={onSearch} showClear />
<div className="panel-controls sp-font">
<span>待选门店: {filterData.length}</span>
<Button onClick={onAllClick} theme="borderless" size="small">
{allChecked ? '取消全选' : '全选'}
</Button>
</div>
<div className="panel-list">{content}</div>
</div>
</section>
);
}
function renderSelectedPanel({ selectedData, onClear, onRemove, onSortEnd }: SelectedPanelProps) {
const renderSelectItem: RenderSortableItem = ({ id, handleRef }) => {
const item = selectedData.find((item) => id === item.key);
return item ? renderItem('selected', item, onRemove, undefined, handleRef) : null;
};
const sortData = selectedData.map((item) => item.key);
const mainContent = (
<div className="panel-main" style={{ display: 'block' }}>
<SortableList onSortEnd={onSortEnd} items={sortData} renderItem={renderSelectItem} />
</div>
);
return (
<section className="selected-panel">
<div className="panel-header sp-font">
<div>已选同步门店: {selectedData.length}</div>
<Button theme="borderless" type="primary" onClick={onClear} size="small">
清空
</Button>
</div>
{mainContent}
</section>
);
}
export default function TransferDemo() {
return <Transfer defaultValue={[2, 4]} onChange={(values) => console.log(values)} className="component-transfer-demo-custom-panel" renderSourcePanel={renderSourcePanel} renderSelectedPanel={renderSelectedPanel} dataSource={dataSource} />;
}Tree Transfer
The input type is treeList, and the Tree component is used as a custom rendering list.
The properties of the default tree can be overridden by treeProps(TreeProps). The default properties of the tree on the left are
{
multiple:true,
disableStrictly:true,
leafOnly:true,
filterTreeNode:true,
defaultExpandAll:true,
searchRender:false,
}
Code
import { useState } from 'react';
import { Transfer } from '@fibrous/semi-ui-react';
export default function TransferDemo() {
const treeData = [
{
label: 'Asia',
value: 'Asia',
key: '0',
children: [
{
label: 'China',
value: 'China',
key: '0-0',
children: [
{
label: 'Beijing',
value: 'Beijing',
key: '0-0-0',
},
{
label: 'Shanghai',
value: 'Shanghai',
key: '0-0-1',
},
{
label: 'Chengdu',
value: 'Chengdu',
key: '0-0-2',
},
],
},
{
label: 'Japan',
value: 'Japan',
key: '0-1',
children: [
{
label: 'Osaka',
value: 'Osaka',
key: '0-1-0',
},
],
},
],
},
{
label: 'North America',
value: 'North America',
key: '1',
children: [
{
label: 'United States',
value: 'United States',
key: '1-0',
},
{
label: 'Canada',
value: 'Canada',
key: '1-1',
},
{
label: 'Mexico',
value: 'Mexico',
disabled: true,
key: '1-2',
},
{
label: 'Cuba',
value: 'Cuba',
key: '1-3',
},
],
},
];
const [v, $v] = useState<Array<string | number>>(['Shanghai']);
return (
<div style={{ margin: 10, padding: 10, width: 600 }}>
<Transfer dataSource={treeData} type="treeList" value={v} onChange={$v} />
</div>
);
}Tree Transfer with Custom Header Showing Leaf Node Count
When type is treeList, the SourceHeaderProps parameter of renderSourceHeader provides an additional leafOnlyNum field, which represents the number of leaf nodes. This is useful in scenarios like file selection, where you can display only the file count instead of the total including folders.
Code
import { useState } from 'react';
import { Transfer, Button, type TransferProps } from '@fibrous/semi-ui-react';
export default function TransferDemo() {
const treeData = [
{
label: '文件夹 1',
value: 'folder1',
key: 'folder1',
children: [
{
label: '文件 1-1',
value: 'file1-1',
key: 'file1-1',
},
{
label: '文件 1-2',
value: 'file1-2',
key: 'file1-2',
},
{
label: '子文件夹 1',
value: 'subfolder1',
key: 'subfolder1',
children: [
{
label: '文件 1-1-1',
value: 'file1-1-1',
key: 'file1-1-1',
},
{
label: '文件 1-1-2',
value: 'file1-1-2',
key: 'file1-1-2',
},
],
},
],
},
{
label: '文件夹 2',
value: 'folder2',
key: 'folder2',
children: [
{
label: '文件 2-1',
value: 'file2-1',
key: 'file2-1',
},
{
label: '文件 2-2',
value: 'file2-2',
key: 'file2-2',
},
],
},
{
label: '独立文件',
value: 'file3',
key: 'file3',
},
];
const [value, setValue] = useState<Array<string | number>>([]);
const renderSourceHeader: TransferProps['renderSourceHeader'] = ({ num, leafOnlyNum, showButton, allChecked, onAllClick }) => {
return (
<div
style={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
padding: '8px 12px',
borderBottom: '1px solid var(--semi-color-border)',
}}
>
<span>
<strong>文件总数:</strong>
{leafOnlyNum !== undefined ? leafOnlyNum : num} 个文件
{leafOnlyNum !== undefined && <span style={{ color: 'var(--semi-color-text-2)', marginLeft: 8, fontSize: 12 }}>(总节点数:{num})</span>}
</span>
{showButton && (
<Button theme="borderless" type="tertiary" size="small" onClick={onAllClick}>
{allChecked ? '取消全选' : '全选'}
</Button>
)}
</div>
);
};
return <Transfer style={{ width: 600 }} dataSource={treeData} type="treeList" value={value} onChange={setValue} renderSourceHeader={renderSourceHeader} />;
}Accessibility
ARIA
- The container of the search box is a native
searchelement (the search landmark), named from the locale - When the root element has
aria-labeloraria-labelledbyand norole, it getsrolegroup - Add
rolelistto the option list on the left andlistitemto its options; ingroupListevery group is alistitemof the outer list, and its title names the nestedlistthat holds the options - Add
rolelistto the selected list on the right, addrolelistitemto the selected item; custom rendered items on either side are wrapped in an element withrolelistitem
Keyboard
- The default filter ignores case
- When disabled, the two lists are Tab stops so the keyboard can scroll their content
- With
draggable, the drag handle is a nativebuttonwith a name: Tab focuses the handle, Space picks up, the arrow keys move, Space drops; the sort is announced to screen readers. Mouse dragging is unchanged - Until the dnd-kit needed for sorting has loaded, the handle is a placeholder with
aria-disabled="true"
API Reference
Transfer
Props
| Property | Description | Type | Default |
|---|---|---|---|
| className | Class name of the root element. | string | - |
| dataSource | Data source: an array of items for list, an array of groups for groupList, or tree data with children for treeList. | DataSource | [] |
| defaultValue | Values selected initially, read once when the transfer is created. | TypeArray<string | number> | [] |
| disabled | Whether the whole transfer is disabled, including the search box, the options and the action buttons. | boolean | - |
| draggable | Enables drag sorting of the selected items on the right. After a sort, the order of the array passed to onChange changes. | boolean | - |
| emptyContent | Custom empty-state content: left when the left list has no data, right when nothing is selected, search when the search matches nothing. A null entry hides that empty state; a missing one shows the locale text. | EmptyContent | {} |
| filter | Filtering of the search. false hides the search box; a function replaces the default case-insensitive match on label and returns true to keep an item. For a custom search in treeList, set it to true and pass the matching function as filterTreeNode in treeProps. | Typeboolean | ((searchValue: string, item: DataItem) => boolean) | - |
| inputProps | Props passed to the search Input, see Input for the options. value and onChange belong to Transfer; to search from outside, call the search method on the ref. | InputProps | - |
| loading | Whether the options on the left are loading; a Spin replaces the list while they load. | boolean | - |
| onChange | Called when the selection changes, including after a drag sort, with the array of values and the array of their items. | Type(values: Array<string | number>, items: Array<DataItem>) => void | - |
| onDeselect | Called when an item is unchecked, with that item. | Type(item: DataItem) => void | - |
| onSearch | Called when the text of the search box changes, with the search value. A search driven by the ref's search method does not call it. | Type(searchValue: string) => void | - |
| onSelect | Called when an item is checked, with that item. | Type(item: DataItem) => void | - |
| pagination | Paging of the left panel, for list and groupList only. onPageChange lives inside the object. | PaginationProps | - |
| ref | Receives a TransferHandle that exposes only the search method; it is not the root element. | TypeReact.Ref<TransferHandle> | - |
| renderSelectedHeader | Replaces the header of the right panel; called on every render with SelectedHeaderProps. | Type(headProps: { num: number; showButton: boolean; onClear: () => void; }) => React.ReactNode | - |
| renderSelectedItem | Renders one selected item on the right, called once per item on every render. The argument is the item plus onRemove; with draggable it also has sortableHandle. With treeList and showPath, fullPath holds the whole path. | Type(item: RenderSelectedItemProps) => React.ReactNode | - |
| renderSelectedPanel | Replaces the whole right panel; called on every render with SelectedPanelProps. | Type(selectedPanelProps: SelectedPanelProps) => React.ReactNode | - |
| renderSourceHeader | Replaces the header of the left panel; called on every render with SourceHeaderProps. leafOnlyNum is provided for treeList only. | Type(headProps: { num: number; showButton: boolean; allChecked: boolean; onAllClick: () => void; leafOnlyNum?: number; }) => React.ReactNode | - |
| renderSourceItem | Renders one candidate on the left, called once per item on every render. The argument is the item plus checked and onChange, which toggles the checked state of the item. | Type(item: RenderSourceItemProps) => React.ReactNode | - |
| renderSourcePanel | Replaces the whole left panel; called on every render with SourcePanelProps. | Type(sourcePanelProps: SourcePanelProps) => React.ReactNode | - |
| showPath | treeList only: whether the selected items on the right show their path from the root. | boolean | false |
| style | Inline style of the root element. | React.CSSProperties | - |
| treeProps | treeList only: props passed to the Tree on the left, overriding the defaults multiple, disableStrictly, leafOnly, filterTreeNode, defaultExpandAll and searchRender (false). value, treeData, keyMaps, treeDataSimpleJson, onChangeWithObject, onChange and ref belong to Transfer and are ignored. | TypeOmit<TreeProps, 'value' | 'ref' | 'onChange' | 'keyMaps' | 'treeData' | 'treeDataSimpleJson' | 'onChangeWithObject'> | - |
| type | Transfer type: list, groupList or treeList. | Type | "list" |
| value | Values of the selected items. Passing it makes the component controlled, and its order is the order on the right; undefined returns to uncontrolled. | TypeArray<string | number> | - |
| virtualize | Virtualizes the selected list on the right, only with the default right panel and draggable off. A numeric height applies in pixels; any other value is measured from the remaining panel height. itemSize is the height of each row. | VirtualizeProps | - |
Methods
| Method | Description | Type |
|---|---|---|
| search | Searches as if the value were typed into the search box, and puts the value there; onSearch is not called. | Type(value: string) => void |
Native attributes not listed above, such as id, aria-* and event handlers, land on the root div; with aria-label or aria-labelledby and no role, the root element gets role="group". tabIndex is set on the search box, including a value injected by a wrapping Tooltip. Pass the props of the search box through inputProps, and those of the left Tree of a treeList through treeProps.
In @fibrous/semi-ui-react, the types of the interfaces below are exported as TransferDataItem for Item, TransferGroupItem for GroupItem and TransferTreeItem for TreeItem.
Item Interface
| props | description | data type | default |
|---|---|---|---|
| disabled | Whether to disable | boolean | false |
| key | Required, unique identification of each option, no repetition is allowed | string | number | |
| label | Options display content | ReactNode | |
| fullPath | When type="treeList" and showPath is true, returns the full path node array from the root node to the current node | Array<Item> | |
| value | The value represented by the option | string | number |
GroupItem Interface
A group is not an item itself, it only has the following properties
| props | description | data type | default |
|---|---|---|---|
| children | Elements of the group | array<Item> | |
| title | Group Name | string |
TreeItem Interface
TreeItem inherits all the properties of Item
| props | description | data type | default |
|---|---|---|---|
| children | Children Items | array<TreeItem> |
VirtualizeProps Interface
| props | description | data type | default |
|---|---|---|---|
| height | Height of the virtualized list. If it is a number, it takes effect directly. If it is a string (e.g. 100%), it adapts to remaining height | number | string | |
| width | Width of the virtualized list | number | string | |
| itemSize | Fixed height of each item row | number |
PaginationProps Interface
| props | description | data type | default |
|---|---|---|---|
| currentPage | Current page number (controlled mode) | number | |
| defaultCurrentPage | Default current page number (uncontrolled mode) | number | 1 |
| pageSize | Number of items per page | number | 10 |
| onPageChange | Callback when page changes | (currentPage: number) => void |
Methods
Methods on the TransferHandle that ref receives can be called through the ref:
const transferRef = useRef<TransferHandle>(null);
<Transfer ref={transferRef} />;
transferRef.current?.search('Option 1');
Design Tokens
| Sass variable | Value | Usage |
|---|---|---|
| $transition_duration_transfer_item-bg | var(--semi-transition_duration-none) | 穿梭框条目-背景色-动画持续时间 |
| $transition_function_transfer_item-bg | var(--semi-transition_function-easeIn) | 穿梭框条目-背景色-过渡曲线 |
| $transition_delay_transfer_item-bg | var(--semi-transition_delay-none) | 穿梭框条目-背景色-延迟时间 |
| $color-transfer-bg | var(--semi-color-bg-1) | 穿梭框背景颜色 - 默认 |
| $color-transfer-border | var(--semi-color-border) | 穿梭框描边颜色 - 默认 |
| $color-transfer_header-text | var(--semi-color-text-2) | 穿梭框 header 文字颜色 |
| $color-transfer_empty-text | var(--semi-color-text-2) | 穿梭框空状态文字颜色 |
| $color-transfer_group_title-text | var(--semi-color-text-2) | 穿梭框分组标题文字颜色 |
| $color-transfer_disabled-text | var(--semi-color-disabled-text) | 穿梭框禁用文本文字颜色 |
| $color-transfer_close_icon-icon | var(--semi-color-text-2) | 穿梭框关闭按钮颜色 |
| $color-transfer_item-text | var(--semi-color-text-1) | 穿梭框常规文字颜色 |
| $color-transfer_selected_item-text | var(--semi-color-text-0) | 穿梭框选中条目文字颜色 |
| $color-transfer_item-bg-hover | var(--semi-color-fill-0) | 穿梭框条目背景色 - 悬浮 |
| $color-transfer_item-bg-active | var(--semi-color-fill-1) | 穿梭框条目背景色 - 按下 |
| $width-transfer-border | 1px | 穿梭框描边宽度 |
| $width-transfer-minWidth | 402px | 穿梭框整体最小宽度 |
| $height-transfer | 400px | 穿梭框高度 |
| $height-transfer_header | 24px | 穿梭框 header 高度 |
| $height-transfer_item-minHeight | 36px | 穿梭框条目最小高度 |
| $width-transfer_item_close-icon | $width-icon-small | 穿梭框选中条目删除按钮宽度 |
| $width-transfer_left | 50% | 穿梭框左侧面板宽度 |
| $width-transfer_right | 50% | 穿梭框左侧面板宽度 |
| $height-transfer_left_empty | 36px | 穿梭框空状态高度 |
| $width-transfer_left-border | $border-thickness-control | 穿梭框双侧面板分割线宽度 |
| $height-transfer_right_header | 32px | 穿梭框 header 高度 |
| $height-transfer_right_empty | 100% | 穿梭框右侧空状态宽度 |
| $width-transfer_empty | 100% | 穿梭框整体空状态宽度 |
| $height-transfer_group_title | 28px | 穿梭框分组标题高度 |
| $spacing-transfer_header-marginTop | 12px | 穿梭框 header 顶部外边距 |
| $spacing-transfer_header-marginRight | 12px | 穿梭框 header 右侧外边距 |
| $spacing-transfer_header-marginBottom | 8px | 穿梭框 header 底部外边距 |
| $spacing-transfer_header-marginLeft | 12px | 穿梭框 header 左侧外边距 |
| $spacing-transfer_header_all-marginLeft | $spacing-base | 穿梭框全选按钮左侧外边距 |
| $spacing-transfer_item-paddingTop | $spacing-tight | 穿梭框条目顶部内边距 |
| $spacing-transfer_item-paddingRight | 9px | 穿梭框条目右侧内边距 |
| $spacing-transfer_item-paddingBottom | $spacing-tight | 穿梭框条目底部内边距 |
| $spacing-transfer_item-paddingLeft | $spacing-base-tight | 穿梭框条目左侧内边距 |
| $spacing-transfer_right_header-marginTop | $spacing-base-tight | 穿梭框右侧面板 header 顶部外边距 |
| $spacing-transfer_right_header-marginBottom | 0 | 穿梭框右侧面板 header 底部外边距 |
| $spacing-transfer_right_item_drag_handler-marginRight | 4px | 穿梭框拖动图标右侧外边距 |
| $spacing-transfer_filter-marginTop | 12px | 穿梭框搜索顶部外边距 |
| $spacing-transfer_filter-marginRight | 12px | 穿梭框搜索右侧外边距 |
| $spacing-transfer_filter-marginBottom | 0 | 穿梭框搜索底部外边距 |
| $spacing-transfer_filter-marginLeft | 12px | 穿梭框搜索左侧外边距 |
| $spacing-transfer_group_title-paddingLeft | $spacing-base-tight | 穿梭框分组标题左侧内边距 |
| $radius-transfer | var(--semi-border-radius-medium) | 穿梭框圆角 |
| $font-transfer_header_all-fontWeight | 600 | 穿梭框字重 |