JGuang

Tree

Tree · Arco Design

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

Tree

For content with many levels, such as folders, catalogs, and organizational structures, the tree can clearly show their hierarchical relationship, and has interactive functions such as expanding, collapsing, and selecting.

Install

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

Import @arco-design/web-react/dist/css/arco.css at the entry and set the locale with ConfigProvider. React 19 projects must also import @arco-design/web-react/es/_util/react-19-adapter first, or imperative APIs such as Message and Modal cannot render.

Examples

  • Basic: Give each TreeNode node a globally unique key (required), and the title is the content to be displayed on the node.
  • From TreeData
  • BlockNode: The treeNode occupy the remaining horizontal space.
  • Multiple Selection: Add multiple={true} to Tree to enable multiple selection.
  • Checkable: Add the checkable attribute to display the checkbox, and you can use defaultCheckedKeys to specify which nodes are checked by default.
  • Controlled: You can specify the selectedKeys or checkedKeys or expandedKeys property of the tree to turn the tree into a controlled mode, and operate on the return value in the onSelect / onCheck / onExpand callback.
  • Dynamic Loading: Load nodes dynamically.
  • Draggable: Draggable nodes.
  • Checked Strategy: Add checkedStrategy to set the return value when selected.
  • Show Line: Add the showLine property to Tree to display the connecting line.
  • Size
  • Node Icon: The property icon of TreeNode can specify an icon for the node.
  • Extra Node: The renderExtra property of Tree can customize node content.
  • Component Icons: The components loadingIcon, dragIcon, and switcherIcon can be set through the icons property.
  • Virtual List: By specifying virtualListProps to turn on the virtual list, high performance can be obtained when a large amount of data is used.
  • Searchable
  • Customize treeData: You can customize treeData by fieldNames.

Examples come from the official Arco Design repository (MIT) at the commit published as npm 2.66.18. Some use online images or upload endpoints; network requests are disabled in previews.

API

Tree

PropertyDescriptionTypeDefaultValueVersion
autoExpandParentWhether to automatically expand the parent nodebooleantrue-
blockNodeWhether to occupy the remaining horizontal spaceboolean--
checkableWhether to add a checkbox before the nodeboolean--
checkStrictlyWhether to cancel the parent-child associationboolean--
draggableWhether the node can be draggedboolean--
multipleWhether to support multiple selectionboolean--
selectableWhether can be selectedbooleantrue-
showLineWhether to display the connection lineboolean--
sizeTo set size'mini' | 'small' | 'default' | 'large'--
actionOnClickThe action when click nodeActionOnClick | ActionOnClick[]-select
allowDropWhether to allow dropping on node. (dragNode in 2.23.0)AllowDrop() => true2.7.0
checkedKeysThe key of the checked node(controlled)string[]--
checkedStrategyCustomize the return value <br/> all: Return all selected nodes<br/> parent:Only return the parent node when both parent and child nodes are selected <br/> child: Return only child nodesSHOW_ALL | SHOW_PARENT | SHOW_CHILDall-
classNameAdditional css classstring | string[]--
defaultCheckedKeysThe key of node checked by defaultstring[]--
defaultExpandedKeysThe key of node expanded by defaultstring[]--
defaultSelectedKeysThe key of node selected by defaultstring[]--
expandedKeysThe key of the expanded node (controlled)string[]--
fieldNamesCustom field name for key, title, isLeaf, disabled and childrenFieldNamesType-2.11.0
halfCheckedKeysthe keys of half checkedstring[]-2.27.0
loadMoreCallback when loaded data asynchronously, returning a Promise.(node: NodeInstance) => Promise<void>--
onDragEndCallback when the node ends drag(e: DragEvent<HTMLSpanElement>, node: NodeInstance) => void--
onDragLeaveCallback when the node leaves a valid target(e: DragEvent<HTMLSpanElement>, node: NodeInstance) => void--
onDragOverCallback when the node is dragged onto a valid target(e: DragEvent<HTMLSpanElement>, node: NodeInstance) => void--
onDragStartCallback when the node starts dragging(e: DragEvent<HTMLSpanElement>, node: NodeInstance) => void--
onDropCallback when the node is dropped on the valid target(info: {e: DragEvent<HTMLSpanElement>;dragNode: NodeInstance | null;dropNode: NodeInstance | null;dropPosition: number;}) => void--
selectedKeysThe key of the selected node(controlled)string[]--
styleAdditional styleCSSProperties--
treeDataThe tree structure can be generated by passing in treeDataTreeDataType[]--
virtualListPropsPass the virtual-list properties, pass in this parameter to turn on virtual scrollingAvailableVirtualListProps-2.11.0
iconsCustomize node icon| ((nodeProps: NodeProps) => {dragIcon?: ReactNode;switcherIcon?: ReactNode;loadingIcon?: ReactNode;})| {dragIcon?: ReactNode;switcherIcon?: ReactNode;loadingIcon?: ReactNode;}FieldNamesType2.9.0
onCheckCallback when checked node(checkedKeys: string[],extra: {node: NodeInstance;checkedNodes: NodeInstance[];checked: boolean;halfCheckedKeys: string[];halfCheckedNodes: NodeInstance[];e: Event;}) => void--
onExpandCallback when expanded or collapsed node(expandedKeys: string[],exra?: { expanded: boolean; node: NodeInstance; expandedNodes: NodeInstance[] }) => void--
onSelectCallback when selected node(selectedKeys: string[],extra: {selected: boolean;selectedNodes: NodeInstance[];node: NodeInstance;e: Event;}) => void--
renderExtraRender additional node(props: NodeProps) => ReactNode--
renderTitleCustomize title rendering(props: NodeProps) => ReactNode--

Tree.Node

PropertyDescriptionTypeDefaultValue
checkableWhether to show checkboxboolean-
disableCheckboxWhether to disable checkboolean-
disabledWhether to disable nodeboolean-
draggableWhether the current node can be draggedboolean-
isLeafIs it a leaf node. Effective when loading dynamicallyboolean-
selectableWhether it can be selectedbooleantrue
iconsCustomize node icons, it have higher priority than Tree. When the icons property on the Tree is set at the same time, will be merged.TreeProps['icons']-
iconCustom iconReactNode-
titleTitle for nodestring | ReactNode-
classNameAdditional css classstring | string[]-
styleAdditional styleCSSProperties-

AllowDrop

export type AllowDrop = (options: {
  dropNode: NodeInstance;
  dragNode: NodeInstance | null;
  dropPosition: number;
}) => boolean;

TreeDataType

export type TreeDataType = NodeProps & {
  key?: string;
  _index?: number;
  children?: TreeDataType[];
  [key: string]: any;
};

FieldNamesType

export type FieldNamesType = {
  key?: string;
  title?: string;
  disabled?: string;
  children?: string;
  isLeaf?: string;
  disableCheckbox?: string;
  checkable?: string;
};

AvailableVirtualListProps

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

ActionOnClick

export type ActionOnClick = "select" | "check" | "expand";

NodeInstance

export type NodeInstance = ReactElement<
  PropsWithChildren<NodeProps>,
  typeof TreeNode
>;

VirtualListProps

PropertyDescriptionTypeDefault
heightViewable area height (2.11.0 starts to support string type such as 80%)number200
thresholdThe threshold of the number of elements that automatically enable virtual scrolling, pass in null to disable virtual scrolling.number | null100
isStaticItemHeightWhether it is a static element of the same heightbooleantrue