CheckBox
The CheckBox
component is a versatile and customizable input control that allows users to select one or more options from a set. It is commonly used in forms and surveys.
#
ImportPearl UI provides two checkbox-related components for different use cases:
CheckBox
: This is a single checkbox that can be toggled between checked and unchecked states.CheckBoxGroup
: This is a container for multipleCheckBox
components. It manages the state of its child checkboxes and allows you to apply shared styles to them.
import { CheckBox, CheckBoxGroup } from "pearl-ui";
#
Usageconst [checked, setIsChecked] = React.useState(false)
<CheckBox isChecked={checked} onPress={() => setIsChecked(!checked)}>Check Me!</CheckBox>
#
Using CheckBoxGroupThe CheckBoxGroup
component is useful when you need to manage the state of multiple checkboxes at once. It also allows you to apply shared styles to all child checkboxes.
const [checkedGroupValue, setCheckedGroupValue] = React.useState([])
<CheckBoxGroup size="s" colorScheme="secondary" shape="circle" defaultValue={["1", "2"]} value={checkedGroupValue} onChange={(value) => { setCheckedGroupValue(value); }}> <Stack direction="vertical" spacing="1.5"> <CheckBox value="1">Value 1</CheckBox> <CheckBox value="2">Value 2</CheckBox> <CheckBox value="3">Value 3</CheckBox> <CheckBox value="4">Value 4</CheckBox> </Stack></CheckBoxGroup>
#
Checkbox sizesThe size
prop allows you to adjust the size of the checkbox. You can use any of the keys available in
<CheckBox size="xs">Small Checkbox</CheckBox><CheckBox size="s">Regular Checkbox</CheckBox><CheckBox size="m">Large Checkbox</CheckBox><CheckBox size="l">Extra Large Checkbox</CheckBox>
#
Checkbox variantsThe variant
prop allows you to change the visual style of the checkbox. You can use any of the keys available in
<CheckBox variant="filled">Filled Checkbox</CheckBox>
<CheckBox variant="outline">Outline Checkbox</CheckBox>
#
Checkbox color schemeThe colorScheme
prop allows you to change the color palette of the checkbox. This is more powerful than simply passing a backgroundColor
style prop, as the colorScheme
prop changes all the color values in a palette through a single prop.
You can use any of the keys available in
<CheckBox colorScheme="primary">Primary Checkbox</CheckBox>
<CheckBox colorScheme="secondary">Secondary Checkbox</CheckBox>
#
Checkbox shapeThe shape
prop allows you to change the shape of the checkbox to
<CheckBox shape="square">Square Checkbox</CheckBox>
<CheckBox shape="circle">Circle Checkbox</CheckBox>
#
Checkbox checked stateThe checkbox is in a checked
state when it is pressed once and the checkmark icon becomes visible. The isChecked
prop is used to define whether the checkbox is in a checked state.
Checkbox
also allows you to update the visual style of the checkbox when it is checked using _checked
prop.
<CheckBox isChecked _checked={{ bgColor: "cyan", borderColor: "violet", }}> I am checked!</CheckBox>
#
Checkbox indeterminate stateIn some cases, you might need to display a partial checked state (also called an Indeterminate
state) by displaying an alternate icon to the user. The isIndeterminate
prop is used to define whether the checkbox is in an indeterminate state.
<CheckBox isChecked isIndeterminate _checked={{ bgColor: "cyan", borderColor: "violet", }}> I am not completely checked!</CheckBox>
#
Checkbox error stateYou can add an error state to the checkbox based on any validation criteria you use. You can also apply special visual styles to it by using the _invalid
prop. The isInvalid
prop is used to define whether the checkbox has an error.
<CheckBox isInvalid _invalid={{ bgColor: "pink", borderColor: "red", }}> Error Checkbox</CheckBox>
#
Checkbox spacingYou can use the spacing
prop to customize the spacing between the checkbox and label text. You can use any of the keys available in "xs"
.
<CheckBox spacing="5">Checkbox with lots of spacing</CheckBox>
<CheckBox spacing="1.5">Checkbox with less spacing</CheckBox>
#
Checkbox iconsYou can customize the icons used in the checkbox using the following props:
checkedIconFamily
checkedIconName
indeterminateIconFamily
indeterminateIconName
<CheckBox isChecked checkedIconFamily="AntDesign" checkedIconName="plus" indeterminateIconFamily="MaterialCommunityIcons" indeterminateIconName="music-note-half"> Custom Checkbox</CheckBox>
#
Overriding StylesThe CheckBox
component supports a variety of style props which can be used to override the pre-defined variant style in the theme. Manual style props passed into the component have a higher precedence than the default component configuration.
// passing style prop marginTop="3" adds a top margin to the checkbox<CheckBox variant="filled" marginTop="3" borderWidth={2} borderColor="green"> Check Me!</CheckBox>
#
Example#
AccessibilityCheckBox
has therole
ofcheckbox
.- When
CheckBox
is disabled or checked, it is reflected asdisabled
andchecked
respectively in screen readers. CheckBox
has the default accessibility label set as the label text passed into it. A custom label can be specified using theaccessibilityLabel
prop.
#
CheckBox Component Properties#
Supported Style PropertiesThe CheckBox
component is composed of the Stack component, which means you can pass all Stack properties to it.
#
Additional PropertiesHere is a list of additional properties that the CheckBox
component supports:
Name | Required | Type | Default | Description |
---|---|---|---|---|
size | No | "m" | Determines the size of the checkbox. | |
variant | No | "filled" | Determines the variant of the checkbox. | |
value | No | Sets the value of the checkbox if it is part of a group. | ||
isDisabled | No | false | If true, the checkbox is disabled. | |
isChecked | No | false | If true, the checkbox is in a checked state. | |
isIndeterminate | No | false | If true, the checkbox is in an indeterminate state. | |
isInvalid | No | false | If true, the checkbox is in an error state. | |
colorScheme | No | "primary" | Sets the active color palette of the checkbox. | |
spacing | No | "xs" | Sets the spacing between the checkbox and the label text. | |
shape | No | "square" | Determines the shape of the checkbox. | |
checkedIconFamily | No | "Ionicons" | Sets the family of the icon when the checkbox is in checked state. | |
checkedIconName | No | "checkmark-sharp" | Sets the name of the icon when the checkbox is in checked state. | |
indeterminateIconFamily | No | "Ionicons" | Sets the family of the icon when the checkbox is in indeterminate state. | |
indeterminateIconName | No | "remove-outline" | Sets the name of the icon when the checkbox is in indeterminate state. | |
_checked | No | {} | Style properties that are applied when the component is in a checked state. | |
_invalid | No | {} | Style properties that are applied when the component is in an invalid state. | |
_disabled | No | {} | Style properties that are applied when the component is in a disabled state. |
#
CheckBoxGroup Component Properties#
Supported Style PropertiesThe CheckBoxGroup
component is composed of the Stack component, which means you can pass all Stack properties to it.
#
Additional PropertiesHere is a list of additional properties that the CheckBoxGroup
component supports:
Name | Required | Type | Default | Description |
---|---|---|---|---|
size | No | "m" | Determines the size of all the children checkbox in the group. | |
variant | No | "filled" | Determines the variant of all the children checkbox in the group. | |
spacing | No | 2 | Sets the spacing between the children checkboxes in the group. | |
value | No | Sets the active value of the checkbox group. | ||
defaultValue | No | [] | Sets the default active value of the checkbox group. | |
onChange | No | Method that gets invoked when the value of the checkbox group changes. | ||
isDisabled | No | false | If true, the checkbox group is disabled. | |
colorScheme | No | "primary" | Sets the active color palette of all the children checkbox in the group. | |
shape | No | "square" | Determines the shape of all the children checkbox in the group. |