# `LedgerUI.Components.Treeview`
[🔗](https://github.com/devaction-labs/ledger_ui/blob/v0.2.0/lib/ledger_ui/components/treeview.ex#L1)

A tree view: a hierarchical list whose branches expand and collapse.

    <.treeview id="notas" on_toggle="toggled" on_select="opened">
      <.tree_item id="2026" label="Notas 2026" expanded>
        <:icon><.icon name="folder" size="md" /></:icon>
        <.tree_item id="out" label="Outubro" branch />
        <.tree_item id="nfe" label="NF-e 48.211.xml" selected />
      </.tree_item>
    </.treeview>

An item with children is a branch, one without is a leaf. `branch` draws a closed branch
before its children are there (loaded when it opens). `checked` (`true`, `false` or `"mixed"`)
puts a checkbox before the label; `chevrons={false}` leaves the chevrons out.

Branches open and close in the browser (`LedgerUI.Treeview` hook), with no round trip; a
branch the person toggled stays that way across patches, and the server wins when it changes
`expanded` for it. `on_toggle` is pushed with `%{"id" => id, "expanded" => boolean}`,
`on_select` with `%{"id" => id}` when a leaf is clicked (or gets Enter), `on_check` with
`%{"id" => id, "checked" => boolean}`; all to `target` when given. A `%JS{}` is executed instead.

The ARIA is rendered by the server: the root is `role="tree"`, each row a `role="treeitem"` with
`aria-selected`, `aria-expanded` (branches), `aria-checked` (when `checked` is given), `aria-owns`
pointing at its `role="group"` of children and a roving `tabindex` (`0` on the selected row, `-1` on
the others; the hook makes sure one row, the first when none is selected, is in the tab order).
The hook only updates `aria-expanded`/`aria-selected`/`tabindex` as the person interacts.

The canvas look is flat rows: `div.lu-tree-i`, indented with an inline `padding-left`. Nested
`tree_item`s are wrapped in a `div.lu-tree-kids` that indents them, so no `depth` is needed;
rows written side by side (as the design does) take `depth`.

## Streams

    <.treeview id="files" rows={@streams.files}>
      <:row :let={{_dom_id, file}}>
        <.tree_item id={"file-#{file.id}"} label={file.name} />
      </:row>
    </.treeview>

# `tree`

A tree written as data: the same as `treeview/1` with one `tree_item/1` per map.

    <.tree id="folders" items={[
      %{id: "fiscal", label: "Fiscal", icon: "folder", count: 12, expanded: true,
        children: [%{id: "nfe", label: "NF-e", icon: "file"}]}
    ]} selected="nfe" />

## Attributes

* `id` (`:string`) - Defaults to `"tree"`.
* `items` (`:list`) (required) - Maps with `:label` and optionally `:id`, `:icon`, `:count`, `:checked`, `:expanded` and `:children` (the same shape, nested).
* `selected` (`:any`) - The `id` (or the `label`, without ids) of the selected item. Defaults to `nil`.
* `expanded` (`:list`) - Ids of the branches that are open. Defaults to `[]`.
* `on_toggle` (`:any`) - Defaults to `nil`.
* `on_select` (`:any`) - Defaults to `nil`.
* `on_check` (`:any`) - Defaults to `nil`.
* `variant` (`:string`) - Defaults to `"canvas"`. Must be one of `"canvas"`, or `"lineone"`.
* `chevrons` (`:boolean`) - Defaults to `true`.
* `target` (`:any`) - Defaults to `nil`.
* `class` (`:any`) - Defaults to `nil`.
* Global attributes are accepted.

# `tree_item`

## Attributes

* `id` (`:string`) - Names the item in events and in `expanded`. Defaults to `nil`.
* `label` (`:string`) - Defaults to `nil`.
* `expanded` (`:boolean`) - Defaults to `false`.
* `selected` (`:boolean`) - Defaults to `false`.
* `branch` (`:boolean`) - A branch even with no children yet. Defaults to `false`.
* `checked` (`:any`) - Defaults to `nil`.Must be one of `nil`, `true`, `false`, or `"mixed"`.
* `tag` (`:string`) - Wraps the item in this tag. Defaults to `nil`. Must be one of `nil`, `"li"`, or `"div"`.
* `count` (`:string`) - A trailing count (the mono figure at the right). Defaults to `nil`.
* `depth` (`:integer`) - Indent level for rows written side by side. Defaults to `0`.
* `chevron` (`:boolean`) - Leaves room for / draws the chevron. Defaults to `true`.
* `chevron_class` (`:any`) - Defaults to `nil`.
* `class` (`:any`) - Defaults to `nil`.
* Global attributes are accepted. `icon="name"` draws that icon before the label (the `:icon` slot takes any markup). Supports all globals plus: `["icon"]`.
## Slots

* `icon` - An icon before the label.
* `inner_block` - The children; makes the item a branch.

# `treeview`

## Attributes

* `id` (`:string`) (required)
* `expanded` (`:list`) - Ids of the branches that are open. Defaults to `[]`.
* `on_toggle` (`:any`) - Defaults to `nil`.
* `on_select` (`:any`) - Defaults to `nil`.
* `on_check` (`:any`) - Defaults to `nil`.
* `variant` (`:string`) - Defaults to `"canvas"`. Must be one of `"canvas"`, or `"lineone"`.
* `chevrons` (`:boolean`) - Defaults to `true`.
* `target` (`:any`) - Defaults to `nil`.
* `rows` (`:any`) - A list or a stream rendered by `:row`. Defaults to `nil`.
* `row_id` (`:any`) - A function giving each row's DOM id (lists only). Defaults to `nil`.
* `class` (`:any`) - Defaults to `nil`.
* Global attributes are accepted.
## Slots

* `inner_block`
* `row` - Renders one row; takes `{dom_id, item}`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
