useCheckboxGroup
Provides the behavior and accessibility implementation for a checkbox group component. Checkbox groups allow users to select multiple items from a list of options.
| install | yarn add @react-aria/checkbox |
|---|---|
| version | 3.2.1 |
| usage | import {useCheckboxGroup, useCheckboxGroupItem} from '@react-aria/checkbox' |
API#
useCheckboxGroup(
(props: AriaCheckboxGroupProps,
, state: CheckboxGroupState
)): CheckboxGroupAriauseCheckboxGroupItem(
props: AriaCheckboxGroupItemProps,
state: CheckboxGroupState,
inputRef: RefObject<HTMLInputElement>
): CheckboxAriaFeatures#
Checkbox groups can be built in HTML with the
<fieldset>
and <input> elements,
however these can be difficult to style. useCheckboxGroup and useCheckboxGroupItem help achieve accessible
checkbox groups that can be styled as needed.
- Checkbox groups are exposed to assistive technology via ARIA
- Each checkbox is built with a native HTML
<input>element, which can be optionally visually hidden to allow custom styling - Full support for browser features like form autofill
- Keyboard focus management and cross browser normalization
- Group and checkbox labeling support for assistive technology
Anatomy#
A checkbox group consists of a set of checkboxes, and a label. Each checkbox includes a label and a visual selection indicator. Zero or more checkboxes within the group can be selected at a time. Users may click or touch a checkbox to select it, or use the Tab key to navigate to it and the Space key to toggle it.
useCheckboxGroup returns props for the group and its label, which you should spread
onto the appropriate element:
| Name | Type | Description |
groupProps | HTMLAttributes<HTMLElement> | Props for the checkbox group wrapper element. |
labelProps | HTMLAttributes<HTMLElement> | Props for the checkbox group's visible label (if any). |
useCheckboxGroupItem returns props for an individual checkbox:
| Name | Type | Description |
inputProps | InputHTMLAttributes<HTMLInputElement> | Props for the input element. |
Selection state is managed by the useCheckboxGroupState
hook in @react-stately/checkbox. The state object should be passed as an option to useCheckboxGroup
and useCheckboxGroupItem.
Individual checkboxes must have a visual label. If the checkbox group does not have a visible label,
an aria-label or aria-labelledby prop must be passed instead to identify the element to assistive
technology.
Note: useCheckboxGroupItem should only be used when it is contained within a checkbox group. For a
standalone checkbox, use the useCheckbox hook instead.
Example#
This example uses native input elements for the checkboxes, and React context to share state from the group
to each checkbox. An HTML <label> element wraps the native input and the text to provide an implicit label
for the radio.
import {useCheckboxGroupState} from '@react-stately/checkbox';
let CheckboxGroupContext = ReactcreateContext();
function CheckboxGroup(props) {
let {children label} = props;
let state = useCheckboxGroupState(props);
let {groupProps labelProps} = useCheckboxGroup(props state);
return (
<div ...groupProps>
<span ...labelProps>label</span>
<CheckboxGroupContextProvider value=state>
children
</CheckboxGroupContextProvider>
</div>
);
}
function Checkbox(props) {
let {children} = props;
let state = ReactuseContext(CheckboxGroupContext);
let ref = ReactuseRef();
let {inputProps} = useCheckboxGroupItem(props state ref);
let isDisabled = stateisDisabled || propsisDisabled;
let isSelected = stateisSelected(propsvalue);
return (
<label
style={
display: 'block'
color: (isDisabled && 'var(--gray)') || (isSelected && 'var(--blue)')
}>
<input ...inputProps ref=ref />
children
</label>
);
}
<CheckboxGroup label="Favorite sports">
<Checkbox value="soccer" isDisabled>
Soccer
</Checkbox>
<Checkbox value="baseball">Baseball</Checkbox>
<Checkbox value="basketball">Basketball</Checkbox>
</CheckboxGroup>import {useCheckboxGroupState} from '@react-stately/checkbox';
let CheckboxGroupContext = ReactcreateContext();
function CheckboxGroup(props) {
let {children label} = props;
let state = useCheckboxGroupState(props);
let {groupProps labelProps} = useCheckboxGroup(
props
state
);
return (
<div ...groupProps>
<span ...labelProps>label</span>
<CheckboxGroupContextProvider value=state>
children
</CheckboxGroupContextProvider>
</div>
);
}
function Checkbox(props) {
let {children} = props;
let state = ReactuseContext(CheckboxGroupContext);
let ref = ReactuseRef();
let {inputProps} = useCheckboxGroupItem(
props
state
ref
);
let isDisabled = stateisDisabled || propsisDisabled;
let isSelected = stateisSelected(propsvalue);
return (
<label
style={
display: 'block'
color:
(isDisabled && 'var(--gray)') ||
(isSelected && 'var(--blue)')
}>
<input ...inputProps ref=ref />
children
</label>
);
}
<CheckboxGroup label="Favorite sports">
<Checkbox value="soccer" isDisabled>
Soccer
</Checkbox>
<Checkbox value="baseball">Baseball</Checkbox>
<Checkbox value="basketball">Basketball</Checkbox>
</CheckboxGroup>import {useCheckboxGroupState} from '@react-stately/checkbox';
let CheckboxGroupContext = ReactcreateContext();
function CheckboxGroup(
props
) {
let {
children
label
} = props;
let state = useCheckboxGroupState(
props
);
let {
groupProps
labelProps
} = useCheckboxGroup(
props
state
);
return (
<div ...groupProps>
<span
...labelProps>
label
</span>
<CheckboxGroupContextProvider
value=state>
children
</CheckboxGroupContextProvider>
</div>
);
}
function Checkbox(
props
) {
let {children} = props;
let state = ReactuseContext(
CheckboxGroupContext
);
let ref = ReactuseRef();
let {
inputProps
} = useCheckboxGroupItem(
props
state
ref
);
let isDisabled =
stateisDisabled ||
propsisDisabled;
let isSelected = stateisSelected(
propsvalue
);
return (
<label
style={
display: 'block'
color:
(isDisabled &&
'var(--gray)') ||
(isSelected &&
'var(--blue)')
}>
<input
...inputProps
ref=ref
/>
children
</label>
);
}
<CheckboxGroup label="Favorite sports">
<Checkbox
value="soccer"
isDisabled>
Soccer
</Checkbox>
<Checkbox value="baseball">
Baseball
</Checkbox>
<Checkbox value="basketball">
Basketball
</Checkbox>
</CheckboxGroup>Styling#
See the useCheckbox docs for details on how to customize the styling of checkbox elements.
| Name | Type | Description |
name | string | The name of the CheckboxGroup, used when submitting an HTML form. |
value | string[] | The current value (controlled). |
defaultValue | string[] | The default value (uncontrolled). |
onChange | (
(value: string[]
)) => void | Handler that is called when the value changes. |
isDisabled | boolean | Whether the input is disabled. |
isReadOnly | boolean | Whether the input can be selected but not changed by the user. |
label | ReactNode | The content to display as the label. |
id | string | The element's unique identifier. See MDN. |
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. |
Properties
| Name | Type | Description |
value | | Current selected values. |
isDisabled | boolean | Whether the checkbox group is disabled. |
isReadOnly | boolean | Whether the checkbox group is read only. |
Methods
| Method | Description |
isSelected(
(value: string
)): boolean | Returns whether the given value is selected. |
setValue(
(value: string[]
)): void | Sets the selected values. |
addValue(
(value: string
)): void | Adds a value to the set of selected values. |
removeValue(
(value: string
)): void | Removes a value from the set of selected values. |
toggleValue(
(value: string
)): void | Toggles a value in the set of selected values. |
| Name | Type | Description |
groupProps | HTMLAttributes<HTMLElement> | Props for the checkbox group wrapper element. |
labelProps | HTMLAttributes<HTMLElement> | Props for the checkbox group's visible label (if any). |
| Name | Type | Description |
value | string | |
isIndeterminate | boolean | Indeterminism is presentational only. The indeterminate visual representation remains regardless of user interaction. |
children | ReactNode | The label for the element. |
onChange | (
(isSelected: boolean
)) => void | Handler that is called when the element's selection state changes. |
name | string | The name of the input element, used when submitting an HTML form. See MDN. |
isDisabled | boolean | Whether the input is disabled. |
isReadOnly | boolean | Whether the input can be selected but not changed by the user. |
validationState | ValidationState | Whether the input should display its "valid" or "invalid" visual styling. |
isRequired | boolean | Whether user input is required on the input before form submission.
Often paired with the necessityIndicator prop to add a visual indicator to the input. |
autoFocus | boolean | Whether the element should receive focus on render. |
onFocus | (
(e: FocusEvent
)) => void | Handler that is called when the element receives focus. |
onBlur | (
(e: FocusEvent
)) => void | Handler that is called when the element loses focus. |
onFocusChange | (
(isFocused: boolean
)) => void | Handler that is called when the element's focus status changes. |
onKeyDown | (
(e: KeyboardEvent
)) => void | Handler that is called when a key is pressed. |
onKeyUp | (
(e: KeyboardEvent
)) => void | Handler that is called when a key is released. |
aria-controls | string | Identifies the element (or elements) whose contents or presence are controlled by the current element. |
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. |
id | string | The element's unique identifier. See MDN. |
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. |
| Name | Type | Description |
inputProps | InputHTMLAttributes<HTMLInputElement> | Props for the input element. |
Provides state management for a checkbox group component. Provides a name for the group, and manages selection and focus state.
useCheckboxGroupState(
(props: CheckboxGroupProps
)): CheckboxGroupState| Name | Type | Description |
value | string[] | The current value (controlled). |
defaultValue | string[] | The default value (uncontrolled). |
onChange | (
(value: string[]
)) => void | Handler that is called when the value changes. |
isDisabled | boolean | Whether the input is disabled. |
isReadOnly | boolean | Whether the input can be selected but not changed by the user. |
label | ReactNode | The content to display as the label. |