Checkbox group

Slant / Forms

A named set of checkboxes that holds its checked values as one array.

Demo

Usage

@tuttn/slant
import { Checkbox, CheckboxGroup, Field } from '@tuttn/slant'

const channelOptions = [
  { value: 'email', label: 'Email', description: 'A digest every morning' },
  { value: 'push', label: 'Push' },
  { value: 'sms', label: 'SMS', description: 'On the Team plan', disabled: true },
]

// Controlled: the value is the list of checked values, in the boxes' order.
const [channels, setChannels] = useState<string[]>(['email'])
<CheckboxGroup label="Notify me by" options={channelOptions} value={channels} onValueChange={setChannels} />

// Uncontrolled and composed: the boxes take their state, name, variant, size
// and disabled state from the group, and a form gets FormData.getAll('labels').
<CheckboxGroup label="Labels" name="labels" defaultValue={['web']} orientation="horizontal">
  <Checkbox value="design">Design</Checkbox>
  <Checkbox value="web">Web</Checkbox>
</CheckboxGroup>

// Cards in a row share it in equal columns.
<CheckboxGroup label="Backups" variant="card" orientation="horizontal" options={backups} defaultValue={['daily']} />

// In a Field, the field's label names the group, and its description and error describe it.
<Field invalid={channels.length === 0} required>
  <Field.Label>Notify me by</Field.Label>
  <CheckboxGroup options={channelOptions} value={channels} onValueChange={setChannels} name="channels" />
  <Field.Error>Choose a channel.</Field.Error>
</Field>

Props

PropTypeDefaultDescription
valuestring[]—The checked values. Pass it to control the group; leave it out to let the group keep its own.
defaultValuestring[][]The values an uncontrolled group starts with.
onValueChange(value: string[]) => void—Called with the new list when a box is toggled. It keeps the boxes' order on the page (the options' order), not the order they were clicked in.
optionsreadonly CheckboxOption[]—The boxes, for uniform content. Leave it out and pass `<Checkbox value="…">` children instead.
labelstring—The group's accessible name. Inside a Field, the field's label names it instead.
namestring—The name every box submits its value under, so `FormData.getAll(name)` lists the checked ones.
formstring—The id of the form the boxes belong to, when the group sits outside it.
variant'default' | 'card''default'The boxes' look: see Checkbox's `variant`.
orientation'vertical' | 'horizontal''vertical'A column of boxes, or a row that wraps (cards share the row in equal columns).
size'sm' | 'md''md'The boxes' size: see Checkbox's `size`.
disabledboolean—Disables every box.
requiredboolean—Marks the group required (`data-required`, and the Field label's mark). No single box becomes required: checking at least one is your validation.
invalidboolean—Marks the group (`data-invalid`) and its boxes invalid. Inside a Field, the field's `invalid` does this.
CheckboxOption{ value, label, description?, disabled? }—One entry of `options`: the value it adds to the list and submits, its label, a hint under it, and whether it is disabled.
Checkbox (child)CheckboxProps with value—The composed form. A Checkbox with a `value` is checked while the group's value holds it, takes the group's name, form, variant and size unless it sets its own, and is disabled while the group is. Its own onCheckedChange still fires.
…restHTMLAttributes<HTMLDivElement>—className, style, other attributes and the ref reach the group's root, which has role="group".

Keyboard

Tab / Shift+Tabmoves from box to box: every enabled box is a stop of its own, and disabled ones are skipped
Spacetoggles the focused box and reports the new list

Accessibility

  • The root is a role="group". Outside a Field, label names it through aria-label, which isn't shown on screen. Inside one, the field's label names it through aria-labelledby, and the field's description and error describe it.
  • Each box is a native checkbox, named by its own label and described by its own description. The field's id and descriptions go on the group, and the boxes keep their own.
  • There are no arrow keys, unlike a radio group: as with any native checkboxes, every box is a Tab stop.
  • invalid marks the group with data-invalid and every box with aria-invalid. required marks only the group (data-required, and the mark on a Field's label), since a required native checkbox would make every box required. Checking at least one is your validation.
  • A disabled box can't be toggled, and isn't submitted even while its value is in the list.

A horizontal group of plain boxes is a row that wraps. A horizontal group of cards shares its row in equal columns that wrap below --sl-checkbox-card-min (10rem).