JGuang

树

Tree · Arco Design

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

树 Tree

对于文件夹、分类目录、组织架构等层级较多的内容,树可以清楚显示他们的层级关系,并具有展开、收起、选择等交互功能。

安装

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 无法渲染。

示例

  • 基础用法:为每个 TreeNode 节点赋予全局唯一的 key(必填项),title 为该节点显示的内容。
  • 由treeData直接生成
  • 节点占一行:节点占据一整行。
  • 多选:Tree 设置 multiple 属性为true,可以启用多选。
  • 带复选框的树:为 Tree 添加 checkable 属性即可使树具有复选框功能,可以用 defaultCheckedKeys 指定复选框默认选中的节点。
  • 受控模式:可以指定树的 selectedKeys 或 checkedKeys 或 expandedKeys 属性使树变为受控模式,在对应的 onSelect / onCheck / onExpand 回调中对返回值进行操作。
  • 动态加载:动态加载节点。
  • 拖拽:可拖拽的树节点。
  • 设置回填方式:为 Tree 添加 checkedStrategy 可以设置选中时的回填方式
  • 显示连接线:为 Tree 添加 showLine 属性即可使树具有连接线
  • 不同尺寸
  • 定制节点图标:只需为 TreeNode 指定 icon 属性的值即可为任意节点指定任意图标。
  • 定制额外节点:为 Tree 设置 renderExtra 可以自定义树节点的展示。
  • 定制组件图标:通过icons 属性即可设置组件 loadingIcon,dragIcon,switcherIcon。
  • 虚拟列表:通过指定 virtualListProps 来开启虚拟列表,在大量数据时获得高性能表现。
  • 搜索树
  • 自定义 TreeData 的字段名称:通过 fieldNames 字段可以自定义 TreeData 的字段名。

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

API

Tree

参数名描述类型默认值版本
autoExpandParent是否自动展开父节点booleantrue-
blockNode是否节点占据一行boolean--
checkable是否在节点前添加选框boolean--
checkStrictly是否取消父子节点关联boolean--
draggable是否可拖拽boolean--
multiple是否支持多选boolean--
selectable是否可以选择booleantrue-
showLine是否展示连接线boolean--
size不同尺寸'mini' | 'small' | 'default' | 'large'--
actionOnClick点击节点时对应的操作,可以是选中,复选选中,展开/收起ActionOnClick | ActionOnClick[]-select
allowDrop是否允许拖拽时放置在该节点。 (dragNode in 2.23.0)AllowDrop() => true2.7.0
checkedKeys选中复选框的树节点。(受控)string[]--
checkedStrategy定制回填方式 <br/> all: 返回所有选中的节点<br/> parent: 父子节点都选中时只返回父节点 <br/> child: 只返回子节点SHOW_ALL | SHOW_PARENT | SHOW_CHILDall-
className节点类名string | string[]--
defaultCheckedKeys默认选中复选框的树节点string[]--
defaultExpandedKeys默认展开的节点。string[]--
defaultSelectedKeys默认选中的树节点string[]--
expandedKeys展开的节点,(受控)。string[]--
fieldNames指定 key,title,isLeaf,disabled,children 对应的字段FieldNamesType-2.11.0
halfCheckedKeys半选状态的节点.仅在 checkable 且 checkStrictly 时生效string[]-2.27.0
loadMore异步加载数据的回调,返回一个 Promise。(node: NodeInstance) => Promise<void>--
onDragEnd节点结束拖拽的回调(e: DragEvent<HTMLSpanElement>, node: NodeInstance) => void--
onDragLeave节点离开可释放目标上时的回调(e: DragEvent<HTMLSpanElement>, node: NodeInstance) => void--
onDragOver节点被拖拽至可释放目标上时的回调(e: DragEvent<HTMLSpanElement>, node: NodeInstance) => void--
onDragStart节点开始拖拽的回调(e: DragEvent<HTMLSpanElement>, node: NodeInstance) => void--
onDrop节点在可释放目标上释放时的回调(info: {e: DragEvent<HTMLSpanElement>;dragNode: NodeInstance | null;dropNode: NodeInstance | null;dropPosition: number;}) => void--
selectedKeys选中的树节点。(受控)string[]--
style节点样式CSSProperties--
treeData可以通过传入treeData,生成对应的树结构TreeDataType[]--
virtualListProps传递虚拟列表属性,传入此参数以开启虚拟滚动AvailableVirtualListProps-2.11.0
icons定制节点图标| ((nodeProps: NodeProps) => {dragIcon?: ReactNode;switcherIcon?: ReactNode;loadingIcon?: ReactNode;})| {dragIcon?: ReactNode;switcherIcon?: ReactNode;loadingIcon?: ReactNode;}FieldNamesType2.9.0
onCheck点击树节点复选框的回调(checkedKeys: string[],extra: {node: NodeInstance;checkedNodes: NodeInstance[];checked: boolean;halfCheckedKeys: string[];halfCheckedNodes: NodeInstance[];e: Event;}) => void--
onExpand点击展开/关闭的回调(expandedKeys: string[],exra?: { expanded: boolean; node: NodeInstance; expandedNodes: NodeInstance[] }) => void--
onSelect点击树节点的回调(selectedKeys: string[],extra: {selected: boolean;selectedNodes: NodeInstance[];node: NodeInstance;e: Event;}) => void--
renderExtra渲染额外节点(props: NodeProps) => ReactNode--
renderTitle自定义 title 的渲染(props: NodeProps) => ReactNode--

Tree.Node

参数名描述类型默认值
checkable是否显示多选框boolean-
disableCheckbox是否禁用复选框boolean-
disabled是否禁用节点boolean-
draggable当前节点是否可拖拽boolean-
isLeaf是否是叶子节点。动态加载时有效boolean-
selectable是否允许选中booleantrue
icons定制节点图标,优先级高于 Tree。同时设置 Tree 上的 icons 属性时候,将会进行合并。TreeProps['icons']-
icon该节点个性化显示的图标ReactNode-
title该节点显示的标题string | ReactNode-
className节点类名string | string[]-
style节点样式CSSProperties-

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

参数名描述类型默认值
height可视区高度 (2.11.0 开始支持如 80% 的 string 类型)number200
threshold自动开启虚拟滚动的元素数量阈值,传入null以禁用虚拟滚动。number|null100
isStaticItemHeight是否为相同高度的静态元素booleantrue

常见问题

  1. Tree 设置了autoExpandParent=true,但没有默认展开全部节点?

autoExpandParent 仅在 Tree 第一次挂载的时候生效。如果数据是从远程获取,可以在数据获取完成后,再去渲染 Tree 组件。