Checkbox
Allow users to select one or more options from a number of choices.
import { Checkbox } from "@vitality-ds/components";
Building a form? Checkout out the FormField component
The checkbox is a checkable input for users to communicate if an option is true, false, or indeterminate. A checkbox can appear standalone by itself or in a set of other options. When used in a group of other checkboxes, more than one checkbox can be selected at once.
Checkboxes are used for multiple choices, not for mutually exclusive choices. Each checkbox works independently from other checkboxes in the list, therefore checking an additional box does not affect any other selections.
() => { const [checked, setChecked] = React.useState(false); const [disabled, setDisabled] = React.useState(false); const [invalid, setInvalid] = React.useState(false); return ( <DocsBox css={{ backgroundColor: "$neutral1", padding: "$xl", borderRadius: "$default", maxWidth: 380, boxShadow: "$sm", }} > <Stack spacing="xl" align="stretch"> <header> <Typography variant="sectionTitle">Checkbox Example</Typography> <Typography color="lowContrast"> The below example provides simple state management to control the Checkbox.{" "} </Typography> </header> <Stack direction="horizontal"> <Checkbox label="Do not SMS?" value="do_not_sms" id="do_not_sms" name="contact-options" onChange={() => setChecked(!checked)} checked={checked} disabled={disabled} /> </Stack> <Stack direction="horizontal"> <Button type="button" size="compact" onClick={() => setChecked((prevIsChecked) => prevIsChecked === "indeterminate" ? false : "indeterminate" ) } > Toggle indeterminate </Button> <Button type="button" size="compact" onClick={() => setDisabled(!disabled)} > Toggle disabled </Button> </Stack> </Stack> </DocsBox> ); };
Often you'll want to display a number of options to choose from. You can choose to either utilise a FormField
component which includes labels and error messages, or use a standalone CheckboxList
as shown below:
Most cases should be using FormField, as all form elements should also contain a
label
and any supporting helper/validation messages.
Often a series of checkboxes will refer to the same question, prompt or property that the user is being asked about. As an example, a list of checkboxes may represent a patient's food allergies:
<CheckboxList value={[""]} name="foodAllergies" options={[ { value: "nuts", label: "Nuts", id: "nuts" }, { value: "shellfish", label: "Shellfish", id: "shellfish" }, ]} />
In this example each checkbox will be given the same name (foodAllergies
). If both checkboxes are checked and then the form is submitted, you'll get a string of name/value pairs submitted like this: `foodAllergies=apples&foodAllergies=bananas``. When this string reaches the server, it can be parsed.
If each Checkbox is completely unrelated, you can just deal with them all separately by passing the name
prop for each option (see below). This would result in the following string being submitted to the server:
nuts=nuts&shellfish=shellfish
<CheckboxList value={[""]} name="allergies" options={[ { value: "mold", label: "Mold", name: "mold", id: "mold" }, { value: "pollen", label: "Pollen", name: "pollen", id: "pollen" }, ]} />
This component takes all valid <input type="checkbox">
attributes. Read more on MDN
Description
The controlled checked state of the checkbox. Must be used in conjunction with onCheckedChange.
Type
boolean | "indeterminate"
Description
When true, prevents the user from interacting with the checkbox.
Type
boolean
Description
The text to be displayed as the Checkbox's label
Type
string
Description
The name of the checkbox. Submitted with its owning form as part of a name/value pair.
Type
string
Description
Event handler called when the checked state of the checkbox changes.
Type
FormEventHandler<HTMLButtonElement> & ((checked: CheckedState) => void)
Description
When true, indicates that the user must check the checkbox before the owning form can be submitted.
Type
boolean
Description
A string representing the value of the checkbox. This is not displayed on the client-side, but on the server this is the value given to the data submitted with the checkbox's name.
Type
(string | number | readonly string[]) & string
Default Value
'on'
Description
Id of the element (or elements) that labels the Checkbox list. When used within a FormField, this property is set automatically.
Type
string
Type
"horizontal"
Description
Provide a name to apply a unified name to checkbox items.
Type
string
Description
The callback function to handle change events, i.e. the event triggered when a user clicks on a checkbox item to change the value of stored checked items.
Type
(CheckedItems: any) => string[]
Default Value
() => []
Description
An array of objects that determine the individual checkbox items. See Checkbox Props for a full list of valid properties.
Type
CheckboxPropsInList[]
Description
Array that stores the string values of any checked item's individual values.
Type
string[]