Fragiola

API: components

CheckboxGroup

Checkboxes sharing one array value, with an optional parent checkbox that checks or clears them all, in each framework.

A group of checkboxes sharing one array value: the values of the checked ones. A parent checkbox (parent) checks every value of allValues, clears them all, and shows the mixed state in between (the APG checkbox pattern, mixed). CheckboxGroup is one component, not a namespace; its checkboxes come from /checkbox.

Pizza toppings
Gallery

Behaviour

  • Children: a checkbox's state is whether the group's value holds its value; toggling it adds or removes it. The value keeps the order of allValues, else the order they were checked.
  • Parent: checked when every value of allValues is, unchecked when none is, mixed otherwise. Toggling it from unchecked or mixed checks all; from checked, clears all. It submits nothing.
  • Forms: each child submits its own name=value; a form reset returns an uncontrolled group to defaultValue.
  • Name: the group is a role="group"; name it with aria-labelledby (a heading) or aria-label, or put it in a Field, whose Label names it.
  • In a Field: the group is the field's control. The Field's Label names it (aria-labelledby), its Description and Errors describe it (aria-describedby), aria-invalid and the field's state attributes are on the group, and validate receives the array of checked values. Its checkboxes take the field's name (each still submits its own value; a parent submits nothing) and disabled, and do not register as the field's control themselves. The browser validates no element for it: a Form blocks a submission with an invalid group, and focuses its first enabled checkbox.

Anatomy

partelementdata-primitives-partrole
CheckboxGroupdivcheckbox-group-rootthe group; holds the array value

Usage

import { Checkbox } from "@fragiola/primitives-react/checkbox";
import { CheckboxGroup } from "@fragiola/primitives-react/checkbox-group";

<CheckboxGroup aria-labelledby="toppings" defaultValue={["cheese"]} allValues={["cheese", "ham"]}>
    <h2 id="toppings">Toppings</h2>
    <label>
        <Checkbox.Root parent />
        All toppings
    </label>
    <label>
        <Checkbox.Root name="toppings" value="cheese" />
        Cheese
    </label>
    <label>
        <Checkbox.Root name="toppings" value="ham" />
        Ham
    </label>
</CheckboxGroup>;

In a Field

<Form>
    <Field.Root name="toppings" validate={(value) => ((value as string[]).length === 0 ? "Choose a topping." : null)}>
        <Field.Label>Toppings</Field.Label>
        <CheckboxGroup>
            <label><Checkbox.Root value="cheese" /> Cheese</label>
            <label><Checkbox.Root value="ham" /> Ham</label>
        </CheckboxGroup>
        <Field.Description>Pick at least one.</Field.Description>
        <Field.Error />
    </Field.Root>
    <button type="submit">Order</button>
</Form>

The checkboxes have no name of their own: the field gives it, and the form submits toppings=ham. The Field's Label is a label with no labelable control here; it names the group through aria-labelledby.

Props

It also takes what the primitive contract gives: render (Vue: as and the render slot), a class and a style, a ref, and any attribute, which it forwards. Vue binds them as v-model:value and @value-change; Angular as [(value)] and (valueChangeDetails).

CheckboxGroup

proptypedefaultdescription
valuestring[]the checked values, controlled
defaultValuestring[][]the initial values, uncontrolled
onValueChange(value: string[], details: { reason }) => voidevery change (reason none)
allValuesstring[][]every child value: what the parent checks
disabledbooleanfalsedisables every child

State and part attributes

partattributes
CheckboxGrouprole="group", data-disabled; in a Field, aria-labelledby (the Field's Label), aria-describedby, aria-invalid and the field's states

The parent checkbox gets aria-controls (its children's ids) and aria-checked="mixed" in between.

Keyboard

Each checkbox is a tab stop; Space toggles the focused one.

Exports

entryexports
@fragiola/primitives-react/checkbox-groupChangeDetails, ChangeReason, CheckboxGroup, CheckboxGroupProps, CheckboxGroupState, RenderProp
@fragiola/primitives-vue/checkbox-groupChangeDetails, ChangeReason, CheckboxGroup, CheckboxGroupEmits, CheckboxGroupProps, CheckboxGroupState, RenderSlotProps
@fragiola/primitives-angular/checkbox-groupChangeDetails, ChangeReason, CheckboxGroup, CheckboxGroupState, ValueChangeEvent