Async combobox example

Lock a field while its lookup is pending.

Switching to read-only closes a pending lookup and cancels its request. Turn holding off before another search to load the local results; selected labels and canonical values stay available.

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

Interactive example

Lock a field while its lookup is pending

Switching to read-only closes a pending lookup and cancels its request. Turn holding off before another search to load the local results; selected labels and canonical values stay available.

Keyboard

  • Leave Hold local lookups on, then type a query to start a pending request.
  • Tab to Lock lookup; the popup closes while the field retains its committed label.
  • Turn holding off before searching again to let the local result arrive.

States covered

  • Pending lookup cancellation
  • Read-only transition
  • Retained selected label
  • Explicit local adapter

Install this complete example and its component dependencies.

Terminal
npx shadcn@latest add https://ui.bld8.dev/r/example-async-readonly-cancellation.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,useEffect,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'}];
export default function AsyncReadOnlyCancellation(){
  const [locked,setLocked]=useState(false),[held,setHeld]=useState(true),[waiting,setWaiting]=useState(0),[cancelled,setCancelled]=useState(0),[status,setStatus]=useState('No lookup submitted.');
  const pending=useRef(new Map<symbol,()=>void>()),hold=useRef(true),alive=useRef(true);
  useEffect(()=>{alive.current=true;return()=>{alive.current=false;};},[]);
  const load=useCallback<AsyncOptionsLoader>(async({query,signal})=>{
    await new Promise<void>((resolve,reject)=>{const key=Symbol('lookup');let timer:ReturnType<typeof setTimeout>|undefined;const finish=()=>{clearTimeout(timer);pending.current.delete(key);signal.removeEventListener('abort',abort);if(alive.current)setWaiting(pending.current.size);resolve();};const abort=()=>{clearTimeout(timer);pending.current.delete(key);if(alive.current){setWaiting(pending.current.size);setCancelled(count=>count+1);}reject(new DOMException('Cancelled','AbortError'));};if(signal.aborted){abort();return;}pending.current.set(key,finish);signal.addEventListener('abort',abort,{once:true});setWaiting(pending.current.size);if(!hold.current)timer=setTimeout(finish,300);});
    if(signal.aborted)throw new DOMException('Cancelled','AbortError');return {items:people.filter(person=>person.label.toLowerCase().includes(query.toLowerCase())),nextCursor:null};
  },[]);
  return <UIProvider><form style={{display:'grid',gap:20}} onSubmit={event=>{event.preventDefault();setStatus('Assignee ID: '+new FormData(event.currentTarget).get('owner'));}}>
    <AsyncCombobox label="Pending assignee" name="owner" defaultValue="maya" selectedOptions={people} loadOptions={load} debounceMs={0} readOnly={locked}/>
    <AsyncMultiSelect label="Pending reviewers" name="reviewers" defaultValue={['sam']} selectedOptions={people} loadOptions={load} debounceMs={0} readOnly={locked}/>
    <Checkbox label="Hold local lookups" checked={held} onCheckedChange={next=>{hold.current=!!next;setHeld(!!next);}}/>
    <Checkbox label="Lock lookup" checked={locked} onCheckedChange={setLocked}/>
    <div style={{display:'flex',gap:8,flexWrap:'wrap'}}><Button type="submit">Read pending assignee</Button></div>
    <p aria-label="Lookup activity">Waiting: {waiting} · Cancelled: {cancelled}</p><p role="status" aria-label="Pending directory result">{status}</p>
    <p className="bui-hint">Held results demonstrate cancellation; turn holding off before another search to load the local directory. No external service is called.</p>
  </form></UIProvider>;
}