Checkbox
A customizable checkbox component for boolean selection inputs.
Checkbox
The Checkbox component provides a way for users to make binary (on/off) selections. It's commonly used in forms for accepting terms, enabling settings, or selecting options.
Features
- Simple Interface: Clean checkbox with accompanying label
- Controlled Component: Easily manage checked state in parent components
- Error Handling: Displays error messages for validation
- Custom Styling: Accepts additional classes for customization
- Dark Mode Support: Automatic light/dark mode styles
Import
Props
Prop | Type | Default | Description |
---|---|---|---|
checked | boolean | false | Whether the checkbox is checked |
onChange | (checked: boolean) => void | Required | Function called when checkbox state changes |
label | string | Required | Text label for the checkbox |
error | string | — | Error message to display |
className | string | '' | Additional Tailwind classes for the container |
Usage Examples
Basic Checkbox
Checkbox with Error State
Multiple Checkboxes
Custom Styled Checkbox
Integration Examples
Form Validation
Settings Screen Example
Building a Checkbox List Component
If you need to manage a group of related checkboxes, you can create a simple wrapper component:
Best Practices
Provide Clear Labels
Use concise, descriptive labels that clearly indicate what the checkbox controls:
Handle Form Submission Validation
Always validate checkbox state before form submission when required:
Visual Feedback for Errors
Provide clear visual feedback when validation fails:
Managing Multiple Related Checkboxes
For related checkboxes, maintain values in an array:
Implementation Details
The Checkbox component is built using React Native's Pressable and View components. It renders a square with a border that changes appearance when checked. When checked, it displays a checkmark icon using the Icon component.
The component accepts a label that's displayed next to the checkbox, and an optional error message that appears below the checkbox when provided. The error message is styled in red to draw attention to validation issues.
The onChange callback is triggered whenever the user taps the checkbox, inverting the current checked state. Because this is a controlled component, you must update the checked prop in your parent component when this callback is called.
Notes
- The Checkbox is a controlled component, so you must handle its state externally
- Error messages appear below the checkbox when the error prop is provided
- The component includes appropriate spacing with margin-bottom for layout consistency
- The checkbox and label are wrapped in a Pressable, allowing users to tap either the box or label
- For groups of related checkboxes, maintain an array of selected values in your state