The Checkbox component is a form control that allows users to select one or more options from a set. It provides a clean and accessible way to handle boolean selections and multi-choice scenarios.
import { Checkbox } from '@dashflowx/core'export default function CheckboxExample() {const [checked, setChecked] = useState(false);return (<Checkboxlabel="Accept terms and conditions"checked={checked}onChange={(e) => setChecked(e.target.checked)}variant="basic"/>)}
Preview:
| Prop | Type | Default | Description |
|---|---|---|---|
label | string | 'Checkbox Label' | The label text for the checkbox |
checked | boolean | false | Whether the checkbox is checked |
disabled | boolean | false | Whether the checkbox is disabled |
variant | 'basic' | 'one' | 'basic' | The visual variant of the checkbox |
value | any | - | The value associated with the checkbox |
onChange | function | - | Callback function when checkbox state changes |
<Checkboxlabel="I agree to the terms"checked={false}onChange={handleChange}variant="basic"/>
Preview:
<Checkboxlabel="Subscribe to newsletter"checked={true}onChange={handleChange}variant="basic"/>
Preview:
<Checkboxlabel="This option is disabled"checked={false}disabled={true}variant="basic"/>
Preview:
<div className="space-y-4"><Checkboxlabel="Basic variant"checked={true}variant="basic"/><Checkboxlabel="One variant"checked={true}variant="one"/></div>
Preview:
<div className="space-y-3"><Checkboxlabel="I agree to the Terms of Service"checked={termsAccepted}onChange={(e) => setTermsAccepted(e.target.checked)}variant="basic"/><Checkboxlabel="I agree to the Privacy Policy"checked={privacyAccepted}onChange={(e) => setPrivacyAccepted(e.target.checked)}variant="basic"/><Checkboxlabel="Send me promotional emails"checked={promotionalEmails}onChange={(e) => setPromotionalEmails(e.target.checked)}variant="basic"/></div>
<div className="space-y-4"><h3 className="text-lg font-semibold">Notification Settings</h3><Checkboxlabel="Email notifications"checked={emailNotifications}onChange={(e) => setEmailNotifications(e.target.checked)}variant="basic"/><Checkboxlabel="Push notifications"checked={pushNotifications}onChange={(e) => setPushNotifications(e.target.checked)}variant="basic"/><Checkboxlabel="SMS notifications"checked={smsNotifications}onChange={(e) => setSmsNotifications(e.target.checked)}variant="basic"/></div>
<div className="space-y-2"><h4 className="font-medium">Select your interests:</h4><Checkboxlabel="Technology"checked={interests.includes('technology')}onChange={() => toggleInterest('technology')}variant="basic"/><Checkboxlabel="Design"checked={interests.includes('design')}onChange={() => toggleInterest('design')}variant="basic"/><Checkboxlabel="Business"checked={interests.includes('business')}onChange={() => toggleInterest('business')}variant="basic"/></div>
<div className="space-y-3"><Checkboxlabel="Enable advanced features"checked={advancedFeatures}onChange={(e) => setAdvancedFeatures(e.target.checked)}variant="basic"/>{advancedFeatures && (<div className="ml-6 space-y-2"><Checkboxlabel="Feature A"checked={featureA}onChange={(e) => setFeatureA(e.target.checked)}variant="basic"/><Checkboxlabel="Feature B"checked={featureB}onChange={(e) => setFeatureB(e.target.checked)}variant="basic"/></div>)}</div>
Use descriptive labels that clearly indicate what the checkbox controls.
// ✅ Good - Clear label<Checkboxlabel="I agree to receive marketing emails"checked={marketingEmails}onChange={handleChange}/>// ❌ Avoid - Unclear label<Checkboxlabel="Option 1"checked={option1}onChange={handleChange}/>
Group related checkboxes together with proper spacing and visual hierarchy.
// ✅ Good - Logical grouping<div className="space-y-3"><h4 className="font-medium">Privacy Settings</h4><Checkbox label="Public profile" checked={publicProfile} onChange={handleChange} /><Checkbox label="Show email" checked={showEmail} onChange={handleChange} /><Checkbox label="Allow messaging" checked={allowMessaging} onChange={handleChange} /></div>
Use the same variant throughout your application for consistency.
// ✅ Good - Consistent variant<div className="space-y-2"><Checkbox label="Option 1" variant="basic" /><Checkbox label="Option 2" variant="basic" /><Checkbox label="Option 3" variant="basic" /></div>
Ensure proper accessibility with clear labels and keyboard navigation.
// ✅ Good - Accessible checkbox<Checkboxlabel="I understand the risks"checked={understood}onChange={handleChange}variant="basic"/>
Use proper state management for checkbox groups.
// ✅ Good - Proper state managementconst [selectedItems, setSelectedItems] = useState<string[]>([]);const toggleItem = (item: string) => {setSelectedItems(prev =>prev.includes(item)? prev.filter(i => i !== item): [...prev, item]);};
You can customize the checkbox appearance using CSS classes:
<Checkboxlabel="Custom styled checkbox"checked={checked}onChange={handleChange}className="custom-checkbox"/>
Use different variants for different use cases:
// Basic variant - standard appearance<Checkboxlabel="Standard checkbox"variant="basic"checked={checked}onChange={handleChange}/>// One variant - alternative appearance<Checkboxlabel="Alternative checkbox"variant="one"checked={checked}onChange={handleChange}/>
The Checkbox component works well with popular form libraries:
// With React Hook Formimport { useForm, Controller } from 'react-hook-form';function MyForm() {const { control } = useForm();return (<Controllername="terms"control={control}render={({ field }) => (<Checkboxlabel="I agree to the terms"checked={field.value}onChange={field.onChange}variant="basic"/>)}/>);}
Add validation to your checkboxes:
<Checkboxlabel="I agree to the terms and conditions"checked={termsAccepted}onChange={handleChange}variant="basic"/>{!termsAccepted && (<p className="text-red-500 text-sm mt-1">You must agree to the terms and conditions</p>)}
- RadioGroup: For single-choice selections
- Switch: For toggle selections
- Select: For dropdown selections
- Input: For text input
- TextArea: For multi-line text input
For the complete API reference and advanced usage patterns, see the Checkbox API documentation.
