Checkbox
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.
Import#
import { Checkbox, CheckboxGroup } from "@nature-ui/core";
Usage#
Basic usage of Checkbox
.
<Checkbox defaultIsChecked>Checkbox</Checkbox>
Disabled Checkbox#
<Stack spacing={10} row><Checkbox isDisabled>Checkbox</Checkbox><Checkbox isDisabled defaultIsChecked>Checkbox</Checkbox></Stack>
Checkbox with custom color#
You can override the color of the Checkbox
to any color key specified in
theme.colors
.
<Stack spacing={10} row><Checkbox color="bg-purple-500 hover:bg-purple-700" defaultIsChecked>Checkbox</Checkbox><Checkbox color="bg-green-500 hover:bg-green-700" defaultIsChecked>Checkbox</Checkbox></Stack>
Checkbox sizes#
Pass the size
prop to change the size of the Checkbox
. Values can be either
sm
, md
or lg
.
<Stack spacing={10} row><Checkbox size="sm" color="bg-purple-500 hover:bg-purple-700">Checkbox</Checkbox><Checkbox size="md" color="bg-green-500 hover:bg-green-700" defaultIsChecked>Checkbox</Checkbox><Checkboxsize="lg"color="bg-orange-500 hover:bg-orange-700"defaultIsChecked>Checkbox</Checkbox></Stack>
Invalid Checkbox#
<Checkbox isInvalid>Checkbox</Checkbox>
Changing the spacing#
We added the spacing
prop to customize the spacing between the Checkbox
and
label text.
<Checkbox spacing="1rem">Option</Checkbox>
Changing the icon color and size#
You can customize the color and size of the check icon by passing the
iconColor
and iconSize
prop.
<Checkbox iconColor="gray-900" iconSize="1rem">Option</Checkbox>
Indeterminate#
function IndeterminateExample() {const [checkedItems, setCheckedItems] = React.useState([false, false]);const allChecked = checkedItems.every(Boolean);const isIndeterminate = checkedItems.some(Boolean) && !allChecked;return (<><CheckboxisChecked={allChecked}isIndeterminate={isIndeterminate}onChange={(e) => setCheckedItems([e.target.checked, e.target.checked])}>Parent Checkbox</Checkbox><Stack className={"pl-6 mt-1"} spacing={1}><CheckboxisChecked={checkedItems[0]}onChange={(e) => setCheckedItems([e.target.checked, checkedItems[1]])}>Child Checkbox 1</Checkbox><CheckboxisChecked={checkedItems[1]}onChange={(e) => setCheckedItems([checkedItems[0], e.target.checked])}>Child Checkbox 2</Checkbox></Stack></>);}
CheckboxGroup#
Nature UI 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.
<CheckboxGroupcolor="bg-green-600 hover:bg-green-700"defaultValue={["naruto", "kakashi"]}><Stack row><Checkbox value="naruto">Naruto</Checkbox><Checkbox value="sasuke">Sasuke</Checkbox><Checkbox value="kakashi">kakashi</Checkbox></Stack></CheckboxGroup>
Hooks#
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.
Props#
Checkbox Props#
Name | Type | Default | Description |
---|---|---|---|
id | string | The id assigned to input field | |
name | string | The name of the input field in a checkbox (Useful for form submission) | |
value | string or number | The value to be used in the checkbox input. This is the value that will be returned on form submission. | |
color | string | The color of the checkbox when it's checked. This should be one of the color keys in the theme (e.g."green", "red") | |
defaultIsChecked | boolean | If true , the checkbox will be initially checked. | |
isChecked | boolean | If true , the checkbox will be checked. You'll need to pass onChange to update it's value (since it's now controlled) | |
isIndeterminate | boolean | If true , the checkbox will be indeterminate. This only affects the icon shown inside checkbox | |
isFullWidth | boolean | If true , the checkbox should take up the full width of the parent. | |
size | sm , md , lg | md | The size (width and height) of the checkbox |
isDisabled | boolean | If true , the checkbox will be disabled | |
isInvalid | boolean | If true , the checkbox is marked as invalid. Changes style of unchecked state. | |
children | React.ReactNode | The children of the checkbox. | |
onChange | function | Function called when the state of the checkbox changes. | |
onBlur | function | Function called when you blur out of the checkbox. | |
onFocus | function | Function called when the checkbox receive focus. | |
aria-label | string | An accessible label for the checkbox in event there's no visible label or children passed | |
aria-labelledby | string | Id that points to the label for the checkbox in event no children was passed |
CheckboxGroup Props#
CheckboxGroup composes Box
so you can pass all Box
props.
Name | Type | Default | Description |
---|---|---|---|
id | string | The id of the checkbox group. | |
name | string | The name of the checkbox group. This attribute is | |
value | Array<Checkbox["value"]> | The value of the checkbox group | |
defaultValue | Array<Checkbox["value"]> | The initial value of the checkbox group | |
colorScheme | string | The color of the checkbox when it's checked. This should be one of the color keys in the theme (e.g."green", "red") | |
onChange | (values: Array<Checkbox["value"]>): void | The callback fired when any children Checkbox is checked or unchecked | |
children | React.ReactNode | The content of the checkbox group. Must be the Checkbox component | |
spacing | StyledSystem.MarginProps | 8px | The space between each checkbox |
size | sm , md , lg | md | The size of the checkbox, it's forwarded to all children checkbox. |