Skip to content

CdsTree

A tree shows a hierarchy: catalog sections, project files, an org structure. Nodes expand, get selected by a click or with checkboxes, load from the server and can be dragged, while virtual scrolling keeps everything smooth even with thousands of nodes.

Storybook GitLab Figma
Test coverage: 82% — partial coverage
API in specs
116 / 116 props, events, slots and exposed members with a test
Test cases
152 unit · 8 browser · 1 planned
Known bugs
23 pinned by an it.fails test
Code coverage
81.5% lines · 71.8% branches measured in v4.0.0-alpha.13
A11y (axe)
38 recommendations in 14 stories measured in v4.0.0-alpha.11

For a flat set without nesting take CdsList, for navigation between sections take CdsMenu, and for collapsible sections of content take CdsExpandablePanel.

Anatomy ​

A tree node is made of the nesting indent, the expand switcher and the content with the title. The checkbox appears with the checkable prop, the node icon with show-icon.

Variations ​

Expanding nodes ​

The data of the tree is an array of { title, key, children } nodes in the tree-data prop. The expanded state is driven by v-model:expanded-keys; if you do not need to control it, set the initial state through default-expanded-keys or default-expand-all.

The expand-action prop expands a node by a click on the whole row: click for a single click, dblclick for a double one. Without it a node expands only from the switcher.

Selecting nodes and checkboxes ​

The selectable prop turns on selecting a node by a click (v-model:selected-keys), multiple allows selecting several nodes. The checkable prop adds checkboxes with their own v-model:checked-keys state: checking a parent checks all its descendants, and a partially checked parent goes into the indeterminate state (turned off with the check-strictly prop). Individual nodes are disabled with the disabled and disableCheckbox flags right in the data.

Connecting lines ​

The show-line prop draws lines between the nodes — in deep trees this makes it easier to follow the levels. The { showLeafIcon } object controls the icon of the leaf nodes.

Icons and slots ​

Slots customize every part of a node; they receive the node context — key, title, expanded, selected and other fields.

SlotDescription
defaultDeclarative description of the nodes in markup instead of tree-data
iconIcon of the node. Shown with show-icon
switcherIconIcon of the expand switcher
titleContent of the node title

Asynchronous loading ​

The load-data prop loads the child nodes on the first expand: the function receives the node, returns a promise and adds children to the data. Until the promise resolves, the node shows a loading indicator.

Dragging ​

The draggable prop allows dragging the nodes; the { icon: true } variant shows a drag handle. The tree does not change the data by itself — the @drop handler rebuilds tree-data, and allow-drop limits the valid drop targets.

Virtual scrolling ​

The virtual list is on by default (the virtual prop). Set height and the tree scrolls inside a fixed height, rendering only the visible nodes: thousands of rows do not slow the page down.

Directory mode ​

CdsTreeDirectory is a tree with presets for files and folders: directory icons, expanding on click and full-width nodes. It accepts the same props as CdsTree.

Guidelines ​

Scenarios ​

Picking sections for an export ​

Checkboxes collect a set of sections, and the counter in the button hints at the size of the selection. The loading prop of the button shows the export in progress, disabled prevents starting it with an empty selection.

Search across the tree ​

An input field filters tree-data on the fly: the matches and their parents stay in the tree, and v-model:expanded-keys expands the branches with the found nodes. When there are no matches, show an empty state instead of the tree.

Project files ​

CdsTreeDirectory with v-model:selected-keys works as a file explorer: a click on a folder expands it, a click on a file selects it. The node keys hold the path to the file, so the selected item is ready to be shown or requested right away.

API ​

CdsTree ​

Props

