Checkbox Group

Several checkboxes whose ticked values form one list.

Source
$npx @dinachi/cli@latest add checkbox-group
Select frameworks you know

Selected: react

parent
disabled

Usage

import { Checkbox } from "@/components/ui/checkbox"
import { CheckboxGroup } from "@/components/ui/checkbox-group"

const toppings = [
  { value: "cheese", label: "Cheese" },
  { value: "olives", label: "Olives" },
  { value: "peppers", label: "Peppers" },
]

export function ToppingsPicker() {
  return (
    <div>
      <p id="toppings-label" className="text-sm font-medium">Toppings</p>
      <CheckboxGroup aria-labelledby="toppings-label" defaultValue={["cheese"]}>
        {toppings.map((t) => (
          <div key={t.value} className="flex items-center gap-2">
            <Checkbox id={t.value} value={t.value} />
            <label htmlFor={t.value} className="text-sm">{t.label}</label>
          </div>
        ))}
      </CheckboxGroup>
    </div>
  )
}

Examples

Selected: react

The ticked values live in your state.
import { CheckboxGroup } from '@/components/ui/checkbox-group';
import { Checkbox } from '@/components/ui/checkbox';
import { useState } from 'react';

export function Example() {
  const [selected, setSelected] = useState<string[]>(["react"]);

  return (
    <div className="space-y-3">
      <label className="text-sm font-medium text-foreground">
        Select frameworks you know
      </label>
      <CheckboxGroup value={selected} onValueChange={setSelected}>
        <div className="flex items-center space-x-2">
          <Checkbox id="react" name="frameworks" value="react" />
          <label htmlFor="react" className="text-sm text-foreground">React</label>
        </div>
        <div className="flex items-center space-x-2">
          <Checkbox id="vue" name="frameworks" value="vue" />
          <label htmlFor="vue" className="text-sm text-foreground">Vue</label>
        </div>
        <div className="flex items-center space-x-2">
          <Checkbox id="angular" name="frameworks" value="angular" />
          <label htmlFor="angular" className="text-sm text-foreground">Angular</label>
        </div>
      </CheckboxGroup>
      <p className="text-sm text-muted-foreground">
        Selected: {selected.length > 0 ? selected.join(", ") : "None"}
      </p>
    </div>
  );
}

Notes

Checkbox values

A Checkbox joins the group through its value. Without one it is not in the group's list or the agent's items.

Parent checkbox

Pass allValues and add a <Checkbox parent />. It ticks or clears every child and shows mixed when some are ticked. Disabled children are left alone.

API

valuestring[]
Controlled list of ticked values.
defaultValuestring[] = []
Starting list when uncontrolled.
onValueChange
agent
(value, details) => void
Runs for people and agents. details.cancel() refuses the change.
allValues
agent
string[]
Every checkbox's value. Required for a parent checkbox.
disabled
agent
boolean = false
Blocks every checkbox, for people and agents.
classNamestring
Merged over the vertical stack, for example to lay out a row.

Agent

Inside an AgentProvider, it appears in agent.snapshot() as checkbox-group.

Reads

label

The group's aria-label or aria-labelledby. Unnamed groups go by checkbox-group.

value

The labels of the ticked checkboxes.

items

Every checkbox's label, in order.

multiple

Always true.

disabled

True when the whole group is blocked.

Actions

setValue(["React", "Vue"])

Ticks exactly these, by label then by value. A miss lists the options. Disabled checkboxes keep their state.

Each checkbox is also listed and takes toggle and setChecked, which run through the group's onValueChange like setValue. isAgentChange(details) is true there; details.cancel() refuses and tells the agent. Naming a disabled checkbox fails; a disabled group refuses on its own.

await agent.act({
  steps: [{ ref: "checkbox-group", action: "setValue", args: [["React", "Vue"]] }],
})

Accessibility

  • Renders role="group". Name it with aria-labelledby or aria-label; the agent reads the same name.
  • Each checkbox needs its own label, also its name in the agent's items.
  • A parent checkbox reports mixed while some children are ticked.