Tree

A list of nodes that open to show their children, for files, categories or any nested data. Pass the nodes in data. Clicking a node selects it, and a node with children also opens or closes.

General

Each node has a unique value and a label, and children for the nodes inside it. Name the tree with aria-label.

Controlled

Use value and onChange for the selected node, and expanded and onExpandedChange for the open ones.

Icons and disabled nodes

icon shows any element before the label. A node with disabled is skipped by the keyboard and cannot be selected.

APIs

Tree.Props

AttributeDescriptionTypeAccepted valuesDefault
datathe nodes of the first levelTreeNodeData[]--
valueselected node, to control itstring / null--
initialValuenode selected on the first renderstring / null-null
onChangecalled with the value and the node that was selected(value: string, node: TreeNodeData) => void--
expandedvalue of the open nodes, to control themstring[]--
initialExpandedvalue of the nodes open on the first renderstring[]-[]
onExpandedChangecalled with the value of every open node(expanded: string[]) => void--
...native props, passed to the ulHTMLAttributes'aria-label', 'id', ...-

TreeNodeData

AttributeDescriptionTypeAccepted valuesDefault
valueidentifies the node, unique in the whole treestring--
labelcontent of the nodeReactNode--
iconshown before the labelReactNode--
disabledthe node cannot be selected and is skipped by the keysboolean-false
childrenthe nodes inside this oneTreeNodeData[]--

Keyboard

  • • Up and Down move the focus to the previous and the next node in view.
  • • Right opens a closed node, and on an open one goes to its first child.
  • • Left closes an open node, and on a closed one goes to its parent.
  • • Home and End go to the first and the last node in view.
  • • Enter and Space select the node, and a node with children also opens or closes.

Ref

The ref points at the ul of the tree.

Accessibility

The tree follows the ARIA tree pattern: tree, treeitem and group, with aria-expanded on the nodes that have children, aria-selected, aria-level and aria-disabled. Each node is named by its own label, not by the text of its children. Only one node is in the tab order, the selected one or the first, and the arrow keys move the focus between the others. Name the tree with aria-label.

Edit this page on GitHub