Grouped validation, native reset and external forms
Choose delivery channels with a group minimum, submit stable repeated values and reset an uncontrolled collection. Read-only choices remain submitted; disabled choices do not.
Keyboard
- Tab visits native checkboxes. Space changes an editable choice.
- Submitting without a delivery channel focuses the first enabled choice.
- Reset restores the uncontrolled defaults unless Prevent reset is checked; controlled values remain owned by the host.
States covered
- External native form
- Required group validation
- Repeated values
- Read-only submission
- Disabled exclusion
- Uncontrolled and controlled reset
- Prevented reset
Install this complete example and its component dependencies.
npx shadcn@latest add https://ui.bld8.dev/r/example-checkbox-group-native-form.jsonComplete source
/*!
MIT License
Copyright (c) 2026 Jaison
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
'use client';
import '@bld8/ui/styles.css';
import {useId,useState} from 'react';
import {Button,Checkbox,CheckboxGroup} from '@bld8/ui';
const channels=[{value:'email',label:'Email',description:'A review link in your inbox.'},{value:'in-app',label:'In-app',description:'An entry in your review queue.'},{value:'legacy',label:'Legacy feed',disabled:true}];
export default function CheckboxGroupFormExample(){
const id=useId(),[prevent,setPrevent]=useState(false),[owned,setOwned]=useState<string[]>(['in-app']),[result,setResult]=useState('No delivery selection submitted.'),[reversed,setReversed]=useState(false);
return <div style={{display:'grid',gap:24,maxWidth:600}}>
<CheckboxGroup label="Delivery channels" name="delivery" form={id} options={reversed?[...channels].reverse():channels} defaultValue={['email']} required hint="Choose at least one available channel."/>
<Button variant="ghost" onClick={()=>setReversed(value=>!value)}>Reverse delivery choice order</Button>
<CheckboxGroup label="Host-owned channels" name="owned" form={id} options={channels} value={owned} onValueChange={setOwned}/>
<CheckboxGroup label="Read-only channels" name="locked" form={id} options={channels} defaultValue={['email']} readOnly/>
<CheckboxGroup label="Disabled channels" name="disabled" form={id} options={channels} defaultValue={['email']} disabled/>
<Checkbox label="Prevent channel reset" checked={prevent} onCheckedChange={setPrevent}/>
<form id={id} aria-label="Delivery channel submission" onReset={event=>{if(prevent){event.preventDefault();return;}setOwned(['in-app']);setResult('Delivery defaults restored.');}} onSubmit={event=>{event.preventDefault();const data=new FormData(event.currentTarget);setResult('Delivery: '+data.getAll('delivery').join(', ')+' | Host: '+data.getAll('owned').join(', ')+' | Locked: '+data.getAll('locked').join(', ')+' | Disabled: '+data.has('disabled'));}}>
<div style={{display:'flex',gap:12,flexWrap:'wrap'}}><Button type="submit">Read delivery values</Button><Button variant="secondary" type="reset">Reset delivery choices</Button></div>
</form><p role="status">{result}</p>
</div>;
}