The Checkbox
component is used in forms when a user needs to select multiple
values from several options.
Native HTML checkboxes are 100% accessible by default, so we used a very common CSS technique to style the checkboxes.
Basic usage of Checkbox
.
You can override the color scheme of the Checkbox
to any color key specified
in theme.colors
.
Pass the size
prop to change the size of the Checkbox
. Values can be either
sm
, md
or lg
.
We added the spacing
prop to customize the spacing between the Checkbox
and
label text.
You can customize the color and size of the check icon by passing the
iconColor
and iconSize
prop.
Chakra exports a CheckboxGroup
component to help manage the checked state of
its children Checkbox
components and conveniently pass a few shared style
props to each. See the props table at the bottom of this page for a list of the
shared styling props.
The useCheckbox
hook is exported with state and focus management logic for use
in creating tailor-made checkbox component for your application.
The useCheckboxGroup
hook is exported with state management logic for use in
creating tailor-made checkbox group component for your application.
Name | Type | Description | Default |
---|---|---|---|
colorScheme | string | - | |
defaultIsChecked | boolean | If `true`, the checkbox will be initially checked. | - |
icon | React.ReactElement | The checked icon to use | CheckboxIcon |
iconColor | string | The color of the checkbox icon when checked or indeterminate | - |
iconSize | string | number | The size of the checkbox icon when checked or indeterminate | - |
isChecked | boolean | If `true`, the checkbox will be checked. You'll need to pass `onChange` to update its value (since it is now controlled) | - |
isDisabled | boolean | If `true`, the checkbox will be disabled | - |
isFocusable | boolean | If `true` and `isDisabled` is passed, the checkbox will remain tabbable but not interactive | - |
isIndeterminate | boolean | If `true`, the checkbox will be indeterminate. This only affects the icon shown inside checkbox and does not modify the isChecked property. | - |
isInvalid | boolean | If `true`, the checkbox is marked as invalid. Changes style of unchecked state. | - |
isReadOnly | boolean | If `true`, the checkbox will be readonly | - |
isRequired | boolean | If `true`, the checkbox input is marked as required, and `required` attribute will be added | - |
name | string | The name of the input field in a checkbox (Useful for form submission). | - |
onChange | ((event: ChangeEvent<HTMLInputElement>) => void) | The callback invoked when the checked state of the `Checkbox` changes.. | - |
orientation | "horizontal" | "vertical" | - | |
size | string | - | |
spacing | SystemProps["marginLeft"] | The spacing between the checkbox and its label text | 0.5rem |
styleConfig | Record<string, any> | - | |
value | string | number | The value to be used in the checkbox input. This is the value that will be returned on form submission. | - |
variant | string | - |
Name | Type | Description | Default |
---|---|---|---|
colorScheme | string | - | |
defaultValue | ReactText[] | The initial value of the checkbox group | - |
isNative | boolean | If `true`, input elements will receive `checked` attribute instead of `isChecked`. This assumes, you're using native radio inputs | - |
onChange | ((value: ReactText[]) => void) | The callback fired when any children Checkbox is checked or unchecked | - |
size | string | - | |
styleConfig | Record<string, any> | - | |
value | ReactText[] | The value of the checkbox group | - |
variant | string | - |
The following style props can be set on CheckboxGroup
and they will be
forwarded to all children Checkbox
components.
Name | Type | Default | Description |
---|---|---|---|
colorScheme | string | The color of the checkbox when it is checked. This should be one of the color keys in the theme (e.g."green", "red"). | |
children | React.ReactNode | The content of the checkbox group. | |
size | sm , md , lg | md | The size of the checkbox. |
variant | string | The variant of the checkbox. |