# TreeView

> A keyboard-navigable tree with expand and select.

Controlled desktop tree with roving keyboard focus and explicit expansion/selection state.

- Category: [Data display](https://tarve.dev/docs.md#data)
- Import: `import { TreeView } from "@tarve/core";`
- Props type: `TreeViewProps<T>` (generic)
- Source: [packages/core/src/desktop-controls.ts:60](https://github.com/omarcosr/tarve/blob/main/packages/core/src/desktop-controls.ts#L60)

## Usage

```tsx title="TreeView.tsx"
import { TreeView } from "@tarve/core";

let expanded = ["src"];
let selected = "app";

<TreeView
  style={{ width: 280 }}
  expandedIds={expanded}
  selectedId={selected}
  nodes={[
    {
      id: "src",
      label: "src",
      icon: "folder",
      children: [
        { id: "app", label: "app.tsx" },
        { id: "theme", label: "theme.ts" },
      ],
    },
    { id: "readme", label: "README.md" },
  ]}
  onExpandedChange={(ids) => {
    expanded = ids;
  }}
  onSelectedChange={(id) => {
    selected = id;
  }}
/>
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `nodes` (required) | `readonly TreeNode<T>[]` | — |  |
| `expandedIds` | `readonly string[]` | `[]` |  |
| `indent` | `number` | `18` |  |
| `onActivate` | `(id: string, node: TreeNode<T>) => void` | — |  |
| `onExpandedChange` | `(ids: string[]) => void` | — |  |
| `onSelectedChange` | `(id: string, node: TreeNode<T>) => void` | — |  |
| `rowHeight` | `number` | `32` |  |
| `selectedId` | `string` | — |  |

## Common props

Shared by every component (descriptions: https://tarve.dev/docs.md#common-props): `children`, `class`, `className`, `highlight`, `id`, `key`, `motionFrom`, `onHighlight`, `onPaste`, `onPointerLeave`, `onPointerMove`, `onSize`, `onTransitionEnd`, `rovingGroup`, `style`.

## Types

```ts title="TreeNode"
interface TreeNode<T = unknown> {
  id: string;
  label: string;
  children?: readonly TreeNode<T>[];
  disabled?: boolean;
  icon?: IconName;
  data?: T;
}
```

Previous: [DataGrid](https://tarve.dev/docs/components/DataGrid.md) · Next: [Chart](https://tarve.dev/docs/components/Chart.md)

---

Tarve v0.13.8 · HTML version: https://tarve.dev/docs/components/TreeView · Index for agents: https://tarve.dev/llms.txt
