A checkbox allows a user to select an item, with support for validation and help text.
Selection
Use the isSelected or defaultSelected prop to set the selection state, and onChange to handle selection changes. The isIndeterminate prop overrides the selection state regardless of user interaction.
You are unsubscribed
Forms
Use the name and value props to submit the checkbox to the server. Set the isRequired prop to validate the user selects the checkbox, or implement custom client or server-side validation. See the Forms guide to learn more.
import {Checkbox} from './Checkbox';
import {Button} from './Button';
import {Form} from './Form';;
<Form>
<Checkbox
name="terms"
value="agree"
isRequired
description="By clicking this checkbox, you agree to the terms and conditions.">
Accept terms and conditions
</Checkbox>
<Button type="submit" style={{marginTop: 8}}>Submit</Button>
</Form>
API
<CheckboxField>
<CheckboxButton>Label</CheckboxButton>
<Text slot="description" />
<FieldError />
</CheckboxField>
CheckboxField
A checkbox allows a user to select an item, with support for validation and help text.
| Name | Type | |
|---|---|---|
inputRef | Ref | |
A ref for the HTML input element. | ||
isIndeterminate | boolean | |
Indeterminism is presentational only. The indeterminate visual representation remains regardless of user interaction. | ||
defaultSelected | boolean | |
Whether the element should be selected (uncontrolled). | ||
isSelected | boolean | |
Whether the element should be selected (controlled). | ||
isDisabled | boolean | |
Whether the input is disabled. | ||
isReadOnly | boolean | |
Whether the input can be selected but not changed by the user. | ||
children | ChildrenOrFunction | |
The children of the component. A function may be provided to alter the children based on component state. | ||
onChange | | |
Handler that is called when the element's selection state changes. | ||
isRequired | boolean | |
Whether user input is required on the input before form submission. | ||
isInvalid | boolean | |
Whether the input value is invalid. | ||
validate | | |
A function that returns an error message if a given value is invalid.
Validation errors are displayed to the user when the form is submitted
if | ||
validationBehavior | 'native' | 'aria' | |
Whether to use native HTML form validation to prevent form submission when the value is missing or invalid, or mark the field as required or invalid via ARIA. | ||
onFocus | | |
Handler that is called when the element receives focus. | ||
onBlur | | |
Handler that is called when the element loses focus. | ||
onFocusChange | | |
Handler that is called when the element's focus status changes. | ||
onKeyDown | | |
Handler that is called when a key is pressed. | ||
onKeyUp | | |
Handler that is called when a key is released. | ||
onPress | | |
Handler that is called when the press is released over the target. | ||
onPressStart | | |
Handler that is called when a press interaction starts. | ||
onPressEnd | | |
Handler that is called when a press interaction ends, either over the target or when the pointer leaves the target. | ||
onPressChange | | |
Handler that is called when the press state changes. | ||
onPressUp | | |
Handler that is called when a press is released over the target, regardless of whether it started on the target or not. | ||
onClick | | |
Not recommended – use | ||
style | StyleOrFunction | |
The inline style for the element. A function may be provided to compute the style based on component state. | ||
className | ClassNameOrFunction | |
The CSS className for the element. A function may be provided to compute the class based on component state. | ||
name | string | |
The name of the input element, used when submitting an HTML form. See MDN. | ||
value | string | |
The value of the input element, used when submitting an HTML form. See MDN. | ||
form | string | |
The | ||
autoFocus | boolean | |
Whether the element should receive focus on render. | ||
id | string | |
The element's unique identifier. See MDN. | ||
excludeFromTabOrder | boolean | |
Whether to exclude the element from the sequential tab order. If true, the element will not be focusable via the keyboard by tabbing. This should be avoided except in rare scenarios where an alternative means of accessing the element or its functionality via the keyboard is available. | ||
aria-controls | string | |
Identifies the element (or elements) whose contents or presence are controlled by the current element. | ||
aria-label | string | |
Defines a string value that labels the current element. | ||
aria-labelledby | string | |
Identifies the element (or elements) that labels the current element. | ||
aria-describedby | string | |
Identifies the element (or elements) that describes the object. | ||
aria-details | string | |
Identifies the element (or elements) that provide a detailed, extended description for the object. | ||
aria-errormessage | string | |
Identifies the element that provides an error message for the object. | ||
slot | string | null | |
A slot name for the component. Slots allow the component to receive props from a parent
component. An explicit | ||
render | DOMRenderFunction | |
Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components. Requirements:
| ||
Default className: react-aria-CheckboxField
| Render Prop | CSS Selector |
|---|---|
isSelected | CSS Selector: [data-selected]
|
| Whether the checkbox is selected. | |
isIndeterminate | CSS Selector: [data-indeterminate]
|
| Whether the checkbox is indeterminate. | |
isDisabled | CSS Selector: [data-disabled]
|
| Whether the checkbox is disabled. | |
isReadOnly | CSS Selector: [data-readonly]
|
| Whether the checkbox is read only. | |
isInvalid | CSS Selector: [data-invalid]
|
| Whether the checkbox invalid. | |
isRequired | CSS Selector: [data-required]
|
| Whether the checkbox is required. | |
CheckboxButton
A checkbox button is the clickable area of a checkbox, including the indicator and label.
| Name | Type | |
|---|---|---|
children | ChildrenOrFunction | |
The children of the component. A function may be provided to alter the children based on component state. | ||
onHoverStart | | |
Handler that is called when a hover interaction starts. | ||
onHoverEnd | | |
Handler that is called when a hover interaction ends. | ||
onHoverChange | | |
Handler that is called when the hover state changes. | ||
style | StyleOrFunction | |
The inline style for the element. A function may be provided to compute the style based on component state. | ||
className | ClassNameOrFunction | |
The CSS className for the element. A function may be provided to compute the class based on component state. | ||
slot | string | null | |
A slot name for the component. Slots allow the component to receive props from a parent
component. An explicit | ||
render | DOMRenderFunction | |
Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components. Requirements:
| ||
Default className: react-aria-CheckboxButton
| Render Prop | CSS Selector |
|---|---|
isSelected | CSS Selector: [data-selected]
|
| Whether the checkbox is selected. | |
isIndeterminate | CSS Selector: [data-indeterminate]
|
| Whether the checkbox is indeterminate. | |
isHovered | CSS Selector: [data-hovered]
|
| Whether the checkbox is currently hovered with a mouse. | |
isPressed | CSS Selector: [data-pressed]
|
| Whether the checkbox is currently in a pressed state. | |
isFocused | CSS Selector: [data-focused]
|
| Whether the checkbox is focused, either via a mouse or keyboard. | |
isFocusVisible | CSS Selector: [data-focus-visible]
|
| Whether the checkbox is keyboard focused. | |
isDisabled | CSS Selector: [data-disabled]
|
| Whether the checkbox is disabled. | |
isReadOnly | CSS Selector: [data-readonly]
|
| Whether the checkbox is read only. | |
isInvalid | CSS Selector: [data-invalid]
|
| Whether the checkbox invalid. | |
isRequired | CSS Selector: [data-required]
|
| Whether the checkbox is required. | |