import { Checkbox } from "@senvenli/react/selection";
export default function Example() {
return (
<Checkbox defaultSelected description="One email a month, no more.">
Send me product updates
</Checkbox>
);
}import { Checkbox, CheckboxGroup } from "@senvenli/react/selection";Examples
A group
Several options under one legend.import { Checkbox, CheckboxGroup } from "@senvenli/react/selection";
export default function Example() {
return (
<CheckboxGroup label="Notify me about" description="Sent to ada@example.com." defaultValue={["orders", "invoices"]}>
<Checkbox value="orders">New orders</Checkbox>
<Checkbox value="invoices">Paid invoices</Checkbox>
<Checkbox value="comments">Comments on my projects</Checkbox>
</CheckboxGroup>
);
}Indeterminate parent
A parent box shows a minus while only some of its options are checked.import { Stack } from "@senvenli/react/layout";
import { Checkbox, CheckboxGroup } from "@senvenli/react/selection";
import { useState } from "react";
const COLUMNS = [
{ value: "customer", label: "Customer" },
{ value: "status", label: "Status" },
{ value: "total", label: "Total" },
];
export default function Example() {
const [shown, setShown] = useState(["customer", "total"]);
const all = shown.length === COLUMNS.length;
return (
<Stack space="space-150">
<Checkbox
isSelected={all}
isIndeterminate={shown.length > 0 && !all}
onChange={(isSelected) => setShown(isSelected ? COLUMNS.map((column) => column.value) : [])}
>
Show all columns
</Checkbox>
<CheckboxGroup label="Columns" value={shown} onChange={setShown}>
{COLUMNS.map((column) => (
<Checkbox key={column.value} value={column.value}>
{column.label}
</Checkbox>
))}
</CheckboxGroup>
</Stack>
);
}Invalid and disabled
The error sits under the label; disabled options keep their value.import { Stack } from "@senvenli/react/layout";
import { Checkbox } from "@senvenli/react/selection";
export default function Example() {
return (
<Stack space="space-150">
<Checkbox isInvalid errorMessage="Accept the terms to continue.">
I accept the terms of service
</Checkbox>
<Checkbox isDisabled>Share usage data</Checkbox>
<Checkbox isDisabled defaultSelected description="Required by your workspace.">
Two-step sign-in
</Checkbox>
</Stack>
);
}Sizes
`size` small, medium (default) or large; a CheckboxGroup sets it for its boxes.import { Checkbox } from "@senvenli/react/selection";
import { Stack } from "@senvenli/react/layout";
export default function Example() {
return (
<Stack space="space-150">
<Checkbox size="small" defaultSelected>
Small
</Checkbox>
<Checkbox defaultSelected>Medium</Checkbox>
<Checkbox size="large" defaultSelected>
Large
</Checkbox>
</Stack>
);
}Guidelines
When to use it
- Agreeing to something, or choosing several options from a few.
Use something else when
- The setting applies at once. Switch
Do and avoid
- Write the option positively: "Send notifications", not "Don't send…".
Accessibility
Keyboard
| Keys | What happens |
|---|---|
| Space | Toggles the box. |
Built in
- Required fields show "Fill in this field." after a blur or a submit; your own
errorMessagereplaces it.
You provide
- A label for each box and a legend for the group.
API reference
Checkbox
| Prop | Type | Default | Description |
|---|---|---|---|
size | ComponentSize | – |
|
childrenRequired | React.ReactNode | – | The option, written positively ("Send notifications"). |
isSelected | boolean | – | |
defaultSelected | boolean | – | |
isIndeterminate | boolean | – | A parent of a partially selected list; shows a minus, never a check. |
onChange | (isSelected: boolean) => void | – | |
description | React.ReactNode | – | |
isInvalid | boolean | – | |
errorMessage | React.ReactNode | – | Shown under the label while the checkbox is invalid ( |
isDisabled | boolean | – | |
isRequired | boolean | – | The box must be checked (terms to accept); inside a CheckboxGroup the group's |
requiredValidation | RequiredValidation | – | Who says a required box left unchecked is missing: |
validationBehavior | "aria" | "native" | – |
|
value | string | – | The value inside a CheckboxGroup. |
name | string | – |
sizeComponentSizesmall,medium(default) orlarge; a group's size, then the provider default, otherwise.childrenRequiredReact.ReactNodeThe option, written positively ("Send notifications").
isSelectedbooleandefaultSelectedbooleanisIndeterminatebooleanA parent of a partially selected list; shows a minus, never a check.
onChange(isSelected: boolean) => voiddescriptionReact.ReactNodeisInvalidbooleanerrorMessageReact.ReactNodeShown under the label while the checkbox is invalid (
isInvalidor failed native validation).isDisabledbooleanisRequiredbooleanThe box must be checked (terms to accept); inside a CheckboxGroup the group's
isRequiredapplies instead.requiredValidationRequiredValidationWho says a required box left unchecked is missing:
field(default) shows "Fill in this field." on blur and on a DS Form submit, which it then blocks;apponly marks it. See {@link RequiredValidation}. The app'sisInvalid(truewith itserrorMessage, orfalse) overrides the check, as on TextField. Disabled, empty and required, it still blocks a DS Form's submit and cannot take focus (as TextField).validationBehavior"aria" | "native"ariaornative; unset, it follows the surrounding form, else React Aria's default (native).valuestringThe value inside a CheckboxGroup.
namestring
CheckboxGroup
| Prop | Type | Default | Description |
|---|---|---|---|
size | ComponentSize | – | The size of every Checkbox in the group. |
labelRequired | React.ReactNode | – | The visible legend, required. |
description | React.ReactNode | – | |
errorMessage | React.ReactNode | – | |
orientation | "horizontal" | "vertical" | "vertical" | |
childrenRequired | React.ReactNode | – | |
requiredValidation | RequiredValidation | – | Who says a required group with nothing checked is missing: |
sizeComponentSizeThe size of every Checkbox in the group.
labelRequiredReact.ReactNodeThe visible legend, required.
descriptionReact.ReactNodeerrorMessageReact.ReactNodeorientation"horizontal" | "vertical"Default"vertical"childrenRequiredReact.ReactNoderequiredValidationRequiredValidationWho says a required group with nothing checked is missing:
field(default) shows "Fill in this field." when focus leaves the group and on a DS Form submit, which it then blocks;apponly marks the group. See {@link RequiredValidation}. The app'sisInvalid(truewith itserrorMessage, orfalse) overrides the check, as on TextField. Disabled, empty and required, it still blocks a DS Form's submit and cannot take focus (as TextField).