JGuang

数据穿梭框

Transfer · Arco Design

交互预览
npm install @arco-design/web-react@2.66.18

数据穿梭框 Transfer

两栏布局的多选组件,将元素从一栏即时移到另一栏。

安装

npm install @arco-design/web-react@2.66.18

在入口引入 @arco-design/web-react/dist/css/arco.css,用 ConfigProvider 设置语言。React 19 项目还需在入口最先引入 @arco-design/web-react/es/_util/react-19-adapter,否则 Message、Modal 等命令式 API 无法渲染。

示例

  • 基础用法:两栏布局的多选穿梭框组件,将元素从一栏即时移到另一栏。
  • 带搜索框:指定 showSearch 来使用带搜索框的穿梭框,可以自定义搜索函数。
  • 单向模式:指定 oneWay 使用单向模式的穿梭框。
  • 高级用法:穿梭框高级用法,可配置操作文案,可定制宽高。
  • 分页:数据量大时,指定 pagination 来使用分页。
  • 自定义渲染行数据:通过 render 自定义渲染每一个项目,可用于渲染复杂数据。
  • 表格穿梭框:使用 Table 组件作为自定义渲染列表。
  • 树穿梭框:使用 Tree 组件作为自定义渲染列表。
  • 简单模式:指定 simple 来开启简单模式,点击选项即可移动。额外配置 simple = { retainSelectedItems: true },将选中的条目保留在左侧。
  • 拖拽排序:指定 draggable 属性为 true,可拖拽排序左右面板。
  • 自定义标题栏:titleTexts 允许传入 render 函数以完全自定义 Transfer 的标题栏,函数接收的参数为 { countTotal: number; countSelected: number; checkbox: ReactNode; clear: () => void }。

示例来自 Arco Design 官方仓库(MIT),固定在 npm 2.66.18 对应的提交;部分示例使用在线图片或上传地址,预览中网络请求被禁用。

API

Transfer

参数名描述类型默认值版本
disabled禁用穿梭框boolean--
draggable列表内条目是否可拖拽boolean--
oneWay单向boolean--
onChange选中项在两栏之间转移时的回调(newTargetKeys: string[], direction: 'source' | 'target', moveKeys: string[]) => void--
onSearch搜索框输入进行搜索时回调函数(value: string, type?: 'source' | 'target') => void--
className节点类名string | string[]--
dataSource穿梭框数据源,其中一部分会被渲染到左边一栏,targetKeys 中指定的除外TransferItem[]--
defaultSelectedKeys默认的 selectKeysstring[][]-
defaultTargetKeys默认的 targetKeysstring[][]-
listStyle左右两栏框的样式,通过数组为左右列表传入不同属性CSSProperties | CSSProperties[]-Array format in '2.40.0'
onDragEnd节点结束拖拽的回调(e: DragEvent<HTMLSpanElement>, item: TransferItem) => void--
onDragLeave节点离开可释放目标上时的回调(e: DragEvent<HTMLSpanElement>, item: TransferItem) => void--
onDragOver节点被拖拽至可释放目标上时的回调(e: DragEvent<HTMLSpanElement>, item: TransferItem) => void--
onDragStart节点开始拖拽的回调(e: DragEvent<HTMLSpanElement>, item: TransferItem) => void--
onDrop节点在可释放目标上释放时的回调(info: {e: DragEvent<HTMLSpanElement>;dragItem: TransferItem;dropItem: TransferItem;dropPosition: number;}) => void--
operationStyle穿梭中间操作部分的样式CSSProperties--
operationTexts穿梭按钮的文案数组,顺序从上至下string[] | ReactNode[]--
pagination是否使用翻页,也可传入 Pagination 的配置,通过数组为左右列表传入不同属性boolean | PaginationProps | Array<boolean | PaginationProps>-Array format in '2.40.0'
searchPlaceholder搜索框默认提示文字,通过数组为左右列表传入不同属性string | string[]-Array format in '2.40.0'
selectedKeys当前应该有哪些项被选中string[]--
showFooter左右两栏是否显示底部重置按钮,通过数组为左右列表传入不同属性boolean | ReactNode | Array<boolean | ReactNode>-ReactNode in 2.11.0, array format in '2.40.0'
showSearch左右两栏是否显示搜索框,通过数组为左右列表传入不同属性boolean | InputProps | Array<boolean | InputProps>-Array format in '2.40.0'
simple简单模式| boolean| {retainSelectedItems?: boolean;}-retainSelectedItems in '2.21.0'
style节点样式CSSProperties--
targetKeys渲染到右边一栏数据的 key 集合string[]--
titleTexts穿梭框左右栏标题数组。(函数写法 2.18.0 开始支持)Array<TransferListTitle>['Source', 'Target']-
virtualListProps传递虚拟滚动属性。AvailableVirtualListProps-2.42.0
children自定义列表渲染函数(props: TransferCustomListProps) => ReactNode--
filterOption搜索框筛选算法(inputValue: string, item: TransferItem) => boolean(inputValue, item) => item.value.indexOf(inputValue) !== -1-
onResetData点击重置按钮后的回调() => void--
onSelectChange数据项选中状态发生改变的回调(leftSelectedKeys: string[], rightSelectedKeys: string[]) => void--
render每行数据渲染函数(item: TransferItem) => any--

TransferCustomListProps

export interface TransferCustomListProps
  extends Pick<
    TransferListProps,
    | "disabled"
    | "listType"
    | "selectedKeys"
    | "validKeys"
    | "selectedDisabledKeys"
  > {
  filteredItems: TransferItem[];
  onItemSelect: (key: string, selected: boolean) => void;
  onItemRemove: (key: string) => void;
  onItemSelectAll: (keys: string[], selected: boolean) => void;
}

TransferItem

export type TransferItem = {
  key: string;
  value: string;
  disabled?: boolean;
};

TransferListTitle

type TransferListTitle =
  | string
  | ((params: {
      countTotal: number;
      countSelected: number;
      clear: () => void;
      checkbox: ReactNode;
      searchInput: ReactNode;
    }) => ReactNode);

AvailableVirtualListProps

export type AvailableVirtualListProps = Pick<
  VirtualListProps<any>,
  | "height"
  | "itemHeight"
  | "threshold"
  | "isStaticItemHeight"
  | "scrollOptions"
  | "onScroll"
  | "wrapperChild"
>;

Transfer.Item

PropertyDescriptionTypeDefault
key选项的键值(唯一标识符)string (required)-
value选项对应的值string (required)-
disabled是否禁用此选项boolean-