Checked hierarchy with native form values
Select leaf IDs through a searchable tree. Parent states reflect loaded enabled children; native external forms retain values, controlled ownership and reset behaviour.
Keyboard
- Arrow Down enters the tree from search. Arrow keys move, open and close branches.
- Space selects loaded leaf choices; Enter opens a branch or toggles a leaf. Escape closes.
- The application checks the required button field on submit and returns focus to it.
States covered
- Mixed parent
- Disabled ancestor and child
- External native form
- Uncontrolled reset
- Controlled and read-only values
- Unknown IDs retained
- React prevented button reset
Install this complete example and its component dependencies.
npx shadcn@latest add https://ui.bld8.dev/r/example-tree-select-native.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,useRef,useState} from 'react';
import {Button,TreeSelect,UIProvider,type TreeSelectNode} from '@bld8/ui';
const nodes:TreeSelectNode[]=[
{id:'product',label:'Product',children:[
{id:'interface',label:'Interface',description:'UI foundations and components.'},
{id:'motion',label:'Motion',searchTerms:['animation','transitions']},
{id:'legacy',label:'Archived patterns',disabled:true}]},
{id:'research',label:'Research'},
{id:'restricted',label:'Restricted',disabled:true,children:[{id:'private-notes',label:'Private notes'}]}
];
export default function TreeFormExample(){
const formId=useId(),field=useRef<HTMLButtonElement>(null);
const [owned,setOwned]=useState<string[]>(['research']),[status,setStatus]=useState(''),[error,setError]=useState(''),[changes,setChanges]=useState(0),[touched,setTouched]=useState(false),[preventReset,setPreventReset]=useState(false);
return <UIProvider><div style={{display:'grid',gap:16}}>
<label className="bui-choice"><input className="bui-checkbox" type="checkbox" checked={preventReset} onChange={event=>setPreventReset(event.target.checked)}/>Prevent tree reset</label>
<form id={formId} aria-label="Tree selection form" onReset={event=>{if(preventReset)event.preventDefault();}} onSubmit={event=>{
event.preventDefault();const data=new FormData(event.currentTarget),ids=data.getAll('access').map(String);
if(!ids.length){setError('Choose at least one team area.');field.current?.focus();return;}
setStatus(ids.join(',')+' | '+data.getAll('owned').join(',')+' | '+data.getAll('readonly').join(',')+' | '+data.getAll('disabled').length);
}}><div className="actions"><Button type="submit">Read tree values</Button><Button type="reset" variant="secondary">Reset tree values</Button></div></form>
<TreeSelect label="Team access" nodes={nodes} name="access" form={formId} defaultValue={['interface','unavailable-id']} defaultExpandedIds={['product','restricted']} required inputRef={field} error={error} hint="Parents select enabled leaf choices. Unavailable IDs are kept until you clear them." onBlur={()=>setTouched(true)} onValueChange={()=>{setError('');setChanges(count=>count+1);}}/>
<TreeSelect label="Application access" nodes={nodes} name="owned" form={formId} value={owned} onValueChange={setOwned}/>
<TreeSelect label="Read-only access" nodes={nodes} name="readonly" form={formId} defaultValue={['research']} readOnly/>
<TreeSelect label="Disabled access" nodes={nodes} name="disabled" form={formId} defaultValue={['interface']} disabled/>
<p role="status" aria-label="Tree form values">{status}</p>
<p role="status" aria-label="Tree change count">{changes}</p>
<p className="bui-hint">Tree field touched: {touched?'yes':'no'}</p>
</div></UIProvider>;
}