Autocomplete example

Search with recent suggestions.

An ordinary text query can use a recent suggestion or a new phrase. This example keeps four recent queries in memory; the host supplies actual search results.

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

Interactive example

Search with recent suggestions

An ordinary text query can use a recent suggestion or a new phrase. This example keeps four recent queries in memory; the host supplies actual search results.

Keyboard

  • Type a query, then use Arrow Down or Arrow Up to explore suggestions while input focus stays in the field.
  • Enter accepts the highlighted suggestion; Escape closes the list without replacing typed text.
  • Use the Search button or Enter with no highlighted suggestion to prepare the query.

States covered

  • Free text
  • Recent queries
  • Keyboard acceptance
  • No matching suggestions
  • Clear query

Install this complete example and its component dependencies.

Terminal
npx shadcn@latest add https://ui.bld8.dev/r/example-autocomplete-search.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 {useState} from 'react';
import {Autocomplete} from '@bld8/ui/autocomplete';
import {Button} from '@bld8/ui';
export default function SearchHistoryExample(){
  const [query,setQuery]=useState(''),[recent,setRecent]=useState(['Design tokens','Keyboard shortcuts','Form validation']),[status,setStatus]=useState('');
  const options=recent.map(label=>({id:'recent-'+label,label,group:'Recent queries',description:'Used in this browser session.'}));
  return <form aria-label="Documentation query" style={{display:'grid',gap:12}} onSubmit={event=>{
    event.preventDefault();const text=query.trim();if(!text)return;
    setRecent(previous=>[text,...previous.filter(item=>item!==text)].slice(0,4));setStatus('Query prepared: '+text);
  }}>
    <Autocomplete label="Search documentation" name="query" value={query} onValueChange={setQuery} options={options} placeholder="Try Design tokens" required hint="Suggestions are optional. Enter any query; recent queries stay in memory for this example."/>
    <div><Button type="submit">Search documentation</Button></div>
    <p role="status" aria-label="Search query status">{status}</p>
  </form>;
}