Chapter 11 Data
Tree
Nested items that open and close, such as folders or menu categories. One Tab stop: the arrow keys move, Right opens a branch and Left closes it, typing a name jumps to it, and Enter or Space selects. For a site's page links, use navigation instead.
import { TreeComponent, type TreeNode } from '@okaapi-ui/angular';
// On the Angular CDK tree.Menu categories
The selected item is bold as well as highlighted, so it never depends on colour.
Selected: pasta
<ui-tree label="Menu" [nodes]="menu" [expanded]="['mains']" [(selected)]="selected" />
menu: TreeNode[] = [
{ id: 'mains', label: 'Mains', children: [
{ id: 'grill', label: 'Grill', children: [{ id: 'kofta', label: 'Kofta' }] },
{ id: 'pasta', label: 'Pasta' },
] },
{ id: 'desserts', label: 'Desserts' },
];In Arabic
<ui-tree label="القائمة" [nodes]="menu" [expanded]="['mains']" />When to use something else
| If you need | Use |
|---|---|
| A flat list with columns | Data table |
| Sections that open and close | Accordion |
Inputs
| Name | Type | Default | What it does |
|---|---|---|---|
label | string | — | Required. Names the tree: "Menu", "Files". |
nodes | TreeNode[] | — | Required. { id, label, children? }, nested as deep as needed. |
expanded | string[] | [] | Ids of the branches open at the start. |
[(selected)] | string | null | null | Id of the selected item. |
Keyboard and screen readers
| Keys | What they do |
|---|---|
| Tab | Reach the tree; it is one Tab stop. |
| Up or Down | Move to the previous or next item. |
| Right | Open a branch, or move into it. |
| Left | Close a branch, or move to its parent. |
| Letters | Jump to the item that starts with them. |
| Enter or Space | Select the item. |
- Read as a tree: each item says its level, whether it is open, and whether it is selected.