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. maxSelectedwhen 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
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.
| Prop | Type | What it does |
|---|---|---|
childrenrequired | ReactNode | The checkboxes of the group: each `Checkbox` carries its `value`. |
labelrequired | string | The name of the group for the screen reader: it is shown above the checkboxes. |
className | string | Merged with the library classes; it does not replace them. |
defaultValue | readonly string[] | The keys ticked at the start, governing itself from inside. |
disabled | boolean | — |
error | string | — |
hint | string | — |
invalid | boolean | — |
maxSelected | number | How many can be ticked at once; on reaching the cap, the others are turned off until one is unticked. |
name | string | The name under which the ticked checkboxes travel in a form. |
onChange | (value: readonly string[]) => void | Called 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. |
readOnly | boolean | It is read and focused, but does not change. |
required | boolean | Draws 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. |
value | readonly 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:
