#Overview
Checkboxes are used with Forms (Code).
#Single Checkbox
Use the <SingleCheckboxField /> to answer a simple “yes” or “no” question inside a form.
Show codeHide code
export const SingleCheckboxFieldDemo = () => {
const form = useForm<{ isChecked: boolean }>();
return (
<form onSubmit={form.handleSubmit(() => {})}>
<SingleCheckboxField
form={form}
name="isChecked"
options={{
required: { value: true, message: "Please check the above box" },
}}
>
Check this Box
</SingleCheckboxField>
<Button type="submit">Submit</Button>
</form>
);
};
| Name | Type | Default | Description |
|---|---|---|---|
| children * | ReactNode | - | The label of the checkbox |
| form * | UseFormReturn<TFieldValues> | - | The connected form that the form field is part of |
| name * | TFieldName | - | The name of the input being registered from the form |
| defaultChecked | boolean | - | If true, the checkbox is checked initially, for uncontrolled mode, mutually exclusive with |
| disabled | boolean |
| If true, the checkbox is disabled |
| helpText | ReactNode | - | A help text that can be displayed below the checkbox |
| hideHelpAndValidation | boolean | - | Hides the help text and validation message |
| noWrap | boolean | - | |
| onChange | ChangeEventHandler<HTMLInputElement> | - | The change event handler |
| options | Pick<RegisterOptions<TFieldValues, TFieldName>, "required" | "validate"> |
| The options that can be set for the registration of the field |
#Multiple Checkboxes
Use a Selector for more complex checkbox content inside a form.
If that is not possible (e.g. for competitions in editorial content)
use several <CheckboxField /> components together.
Multiple Checkboxes have to be grouped with a <Fieldset /> and labelled with a <FieldsetLegend /> to make them accessible.
The <FieldHelpText /> displays a validation message and help text related to the Checkboxes.
Show codeHide code
export const CheckboxFieldsDemo = () => {
const form = useForm<{ fruit: string[] }>();
const validate = () => {
const values = form.getValues("fruit");
const amount = Array.isArray(values) ? values.length : values ? 1 : 0;
if (amount < 1) {
return "Select at least one fruit";
}
if (amount > 3) {
return "Not more than 3 fruits";
}
return true;
};
return (
<form onSubmit={form.handleSubmit(() => {})}>
<Fieldset>
<FieldsetLegend>Which fruit are your favourite?</FieldsetLegend>
{fruit.map(({ label, value }) => (
<CheckboxField
key={value}
form={form}
name="fruit"
value={value}
options={{ validate, required: false }}
>
{label}
</CheckboxField>
))}
<FieldHelpText form={form} name="fruit">
Select between one and three fruits
</FieldHelpText>
</Fieldset>
<Button type="submit">Submit</Button>
</form>
);
};
| Name | Type | Default | Description |
|---|---|---|---|
| children * | ReactNode | - | The label of the checkbox |
| form * | UseFormReturn<TFieldValues> | - | The connected form that the form field is part of |
| name * | TFieldName | - | The name of the input being registered from the form |
| defaultChecked | boolean | - | If true, the checkbox is checked initially, for uncontrolled mode, mutually exclusive with |
| disabled | boolean |
| If true, the checkbox is disabled |
| onChange | ChangeEventHandler<HTMLInputElement> | - | The change event handler |
| options | Pick<RegisterOptions<TFieldValues, TFieldName>, "required" | "validate"> |
| The options that can be set for the registration of the field |
#Outside of a form
If there is no need to submit a form, consider using a Toggle Switch.
If more granular control is needed <Checkbox /> can be used directly.
| Name | Type | Default | Description | Controls |
|---|---|---|---|---|
| children * | ReactNode | - | The label of the checkbox | |
| checked | boolean | - | If true, the checkbox is checked, for controlled mode, mutually exclusive with | |
| defaultChecked | boolean | - | If true, the checkbox is checked initially, for uncontrolled mode, mutually exclusive with | - |
| disabled | boolean |
| If true, the checkbox is disabled | |
| hasError | boolean |
| If true, the checkbox becomes outlined in red | |
| onChange | ChangeEventHandler<HTMLInputElement> | - | The change event handler | - |
#Colors
- Token
BORDER_FOCUS#0003#fff4#0003#fff4BORDER_FOCUS_ERROR#0003#fff4#0003#fff4BORDER_FOCUS_SELECTED#0003#fff4#0003#fff4CHECKBOX_BORDER#bbb#fff#bbb#fffCHECKBOX_BORDER_DISABLED#0000#fff#bbb#fffCHECKBOX_BORDER_ERROR#e64#f75#e02#f55CHECKBOX_BORDER_FOCUS#bbb#fff#bbb#fffCHECKBOX_BORDER_FOCUS_ERROR#e64#f75#e02#f55CHECKBOX_FOCUS_SELECTED#555#fff#059#fffCHECKBOX_SELECTED#555#fff#059#fffCHECKBOX_SELECTED_DISABLED#555#fff#059#fffCHECKBOX_UNCHECKED_BACKGROUND_DISABLED#fff#0000#0000#0000CHECKBOX_UNCHECKED_BORDER_DISABLED#bbb#0000#0000#0000ICON_SHAPE_FOCUS_SELECTED#fff#111#fff#111ICON_SHAPE_SELECTED#fff#111#fff#111ICON_SHAPE_SELECTED_DISABLED#fff#111#fff#111LABEL_TEXT#000#fff#000#fffLABEL_TEXT_DISABLED#000#fff#000#fffLABEL_TEXT_ERROR#000#fff#000#fffLABEL_TEXT_FOCUS#000#fff#000#fffLABEL_TEXT_FOCUS_ERROR#000#fff#000#fffLABEL_TEXT_FOCUS_SELECTED#000#fff#000#fffLABEL_TEXT_SELECTED#000#fff#000#fffLABEL_TEXT_SELECTED_DISABLED#000#fff#000#fffMARK_FOCUS_SELECTED#fff#111#fff#111MARK_SELECTED#fff#111#fff#111MARK_SELECTED_DISABLED#fff#111#fff#111