Checkbox API
Import
Section titled “Import”Cimpress UI exports two checkbox-related components:
Checkbox: main component to display a checkbox.CheckboxGroup: container component that groups related checkboxes together, providing a shared label, optional description, error handling, and state management.
import { Checkbox, CheckboxGroup } from '@cimpress-ui/react';Controlled/uncontrolled usage
Section titled “Controlled/uncontrolled usage”This component can be used in a controlled or uncontrolled way.
In the controlled way, this component doesn’t maintain its own internal state, and its value is provided by the parent component. Use the controlled way when you need to be able to change the state of this component in other parts of your application.
In the uncontrolled way, this component maintains its own internal state, and can optionally notify the parent component when its internal state changes. Use the uncontrolled way when you don’t need to change the state of this component in other parts of your application.
Single checkbox
Section titled “Single checkbox”import { Checkbox, Stack } from '@cimpress-ui/react';import { useState } from 'react';
export default function Demo() { const [isSelected, setIsSelected] = useState(false); const [isIndeterminate, setIsIndeterminate] = useState(false);
return ( <Stack gap={32}> <Stack gap={16}> <Checkbox isSelected={isSelected} isIndeterminate={isIndeterminate} onChange={setIsSelected}> I am controlled by React state </Checkbox>
<Checkbox isSelected={isIndeterminate} onChange={setIsIndeterminate}> Make the above checkbox indeterminate </Checkbox> </Stack>
<Checkbox>I am uncontrolled and unselected by default</Checkbox>
<Checkbox defaultSelected>I am uncontrolled and selected by default</Checkbox> </Stack> );}Checkbox group
Section titled “Checkbox group”import { Checkbox, CheckboxGroup, Stack } from '@cimpress-ui/react';import { useState } from 'react';
export default function Demo() { const [selectedValues, setSelectedValues] = useState<string[]>(['apple']);
return ( <Stack gap={32}> <CheckboxGroup label="Favorite fruits (controlled)" value={selectedValues} onChange={setSelectedValues}> <Checkbox value="apple">Apple</Checkbox> <Checkbox value="banana">Banana</Checkbox> <Checkbox value="cherry">Cherry</Checkbox> </CheckboxGroup>
<CheckboxGroup label="Favorite fruits (uncontrolled)" defaultValue={['apple']}> <Checkbox value="apple">Apple</Checkbox> <Checkbox value="banana">Banana</Checkbox> <Checkbox value="cherry">Cherry</Checkbox> </CheckboxGroup> </Stack> );}Accessibility notes
Section titled “Accessibility notes”The checkbox state can be changed by pressing Enter or Space, similar to a native HTML checkbox.
Each checkbox within CheckboxGroup is a separate tab stop within the page’s tab order. Use the Tab key to navigate between checkboxes.
Checkbox and CheckboxGroup require textual labels to remain accessible to assistive technologies. See our accessibility guide for more details.
Group selection
Section titled “Group selection”Selection state of checkboxes within CheckboxGroup cannot be set individually.
Please use CheckboxGroup’s value/defaultValue props instead.
// This won't work! :(<CheckboxGroup label="Favourite fruits"> <Checkbox value="apple">Apple</Checkbox> <Checkbox value="banana">Banana</Checkbox> <Checkbox value="cherry" defaultSelected>Cherry</Checkbox></CheckboxGroup>
// But this will :)<CheckboxGroup label="Favourite fruits" defaultValue={['cherry']}> <Checkbox value="apple">Apple</Checkbox> <Checkbox value="banana">Banana</Checkbox> <Checkbox value="cherry">Cherry</Checkbox></CheckboxGroup>Checkbox and checkbox group can integrate with native HTML forms. See our forms guide to learn how to work with forms.
API reference
Section titled “API reference”Checkbox
Section titled “Checkbox”Allows users to mark an item as selected. Can be used standalone, or as part of CheckboxGroup.
- Ref<HTMLLabelElement>
-
The
reftype for this component.
CheckboxProps
- StringLikeChildren
children
Section titled “ children ” -
The label rendered next to the checkbox.
- string
-
The element's unique identifier. See MDN.
- boolean
data-cim-style-root
Section titled “ data-cim-style-root ” -
Use this attribute to "claim" the component tree for exclusive Cimpress UI usage.
- string
UNSAFE_className
Section titled “ UNSAFE_className ” -
Sets the CSS className for the element. Only use as a last resort. Use style props instead.
See styling guide.
- CSSProperties
UNSAFE_style
Section titled “ UNSAFE_style ” -
Sets the CSS style for the element. Only use as a last resort. Use style props instead.
See styling guide.
- string
aria-label
Section titled “ aria-label ” -
Defines a string value that labels the current element.
- string
aria-labelledby
Section titled “ aria-labelledby ” -
Identifies the element (or elements) that labels the current element.
- string
aria-describedby
Section titled “ aria-describedby ” -
Identifies the element (or elements) that describes the object.
- string
aria-details
Section titled “ aria-details ” -
Identifies the element (or elements) that provide a detailed, extended description for the object.
- boolean
defaultSelected
Section titled “ defaultSelected ” -
Whether the element should be selected (uncontrolled).
- boolean
isSelected
Section titled “ isSelected ” -
Whether the element should be selected (controlled).
- (isSelected: boolean) => void
onChange
Section titled “ onChange ” -
Handler that is called when the element's selection state changes.
- string
value
Section titled “ value ” -
The value of the input element, used when submitting an HTML form. See MDN.
- boolean
isIndeterminate
Section titled “ isIndeterminate ” -
Indeterminism is presentational only. The indeterminate visual representation remains regardless of user interaction.
- string
-
The name of the input element, used when submitting an HTML form. See MDN.
- string
-
The
<form>element to associate the input with. The value of this attribute must be the id of a<form>in the same document. See MDN. - boolean
autoFocus
Section titled “ autoFocus ” -
Whether the element should receive focus on render.
- boolean
isRequired
Section titled “ isRequired ” -
Whether user input is required on the input before form submission.
- boolean
isInvalid
Section titled “ isInvalid ” -
Whether the input value is invalid.
- boolean
isDisabled
Section titled “ isDisabled ” -
Whether the input is disabled.
- boolean
isReadOnly
Section titled “ isReadOnly ” -
Whether the input can be selected but not changed by the user.
StyleProps
- Responsive<Spacing | "auto">
margin
Section titled “ margin ” -
The amount of margin applied to all edges of this component.
- Responsive<Spacing | "auto">
marginX
Section titled “ marginX ” -
The amount of margin applied to the left and right edges of this component. Takes priority over
margin. - Responsive<Spacing | "auto">
marginY
Section titled “ marginY ” -
The amount of margin applied to the top and bottom edges of this component. Takes priority over
margin. - Responsive<Spacing | "auto">
marginTop
Section titled “ marginTop ” -
The amount of margin applied to the top edge of this component. Takes priority over
marginYandmargin. - Responsive<Spacing | "auto">
marginRight
Section titled “ marginRight ” -
The amount of margin applied to the right edge of this component. Takes priority over
marginXandmargin. - Responsive<Spacing | "auto">
marginBottom
Section titled “ marginBottom ” -
The amount of margin applied to the bottom edge of this component. Takes priority over
marginYandmargin. - Responsive<Spacing | "auto">
marginLeft
Section titled “ marginLeft ” -
The amount of margin applied to the left edge of this component. Takes priority over
marginXandmargin.
CheckboxGroup
Section titled “CheckboxGroup”Allows users to select one or more items from a visible list.
- Ref<HTMLDivElement>
-
The
reftype for this component.
CheckboxGroupProps
- ReactNode
children *
Section titled “ children * ” -
Checkboxes belonging to the group. Each checkbox must have a
valueprop defined. - Responsive<Direction>
direction
Section titled “ direction ” - Defaults to 'vertical' .
Determines the direction in which checkboxes will be stacked.
- string
-
The element's unique identifier. See MDN.
- boolean
data-cim-style-root
Section titled “ data-cim-style-root ” -
Use this attribute to "claim" the component tree for exclusive Cimpress UI usage.
- string
UNSAFE_className
Section titled “ UNSAFE_className ” -
Sets the CSS className for the element. Only use as a last resort. Use style props instead.
See styling guide.
- CSSProperties
UNSAFE_style
Section titled “ UNSAFE_style ” -
Sets the CSS style for the element. Only use as a last resort. Use style props instead.
See styling guide.
- string
label
Section titled “ label ” -
The content to display as the label.
- string
aria-label
Section titled “ aria-label ” -
Defines a string value that labels the current element.
- string
aria-labelledby
Section titled “ aria-labelledby ” -
Identifies the element (or elements) that labels the current element.
- string
aria-describedby
Section titled “ aria-describedby ” -
Identifies the element (or elements) that describes the object.
- string
aria-details
Section titled “ aria-details ” -
Identifies the element (or elements) that provide a detailed, extended description for the object.
- string
-
The
<form>element to associate the input with. The value of this attribute must be the id of a<form>in the same document. See MDN. - string
-
The name of the input element, used when submitting an HTML form. See MDN.
- string
description
Section titled “ description ” -
A description for the field. Provides a hint such as specific requirements for what to choose.
- FieldError
error
Section titled “ error ” -
An error message for the field.
- (value: string[]) => string | true | string[] | undefined
validate
Section titled “ validate ” -
A function that returns an error message (or
true) if a given value is invalid. Validation errors are displayed to the user when the form is submitted. For real-time validation, use theerrorprop instead. - boolean
isRequired
Section titled “ isRequired ” -
Whether user input is required on the input before form submission.
- boolean
isInvalid
Section titled “ isInvalid ” -
Whether the input value is invalid.
- boolean
isDisabled
Section titled “ isDisabled ” -
Whether the input is disabled.
- boolean
isReadOnly
Section titled “ isReadOnly ” -
Whether the input can be selected but not changed by the user.
- string[]
value
Section titled “ value ” -
The current value (controlled).
- string[]
defaultValue
Section titled “ defaultValue ” -
The default value (uncontrolled).
- (value: string[]) => void
onChange
Section titled “ onChange ” -
Handler that is called when the value changes.
StyleProps
- Responsive<Spacing | "auto">
margin
Section titled “ margin ” -
The amount of margin applied to all edges of this component.
- Responsive<Spacing | "auto">
marginX
Section titled “ marginX ” -
The amount of margin applied to the left and right edges of this component. Takes priority over
margin. - Responsive<Spacing | "auto">
marginY
Section titled “ marginY ” -
The amount of margin applied to the top and bottom edges of this component. Takes priority over
margin. - Responsive<Spacing | "auto">
marginTop
Section titled “ marginTop ” -
The amount of margin applied to the top edge of this component. Takes priority over
marginYandmargin. - Responsive<Spacing | "auto">
marginRight
Section titled “ marginRight ” -
The amount of margin applied to the right edge of this component. Takes priority over
marginXandmargin. - Responsive<Spacing | "auto">
marginBottom
Section titled “ marginBottom ” -
The amount of margin applied to the bottom edge of this component. Takes priority over
marginYandmargin. - Responsive<Spacing | "auto">
marginLeft
Section titled “ marginLeft ” -
The amount of margin applied to the left edge of this component. Takes priority over
marginXandmargin.