NameTypeDefaultDescription
allowDropNTree.TAllowDrop<NTree.IDataNode> | undefinedtrueDecides whether a node accepts a drop
autoExpandParentboolean | undefinedfalseExpands the parent node automatically
blockNodeboolean | undefinedfalseThe tree node fills all the remaining horizontal space
checkableboolean | undefinedfalseAdds a checkbox before the tree nodes
checkedKeysNTree.TKey[] | { checked: NTree.TKey[]; halfChecked: NTree.TKey[]; } | undefined—(v-model) Keys of the checked tree nodes. When the key of a parent node is given, all its child nodes become checked; and the other way around, when the key of a child node is given, its parent node becomes checked too. When checkable and checkStrictly are both true, the object has the checked and halfChecked fields, and checking a child or a parent node no longer affects the other.
checkStrictlyboolean | undefinedfalseChecks only the node itself, without its parents and children.
defaultExpandAllboolean | undefinedfalseExpands all the nodes by default
defaultExpandedKeysNTree.TKey[] | undefined[]Expands all the nodes by default
defaultExpandParentboolean | undefinedtrueExpands the parent nodes by default
defaultSelectedKeysNTree.TKey[] | undefined—Tree nodes selected by default
disabledboolean | null | undefinednullDisabled state
draggableboolean | NTree.TDraggableConfig | undefinedfalseAllows dragging the tree nodes
expandActionNTree.TExpandAction | undefined—Action that expands or collapses the tree nodes
expandedKeysNTree.TKey[] | undefined—(v-model) Keys of the expanded tree nodes.
fieldNamesNTree.IFieldNames | undefined—Maps the title, key and children fields of a tree node to the matching fields of treeData
filterTreeNode((treeNode: NTree.IEventDataNode) => boolean) | undefined—Function that filters (highlights) the tree nodes. When it returns true, the node is highlighted.
heightnumber | undefined—Height of the virtual scrolling list
icon((nodeProps: NTree.ITreeNodeAttribute) => any) | undefined—Icon of the tree node (also available through slot#icon)
loadData((treeNode: NTree.IEventDataNode) => Promise<any>) | undefined—Loads the data of a tree node asynchronously
loadedKeysNTree.TKey[] | undefined[]Keys of the already loaded tree nodes. Required to work with loadData
multipleboolean | undefinedfalseAllows selecting several tree nodes
openAnimationFunction | undefined—Animation function for expanding and collapsing a tree node
selectableboolean | undefinedfalseMakes the tree nodes selectable
selectedKeysNTree.TKey[] | undefined—(v-model) Keys of the selected tree nodes.
showIconboolean | undefinedfalseShows the icon of the tree node
showLineboolean | { showLeafIcon: string | boolean; } | undefined—Shows the lines between the tree nodes
switcherIcon((nodeProps: SwitcherIconProps) => any) | undefined—Icon of the expand/collapse switcher (also available through slot#switcherIcon)
themestring | undefined—Component theme
treeDataNTree.IDataNode[] | undefined—Data of the tree
virtualboolean | undefinedtrueEnables virtual scrolling of the list inside the tree; enabled by default

Events

NameTypeDescription
check[checked: NTree.TKey[] | { checked: NTree.TKey[]; halfChecked: NTree.TKey[]; }, info: NTree.ICheckInfo]A node was checked with its checkbox.
click[event: MouseEvent, node: NTree.IEventDataNode]Click on a node.
dblclick[event: MouseEvent, node: NTree.IEventDataNode]Double click on a node.
doubleclick[event: MouseEvent, node: NTree.IEventDataNode]Double click on a node (alias).
dragend[info: NTree.TNodeDragEventParams]Dragging of a node finished.
dragenter[info: NTree.TNodeDragEventParams & { expandedKeys: NTree.TKey[]; }]A dragged node entered the drop area.
dragleave[info: NTree.TNodeDragEventParams]A dragged node left the drop area.
dragover[info: NTree.TNodeDragEventParams]A dragged node moves inside the drop area.
dragstart[info: NTree.TNodeDragEventParams]Dragging of a node started.
drop[info: NTree.TNodeDragEventParams & { dragNode: NTree.IEventDataNode; dragNodesKeys: NTree.TKey[]; dropPosition: number; dropToGap: boolean; }]A node was dropped.
expand[expandedKeys: NTree.TKey[], info: { node: NTree.IEventDataNode; expanded: boolean; nativeEvent: MouseEvent; }]A node was expanded or collapsed.
load[loadedKeys: NTree.TKey[], info: { event: "load"; node: NTree.IEventDataNode; }]Children of a node were loaded (async loadData).
rightClick[info: { event: MouseEvent; node: NTree.IEventDataNode; }]Right click on a node.
select[selectedKeys: NTree.TKey[], info: { event: "select"; selected: boolean; node: NTree.IEventDataNode; selectedNodes: NTree.IDataNode[]; nativeEvent: MouseEvent; }]A node was selected.
update:checkedKeys[checkedKeys: NTree.TKey[] | { checked: NTree.TKey[]; halfChecked: NTree.TKey[]; }]Checking of a node changed (v-model).
update:expandedKeys[expandedKeys: NTree.TKey[]]Expansion of a node changed (v-model).
update:selectedKeys[selectedKeys: NTree.TKey[]]Selection of a node changed (v-model).

Slots

NameTypeDescription
defaultanyTree nodes by default (when described declaratively in markup).
iconNTree.ITreeNodeAttributeCustom icon of the tree node.
switcherIconanyCustom icon of the expand/collapse switcher.
titleNTree.IEventDataNodeCustom title of the tree node.

Exposed

NameTypeDescription
halfCheckedKeysanyKeys of the nodes with partially checked children — the “half checked” state.
loadingKeysanyKeys of the nodes whose children are being lazily loaded right now.
scrollToScrollToScrolls the tree to a node by its key or to a given position.
treeRefanyReference to the internal tree instance.

CdsTreeDirectory ​

Props

NameTypeDefaultDescription
allowDropNTree.TAllowDrop<NTree.IDataNode> | undefinedtrueDecides whether a node accepts a drop
autoExpandParentboolean | undefinedfalseExpands the parent node automatically
blockNodeboolean | undefinedfalseThe tree node fills all the remaining horizontal space
checkableboolean | undefinedfalseAdds a checkbox before the tree nodes
checkedKeysNTree.TKey[] | { checked: NTree.TKey[]; halfChecked: NTree.TKey[]; } | undefined—(v-model) Keys of the checked tree nodes. When the key of a parent node is given, all its child nodes become checked; and the other way around, when the key of a child node is given, its parent node becomes checked too. When checkable and checkStrictly are both true, the object has the checked and halfChecked fields, and checking a child or a parent node no longer affects the other.
checkStrictlyboolean | undefinedfalseChecks only the node itself, without its parents and children.
defaultExpandAllboolean | undefinedfalseExpands all the nodes by default
defaultExpandedKeysNTree.TKey[] | undefined[]Expands all the nodes by default
defaultExpandParentboolean | undefinedtrueExpands the parent nodes by default
defaultSelectedKeysNTree.TKey[] | undefined—Tree nodes selected by default
draggableboolean | NTree.TDraggableConfig | undefinedfalseAllows dragging the tree nodes
expandActionstring | false | undefined—Action that expands or collapses the tree nodes
expandedKeysNTree.TKey[] | undefined—(v-model) Keys of the expanded tree nodes.
fieldNamesNTree.IFieldNames | undefined—Maps the title, key and children fields of a tree node to the matching fields of treeData
filterTreeNode((treeNode: NTree.IEventDataNode) => boolean) | undefined—Function that filters (highlights) the tree nodes. When it returns true, the node is highlighted.
heightnumber | undefined—Height of the virtual scrolling list
icon((nodeProps: NTree.ITreeNodeAttribute) => any) | undefined—Icon of the tree node (also available through slot#icon)
loadData((treeNode: NTree.IEventDataNode) => Promise<any>) | undefined—Loads the data of a tree node asynchronously
loadedKeysNTree.TKey[] | undefined[]Keys of the already loaded tree nodes. Required to work with loadData
multipleboolean | undefinedfalseAllows selecting several tree nodes
openAnimationFunction | undefined—Animation function for expanding and collapsing a tree node
selectableboolean | undefinedfalseMakes the tree nodes selectable
selectedKeysNTree.TKey[] | undefined—(v-model) Keys of the selected tree nodes.
showIconboolean | undefinedfalseShows the icon of the tree node
showLineboolean | { showLeafIcon: string | boolean; } | undefined—Shows the lines between the tree nodes
switcherIcon((nodeProps: SwitcherIconProps) => any) | undefined—Icon of the expand/collapse switcher (also available through slot#switcherIcon)
treeDataNTree.IDataNode[] | undefined—Data of the tree
virtualboolean | undefinedtrueEnables virtual scrolling of the list inside the tree; enabled by default

Events

NameTypeDescription
check[checked: NTree.TKey[] | { checked: NTree.TKey[]; halfChecked: NTree.TKey[]; }, info: NTree.ICheckInfo]A node was checked with its checkbox.
click[event: MouseEvent, node: NTree.IEventDataNode]Click on a node.
dblclick[event: MouseEvent, node: NTree.IEventDataNode]Double click on a node.
doubleclick[event: MouseEvent, node: NTree.IEventDataNode]Double click on a node (alias).
dragend[info: NTree.TNodeDragEventParams]Dragging of a node finished.
dragenter[info: NTree.TNodeDragEventParams & { expandedKeys: NTree.TKey[]; }]A dragged node entered the drop area.
dragleave[info: NTree.TNodeDragEventParams]A dragged node left the drop area.
dragover[info: NTree.TNodeDragEventParams]A dragged node moves inside the drop area.
dragstart[info: NTree.TNodeDragEventParams]Dragging of a node started.
drop[info: NTree.TNodeDragEventParams & { dragNode: NTree.IEventDataNode; dragNodesKeys: NTree.TKey[]; dragPosition: number; dropToGap: boolean; }]A node was dropped.
expand[expandedKeys: NTree.TKey[], info: { node: NTree.IEventDataNode; expanded: boolean; nativeEvent: MouseEvent; }]A node was expanded or collapsed.
load[treeNode: NTree.IEventDataNode]Children of a node were loaded (async loadData).
rightClick[info: { event: MouseEvent; node: NTree.IEventDataNode; }]Right click on a node.
select[selectedKeys: NTree.TKey[], info: { event: "select"; selected: boolean; node: NTree.IEventDataNode; selectedNodes: NTree.IDataNode[]; nativeEvent: MouseEvent; }]A node was selected.
update:checkedKeys[checkedKeys: NTree.TKey[] | { checked: NTree.TKey[]; halfChecked: NTree.TKey[]; }]Checking of a node changed (v-model).
update:expandedKeys[expandedKeys: NTree.TKey[]]Expansion of a node changed (v-model).
update:selectedKeys[selectedKeys: NTree.TKey[]]Selection of a node changed (v-model).

CdsTreeNode ​

Props

NameTypeDefaultDescription
activeboolean | undefined—Highlights the node as active
checkableboolean | undefined—Shows a checkbox next to the node
dataNTree.IDataNode | undefinedundefined as unknown as NTree.IDataNodeData of the node — the original list item the node is built from
disableCheckboxboolean | undefined—Disables only the checkbox of the node, leaving the node itself available
disabledboolean | undefined—Disables the node: it cannot be selected, checked or expanded
domRef((arg: any) => void) | undefined—Callback that receives the DOM element of the node after mounting
eventKeyNTree.TKey | undefined''Unique key of the node — the tree uses it to track selection, expansion and checking
iconFunction | undefined—Function that returns the icon of the node
isEndboolean[] | undefined—“Last on its level” flags for every nesting level — the connecting lines are drawn from them
isLeafboolean | undefined—The node is a leaf: it has no children and shows no expand switcher
isStartboolean[] | undefined—“First on its level” flags for every nesting level — the connecting lines are drawn from them
parentNTree.IDataNode | undefinedundefined as unknown as NTree.IDataNodeData of the parent node. Not set for the top-level nodes
prefixClsstring | undefined—Prefix of the CSS classes of the node
selectableboolean | undefined—Allows selecting the node
switcherIconFunction | undefined—Function that returns the icon of the expand switcher
titlestring | (Function & string) | undefined—Tree node title: a string or a render function