# Tree View checkboxes

> Tri-state checkboxes with cascade or independent propagation, disabled-item rules, check on click, reactive checked ids, cancellable events, check-all commands and form submission with reset.

Canonical: https://salmonui.dev/docs/navigation/tree-view-checkboxes

`checkable` shows a checkbox on every row; rows carry `aria-checked`. Space toggles the focused item, a click
on the checkbox toggles it, and with `checkOnClick` a click anywhere on the
row does.

### Live demo: Tree View checkboxes

Live demo: https://salmonui.dev/demos/navigation/tree-view-checkboxes

Tri-state permissions with cascade or independent checks, a disabled item the cascade skips, check on click, form submission with reset, and the checkAll, uncheckAll and getCheckedIds methods.

`Demo.tsx`:

```tsx
'use client';
import { useRef, useState } from 'react';
import { Button } from '@salmonui/buttons/button';
import { Checkbox } from '@salmonui/inputs/checkbox';
import { TreeView, type TreeViewApi } from '@salmonui/navigation/tree-view';

interface Perm {
  id: string;
  name: string;
  children?: Perm[];
  locked?: boolean;
}

const permissions: Perm[] = [
  {
    id: 'orders',
    name: 'Orders',
    children: [
      { id: 'orders.read', name: 'View orders' },
      { id: 'orders.edit', name: 'Edit orders' },
      { id: 'orders.refund', name: 'Issue refunds', locked: true },
    ],
  },
  {
    id: 'catalog',
    name: 'Catalog',
    children: [
      { id: 'catalog.read', name: 'View products' },
      { id: 'catalog.publish', name: 'Publish products' },
    ],
  },
  { id: 'reports', name: 'Reports' },
];

// `expanded` is reactive (applied when the array identity changes): keep one array.
const EXPANDED = ['orders', 'catalog'];

export default function TreeViewCheckboxesDemo() {
  const [cascade, setCascade] = useState(true);
  const [submitted, setSubmitted] = useState<string[]>([]);
  const [checkedIds, setCheckedIds] = useState('not read yet');
  const api = useRef<TreeViewApi<Perm>>(null);
  return (
    <form
      style={{ display: 'grid', gap: 12, maxWidth: 420 }}
      onSubmit={(event) => {
        event.preventDefault();
        setSubmitted(new FormData(event.currentTarget).getAll('permissions').map(String));
      }}
    >
      <Checkbox checked={cascade} onCheckedChange={(checked) => setCascade(checked)}>
        Cascade
      </Checkbox>
      <TreeView
        ref={api}
        aria-label="Permissions"
        data={permissions}
        getItemId={(p) => p.id}
        getItemLabel={(p) => p.name}
        getChildren={(p) => p.children}
        isItemDisabled={(p) => p.locked === true}
        checkable
        checkPropagation={cascade ? 'cascade' : 'none'}
        cascadeToDisabled={false}
        checkOnClick
        selection="none"
        name="permissions"
        defaultChecked={['orders.read']}
        expanded={EXPANDED}
        height={260}
      />
      <div className="demo-toolbar" style={{ marginBlockEnd: 0 }}>
        <Button type="submit" tone="primary">
          Save
        </Button>
        <Button type="reset" variant="outline">
          Reset
        </Button>
        <Button variant="outline" onClick={() => api.current?.checkAll()}>
          Check all
        </Button>
        <Button variant="outline" onClick={() => api.current?.uncheckAll()}>
          Uncheck all
        </Button>
        <Button variant="outline" onClick={() => api.current?.checkAll(['catalog'])}>
          Check Catalog
        </Button>
        <Button variant="outline" onClick={() => setCheckedIds(api.current?.getCheckedIds().join(', ') || 'none')}>
          Read checked ids
        </Button>
      </div>
      <p style={{ margin: 0 }}>
        api.getCheckedIds(): <output>{checkedIds}</output>
        <br />
        Submitted: <output>{submitted.join(', ') || 'nothing yet'}</output>
      </p>
    </form>
  );
}
```

## Propagation

`checkPropagation`:

- `'cascade'` (default): checking a parent checks its subtree; a parent is
  checked when all its leaves are, `aria-checked="mixed"` when some are.
- `'none'`: every item is independent.

The cascade does not visit each descendant: the tree keeps checked-leaf
counts per parent and marks the subtree lazily, so checking a parent of
100,000 descendants costs O(depth) and stays within 16 ms.

`cascadeToDisabled` (default `true`): when `false`, a cascade leaves disabled descendants as they are and
they do not count toward their parent's state. Disabled items cannot be
toggled by the user either way.

Children loaded later under a checked parent come checked.

## State and events

`checked` is a reactive id list; `defaultChecked` sets the initial ids when
`checked` is not given. `onBeforeCheckedChange({ id, item, checked,
source })` runs before a user toggle and `event.preventDefault()` cancels it.
`onCheckedChange({ checked, id, item, source })` reports every checked id
afterwards: fully checked parents included, mixed ones not. `event.checked`
is computed on first read, so a handler that ignores it costs nothing.

```tsx
const [checked, setChecked] = useState<readonly TreeViewKey[]>([]);
<TreeView checkable checked={checked} onCheckedChange={(e) => setChecked(e.checked)} /* ... */ />
```

The API: `api.getCheckedIds()`, `api.checkAll(ids?)` and
`api.uncheckAll(ids?)` (every item when `ids` is omitted); both report
`onCheckedChange` with `id: null` and `source: 'api'`.

## Forms

With `name`, the tree renders one `<input type="hidden" name value>` per
checked id, so the cost follows the selection, not the tree. `form` links
the inputs to a form by id. The owning form's `reset` restores
`defaultChecked` (or the `checked` ids of the first render).

```tsx
<form onSubmit={(e) => console.log(new FormData(e.currentTarget).getAll('folders'))}>
  <TreeView checkable name="folders" defaultChecked={['docs']} /* ... */ />
  <button>Save</button> <button type="reset">Reset</button>
</form>
```

## Server rendering

Checked state is derived from `checked` / `defaultChecked` during render,
so checkboxes, `aria-checked` and the hidden form inputs are in the server
markup and hydrate without warnings.

## API reference

`checkable`, `checkPropagation`, `cascadeToDisabled`, `checkOnClick`,
`checked`, `defaultChecked`, `onBeforeCheckedChange`, `onCheckedChange`,
`name`, `form` and the `TreeViewApi` check methods are in the
[`@salmonui/navigation/tree-view` reference](https://salmonui.dev/docs/reference/navigation/tree-view);
imports and styles are on the [Tree View](https://salmonui.dev/docs/navigation/tree-view) page.
