# Tree view

Source: https://ecosphere.dev/docs/data/tree-view (Ecosphere 1.5). Install `vue-ecosphere` for Vue 3 or `react-ecosphere` for React 18.2+/19: same components, props, markup and stylesheet.

A hierarchy of links: the ancestor path down to the current node, then its children in branches that open and load the first time they open.

`import { EcoTreeView } from "vue-ecosphere"`

## Ancestor path, current node and loaded branches

Open York (it loads) or Hull (it fails once; Retry). “Show 2 more” appends a page, including a branch that comes back empty.

Vue (vue-ecosphere):

```vue
<EcoTreeView
  label="Hierarchy of North region"
  :nodes="nodes"
  v-model:expanded="expanded"
  @toggle="(node, open) => open && loadChildren(node)"
  @retry="loadChildren"
  @more="appendPage"
/>

// nodes: ancestors carry path: true, the current node current: true;
// a branch not loaded yet has hasChildren: true and state: "loading" | "error" | "empty" while it loads.
```

React (react-ecosphere):

```tsx
const [expanded, setExpanded] = useState(["north", "leeds"]);

<EcoTreeView
  label="Hierarchy of North region"
  nodes={nodes}
  expanded={expanded}
  onExpandedChange={setExpanded}
  onToggle={(node, open) => { if (open) loadChildren(node); }}
  onRetry={loadChildren}
  onMore={appendPage}
/>

// nodes: ancestors carry path: true, the current node current: true;
// a branch not loaded yet has hasChildren: true and state: "loading" | "error" | "empty" while it loads.
```

## Depth limit and a repeated unit

A node that repeats among its own ancestors never gets a toggle, so a cycle cannot loop requests.

## Narrow container

tree--tight for a drawer or side panel on a wide screen.

## Selection widget

Tab into the tree, then use the arrow keys: Right opens or moves in, Left closes or moves to the parent, Enter or Space selects.

Vue (vue-ecosphere):

```vue
<p class="caption" id="pickLabel">Move 12 vehicles to</p>
<EcoTreeView
  selectable
  labelledby="pickLabel"
  :nodes="depots"
  v-model:expanded="open"
  v-model:selected="depot"
/>
```

React (react-ecosphere):

```tsx
const [open, setOpen] = useState(["p-leeds"]);
const [depot, setDepot] = useState<string | string[] | null>("p-kirk");

<p className="caption" id="pickLabel">Move 12 vehicles to</p>
<EcoTreeView
  selectable
  labelledby="pickLabel"
  nodes={depots}
  expanded={open}
  onExpandedChange={setOpen}
  selected={depot}
  onSelectedChange={setDepot}
/>
```

## API

Vue (vue-ecosphere):

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `nodes` | `EcoTreeNode[]` | — | { id, label, href?, meta?, icon?, badges?, current?, path?, note?, children?, hasChildren?, state?, stateText?, more?, selectable? }. |
| `v-model:expanded` | `string[]` | [] | Open branches. Keep it in the URL; open the current node by default. |
| `label / labelledby` | `string` | — | Whose hierarchy this is (“Hierarchy of North region”). |
| `tight` | `boolean` | false | Narrow-container spacing (tree--tight) for drawers and side panels. |
| `maxDepth` | `number` | 8 | Past this depth, nodes with children show a note instead of a toggle. |
| `selectable` | `boolean` | false | Selection widget on the ARIA tree pattern: role="tree", roving tabindex, arrows, Home/End, type-ahead, Enter/Space. |
| `v-model:selected` | `string \| string[] \| null` | null | Selected node id (an array with multiple). |
| `multiple / selectParents` | `boolean` | false | Several selections; let nodes with children be selected too. |
| `@toggle` | `(node, open) => void` | — | A branch opened or closed — load it the first time it opens. |
| `@retry / @more / @select` | `(node) => void` | — | Retry a failed branch, append the next page, an item was selected. |

React (react-ecosphere):

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `nodes` | `EcoTreeNode[]` | — | { id, label, href?, meta?, icon?, badges?, current?, path?, note?, children?, hasChildren?, state?, stateText?, more?, selectable? }. |
| `expanded / onExpandedChange` | `string[]` | [] | Open branches. Keep it in the URL; open the current node by default. |
| `label / labelledby` | `string` | — | Whose hierarchy this is (“Hierarchy of North region”). |
| `tight` | `boolean` | false | Narrow-container spacing (tree--tight) for drawers and side panels. |
| `maxDepth` | `number` | 8 | Past this depth, nodes with children show a note instead of a toggle. |
| `selectable` | `boolean` | false | Selection widget on the ARIA tree pattern: role="tree", roving tabindex, arrows, Home/End, type-ahead, Enter/Space. |
| `selected / onSelectedChange` | `string \| string[] \| null` | null | Selected node id (an array with multiple). |
| `multiple / selectParents` | `boolean` | false | Several selections; let nodes with children be selected too. |
| `onToggle` | `(node, open) => void` | — | A branch opened or closed — load it the first time it opens. |
| `onRetry / onMore / onSelect` | `(node) => void` | — | Retry a failed branch, append the next page, an item was selected. |
