Several checkboxes under one label, to pick any number of options.
$ pnpm dlx shadcn@latest add @dashral/checkbox-groupInstallation
$ pnpm dlx shadcn@latest add @dashral/checkbox-groupAdds the component, the components it builds on, its npm packages and the theme tokens it uses. Or by its full URL: https://dashral.com/r/checkbox-group.json
Usage
import { CheckboxGroup } from "@/components/dashral/checkbox-group"
<CheckboxGroup
label="Notify me by"
options={["Email", "Push", "SMS"]}
value={channels}
onValueChange={setChannels}
/>Using the npm package? Import from @dashral/ui/components/checkbox-group instead, here and in the examples; the rest is the same.
Examples
API reference
CheckboxGroup
| Prop | Type | Default |
|---|---|---|
label | ReactNodeShown above, linked to the control. | — |
description | ReactNodeA hint under the control. | — |
error | boolean | ReactNodetrue shows the error style; a message also shows under the control. | — |
labelAction | ReactNodeAt the end of the label's row, e.g. a “Forgot password?” link or the current value. | — |
withAsterisk | booleanMarks the label with an asterisk, e.g. alongside required. | false |
options | (string | { value, label?, description?, icon?, disabled? })[]The checkboxes. Required. | — |
value / defaultValue | string[]The ticked values, controlled or initial; always in the options' order. | [] |
onValueChange | (value: string[]) => voidCalled with the new value. | — |
variant | "default" | "cards"Checkboxes with labels, or bordered cards you click anywhere on. | "default" |
selectAll | boolean | ReactNodeA checkbox above the rest that ticks or clears them all, half-ticked while some are. true for "Select all". | — |
max | numberAt most this many; the rest are disabled once it's reached. | — |
color | "primary" | "neutral" | "info" | "success" | "warning" | "destructive" | "highlight"A ticked checkbox (and card). | "primary" |
name | stringThe form field's name; each ticked value is sent under it. | — |
disabled / required | booleanAs on other controls. | — |
classNames | { root, label, description, error, group, item, checkbox }Classes for each part. | — |
className | stringThe group's classes, e.g. columns: "sm:grid-cols-2". | — |
Keyboard
| Keys | Action |
|---|---|
| Tab | Move between checkboxes |
| Space | Tick or clear the focused one |
Accessibility
- With label, description or error, the id, aria-describedby and aria-invalid are wired up for you.
- role="group" named by the label, so each checkbox is read with it.
- "Select all" is a real checkbox with the mixed state while some are ticked.