Autocomplete example

Native form values, reset and field errors.

An external native form reads actual text values and excludes disabled inputs. Uncontrolled reset respects preventDefault; the host resets controlled text and clears domain errors. Required and maximum-length constraints stay on the real input.

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

Interactive example

Native form values, reset and field errors

An external native form reads actual text values and excludes disabled inputs. Uncontrolled reset respects preventDefault; the host resets controlled text and clears domain errors. Required and maximum-length constraints stay on the real input.

Keyboard

  • Submit an empty required field to focus the native input.
  • Tab visits editable and read-only fields; disabled text is skipped.
  • Reset restores the original uncontrolled text; the checkbox demonstrates a prevented reset.

States covered

  • External form
  • Native required validation
  • Host validation and ref focus
  • Uncontrolled reset
  • Controlled reset
  • Read-only submission
  • Disabled exclusion
  • Prevented reset

Install this complete example and its component dependencies.

Terminal
npx shadcn@latest add https://ui.bld8.dev/r/example-autocomplete-form.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 {useId,useRef,useState} from 'react';
import {Autocomplete} from '@bld8/ui/autocomplete';
import {Button} from '@bld8/ui';
const teams=[{id:'design',label:'Design'},{id:'research',label:'Research'},{id:'engineering',label:'Engineering'}];
export default function AutocompleteFormExample(){
  const formId=useId(),field=useRef<HTMLInputElement>(null);
  const [owned,setOwned]=useState('Research'),[error,setError]=useState(''),[status,setStatus]=useState(''),[preventReset,setPreventReset]=useState(false),[changes,setChanges]=useState(0);
  return <div style={{display:'grid',gap:14}}>
    <label className="bui-choice"><input type="checkbox" className="bui-checkbox" checked={preventReset} onChange={event=>setPreventReset(event.target.checked)}/>Prevent autocomplete reset</label>
    <form id={formId} aria-label="Autocomplete native form" onReset={event=>{if(preventReset){event.preventDefault();return;}setOwned('Research');setError('');setStatus('');}} onSubmit={event=>{
      event.preventDefault();const data=new FormData(event.currentTarget),team=String(data.get('team')||'');
      if(team.trim().length<3){setError('Use at least three non-space characters.');field.current?.focus();return;}
      setError('');setStatus('Values: '+team+' | '+data.get('owned')+' | '+data.get('readonly')+' | disabled '+data.getAll('disabled').length);
    }}><div style={{display:'flex',gap:8,flexWrap:'wrap'}}><Button type="submit">Read autocomplete values</Button><Button type="reset" variant="secondary">Reset autocomplete values</Button></div></form>
    <Autocomplete label="Team name" options={teams} defaultValue="Design" name="team" form={formId} inputRef={field} required maxLength={60} hint="Any team name is valid; the host requires at least three non-space characters." error={error} onInvalid={()=>setError('Enter a team name.')} onValueChange={()=>{setError('');setChanges(count=>count+1);}}/>
    <Autocomplete label="Controlled team" options={teams} value={owned} onValueChange={setOwned} name="owned" form={formId}/>
    <Autocomplete label="Read-only team" options={teams} defaultValue="Operations" name="readonly" form={formId} readOnly/>
    <Autocomplete label="Disabled team" options={teams} defaultValue="Finance" name="disabled" form={formId} disabled/>
    <p role="status" aria-label="Autocomplete form values">{status}</p>
    <p role="status" aria-label="Autocomplete change count">{changes}</p>
  </div>;
}