Async combobox example

Remote choices with an external form.

Single and multiple choices submit stable IDs to a separate form. Reset restores uncontrolled defaults without reporting edits; read-only values are kept, while disabled values are excluded.

Complete React client module · MIT · Next.js and Vite

Interactive example

Remote choices with an external form

Single and multiple choices submit stable IDs to a separate form. Reset restores uncontrolled defaults without reporting edits; read-only values are kept, while disabled values are excluded.

Keyboard

  • Type in Assignee or Reviewers; Arrow Down and Enter choose an available person.
  • Escape closes results and preserves the committed selection.
  • Tab reaches the separate form submit/reset actions; locked choices remain readable and focusable.

States covered

  • External form ownership
  • Uncontrolled reset
  • Prevented reset
  • Read-only submitted values
  • Disabled excluded values
  • Selected labels retained

Install this complete example and its component dependencies.

Terminal
npx shadcn@latest add https://ui.bld8.dev/r/example-async-form-lifecycle.json
Complete source
React / TypeScript
/*!
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 {useCallback,useId,useRef,useState} from 'react';
import {AsyncCombobox,AsyncMultiSelect,Button,Checkbox,UIProvider,type AsyncOptionsLoader} from '@bld8/ui';
const people=[{value:'maya',label:'Maya Chen'},{value:'sam',label:'Sam Rivera'},{value:'oliver',label:'Oliver Reed'}];
export default function AsyncFormLifecycle(){
  const formId=useId(),assignee=useRef<HTMLInputElement>(null);
  const [locked,setLocked]=useState(false),[preventReset,setPreventReset]=useState(false),[changes,setChanges]=useState(0),[status,setStatus]=useState('No directory values submitted.');
  const load=useCallback<AsyncOptionsLoader>(async({query,signal})=>{if(signal.aborted)throw new DOMException('Cancelled','AbortError');return {items:people.filter(person=>person.label.toLowerCase().includes(query.toLowerCase())),nextCursor:null};},[]);
  const changed=()=>setChanges(count=>count+1);
  return <UIProvider><div style={{display:'grid',gap:20}}>
    <p className="bui-hint">Local directory data. The application owns transport and access control.</p>
    <AsyncCombobox label="Assignee" name="owner" form={formId} defaultValue="maya" selectedOptions={people} loadOptions={load} debounceMs={0} required readOnly={locked} inputRef={assignee} onValueChange={changed} hint="The selected ID belongs to the separate form below."/>
    <AsyncMultiSelect label="Reviewers" name="reviewers" form={formId} defaultValue={['sam']} selectedOptions={people} loadOptions={load} debounceMs={0} readOnly={locked} onValueChange={changed}/>
    <AsyncCombobox label="Approved by" name="approvedBy" form={formId} defaultValue="maya" selectedOptions={people} loadOptions={load} readOnly hint="Read-only values are still submitted."/>
    <AsyncMultiSelect label="Unavailable reviewers" name="unavailable" form={formId} defaultValue={['oliver']} selectedOptions={people} loadOptions={load} disabled hint="Disabled values are excluded from submission."/>
    <Checkbox label="Lock directory fields" checked={locked} onCheckedChange={setLocked}/>
    <Checkbox label="Keep values on reset" checked={preventReset} onCheckedChange={setPreventReset}/>
    <form id={formId} onReset={event=>{if(preventReset){event.preventDefault();setStatus('Reset prevented.');}else setStatus('Reset requested.');}} onSubmit={event=>{event.preventDefault();setStatus(JSON.stringify([...new FormData(event.currentTarget).entries()]));}}>
      <div style={{display:'flex',gap:8,flexWrap:'wrap'}}><Button type="submit">Read directory values</Button><Button type="reset" variant="secondary">Reset directory values</Button><Button variant="ghost" onClick={()=>assignee.current?.focus()}>Focus assignee</Button></div>
    </form><p aria-label="Directory edit count">Edits: {changes}</p><p role="status" aria-label="Directory form result">{status}</p>
  </div></UIProvider>;
}