Skip to content
EspañolEnglish
All components (283)

Checkbox group

Choice · CheckboxGroup

What it is and when to use it

A group of checkboxes for choosing several options, with its label, its hint and its error, and the value as a list of keys. Each option is a Checkbox with its value inside the CheckboxGroup. It is the sibling of RadioGroup for when several can be ticked.

When to use it

  • Choosing several options that are worth seeing at once: the extras of an order, the permissions of a role, the categories of a notice.
  • In a form that is sent with a button: with name, all the ticked boxes travel with that name.
  • maxSelected when only a few fit: on reaching the cap, the ones left are turned off until one is unticked.

When not to

  • One option among several: RadioGroup. A single loose checkbox (accepting the terms): Checkbox.
  • Many options that are searched by typing: MultiSelect. The facets of a catalog: FilterGroup.
  • Filters with color or count that switch on right away: ToggleChipGroup.

Keyboard: each checkbox is a native <input type="checkbox">, so Tab goes through all of them and Space ticks or unticks the focused one. Disabled ones take no focus.

Accessibility: it is a group named by its label; the hint (hint) and the error (error) are read as its description, and the error marks each checkbox as invalid. required only draws the asterisk: a group does not accept aria-required, so what is mandatory is told with the error message. disabled and readOnly apply to all of them.

Sizes: size belongs to the whole group: the box and the text of each checkbox, and the label, the hint and the error of that field size. A size on a checkbox takes precedence over the group's.

Fine-tuning: the --amset-checkbox-* variables of each checkbox; the label, the hint and the error are those of a field (--amset-field-*).

What it looks like

Order extras

3 €

15 €

Free

You can pick two at most

Extras total

3 € + 15 €

2 of 2: limit reached

18 €

const [extras, setExtras] = useState(["envoltorio", "grabado"]);

<CheckboxGroup
  label="Order extras"
  hint="You can pick two at most"
  orientation="vertical"
  value={extras}
  onChange={setExtras}
  maxSelected={2}
  name="extras"
>
  <Checkbox value="envoltorio" description="Cardboard box and ribbon">Gift wrapping</Checkbox>
  <Checkbox value="grabado" description="Up to 20 letters inside the ring">Engraved</Checkbox>
  <Checkbox value="card" description="Handwritten">Card with a message</Checkbox>
</CheckboxGroup>

Props

Import it with import { CheckboxGroup } from '@amset/ui'. Required props are marked.

PropTypeWhat it does
childrenrequiredReactNodeThe checkboxes of the group: each `Checkbox` carries its `value`.
labelrequiredstringThe name of the group for the screen reader: it is shown above the checkboxes.
classNamestringMerged with the library classes; it does not replace them.
defaultValuereadonly string[]The keys ticked at the start, governing itself from inside.
disabledboolean—
errorstring—
hintstring—
invalidboolean—
maxSelectednumberHow many can be ticked at once; on reaching the cap, the others are turned off until one is unticked.
namestringThe name under which the ticked checkboxes travel in a form.
onChange(value: readonly string[]) => voidCalled with the NEW list of keys, in the order they were ticked.
orientation"horizontal" | "vertical"How the checkboxes are laid out: in a row that wraps, or one under another.
readOnlybooleanIt is read and focused, but does not change.
requiredbooleanDraws the asterisk. What is mandatory is told with `error`, since a group has no `aria-required`.
size"sm" | "md" | "lg"Size of all the checkboxes of the group and of the label, the hint and the error.
valuereadonly string[]The ticked keys, governed from outside.

It also accepts the attributes of the element it renders: className, style, data-*, aria-* and its events.

In the test bench

Every state and variant, to try one at a time, on its page at storybook.amsetui.com: