Input · Transfer
Transfer
A more intuitive and efficient multiple-selection selector, which can reveal more information about options, and supports search functions. The disadvantage is that it takes up more space
Demos
How to import
Basic Usage
dataSource should have value、label、key.
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
Custom filtering logic, custom option data rendering
Use
When type is
Using
Using
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.Disabled
Drag and drop sort
Set
draggable
to true to enable the drag sort function. Support after v1.11.0Drag 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 (sortableHandle is provided after v 1.22.0)Custom rendering header information in panel
Semi has provided
The parameter types are as follows:
renderSourceHeader
and renderSelectedHeader
parameter allows users to customize the header information of the left and right panels since version 2.29.0.renderSourceHeader: (props: SourceHeaderProps) => ReactNode
renderSelectedHeader: (props: SelectedHeaderProps) => ReactNode
The parameter types are as follows:
The example is as follows:
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 structurerenderSelectedPanel: (selectedPanelProps: SelectedPanelProps) => ReactNode
SelectedPanelProps
contains the following parametersFully 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 or react-sortable-hoc, quickly realize the function. Regarding the selection of the two, here are some of our suggestions.
- Both are maintained by the same author, dnd-kit is the successor of react-sortable-hoc
- The API design of react-sortable-hoc is more cohesive, and the code is more concise in simple scenarios. But it strongly relies on the findDOMNode API, which will be deprecated in future React versions. At the same time, the library has not been maintained for the past two years.
- Relatively speaking, dnd-kit has a certain threshold for getting started, but it has a higher degree of freedom, stronger scalability, and is still under maintenance. we recommend it.
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 react-sortable-hoc:
Example using dnd-kit,The core dependencies that need to be used are @dnd-kit/sortable, @dnd-kit/core. The core hooks are useSortable, and the usage instructions of useSortable are as follows
Tree Transfer
The input type is
treeList
, and the Tree
component is used as a custom rendering list. v1.20.0 availableThe properties of the default tree can be overridden by treeProps(TreeProps). The default properties of the tree on the left are
Accessibility
ARIA
- Add
role
search
to the search box - Add
role
list
to the selected list on the right, addrole
listitem
to the selected item
API Reference
Transfer Props
props | description | data type | default | version |
---|---|---|---|---|
className | Style class name | string | ||
dataSource | Data Source | Array<Item>|Array<GroupItem>|Array<TreeItem> | [] | |
defaultValue | Default selected value | Array<string|number> | ||
disabled | Whether to disable | boolean | false | |
draggable | Whether to enable drag sorting | boolean | false | |
emptyContent | Custom empty state prompt text, search is the text displayed when there are no search results, left is the text when there is no source data on the left, and right is the prompt text when no data is checked | {left: ReactNode; right: ReactNode; search: ReactNode;} | ||
filter | Custom filter logic, when false, the search box is not displayed. 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 . | boolean | (input:string, item: Item) => boolean | true | |
inputProps | Can be used to customize the search box Input, the configurable properties refer to the Input component, the value and onChange parameters will be used inside Transfer, users should not use them. If you want to search through external data, you can call the search method of Transfer | InputProps | ||
loading | Whether the left option is being loaded | boolean | - | |
onChange | The callback that is triggered when the selected value changes, and the callback is also triggered after the drag sort changes | (values: Array<string|number>, items: Array<Item>) => void | ||
onDeselect | Callback when unchecking | (item: Item) => void | ||
onSearch | Called when the input content of the search box changes | (inputValue: string) => void | ||
onSelect | Callback when checked | (item: Item) => void | ||
renderSelectedHeader | Customize the rendering of the header information on the right panel | (props: SelectedHeaderProps) => ReactNode | 2.29.0 | |
renderSelectedItem | Customize the rendering of a single selected item on the right | (item: {onRemove, sortableHandle} & Item) => ReactNode | ||
renderSelectedPanel | Customize the rendering of the selected panel on the right | (selectedPanelProps) => ReactNode | 1.11.0 | |
renderSourceHeader | Customize the rendering of the header information on the left panel | (props: SourceHeaderProps) => ReactNode | 2.29.0 | |
renderSourceItem | Customize the rendering of a single candidate item on the left | (item: {onChange, checked} & Item) => ReactNode | ||
renderSourcePanel | Customize the rendering of the left candidate panel | (sourcePanelProps) => ReactNode | 1.11.0 | |
showPath | When the type is treeList , control whether the selected item on the right shows the selection path | boolean | false | 1.20.0 |
style | Inline style | CSSProperties | ||
treeProps | When the type is treeList , it can be passed as TreeProps to the Tree component on the left | TreeProps | 1.20.0 | |
type | Transfer type, optional list , groupList , treeList | string | 'list' | 1.20.0 |
value | The selected value, when the item is passed in, it will be used as a controlled component | Array<string|number> |
Item Interface
props | description | data type | default | version |
---|---|---|---|---|
className | Style class name | string | ||
disabled | Whether to disable | boolean | false | |
key | Required, unique identification of each option, no repetition is allowed | string | number | ||
label | Options display content | ReactNode | ||
style | Inline style | CSSProperties | ||
value | The value represented by the option | string | number |
GroupItem Interface
GroupItem inherits all the properties of Item
props | description | data type | default | version |
---|---|---|---|---|
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> |
Methods
Some internal methods provided by Transfer can be accessed through ref:
Name | Description |
---|---|
search(value: string) | You can call this method through ref to search, and the search value will be set to Input. |