# Autocomplete

SvAutoComplete: a free-text search field with live suggestion shortcuts (any value accepted) - city search and a filter-syntax helper.

Uses `@svgrid/grid` (MIT). Category: Selection. Live demo: https://svgrid.com/demos/315-autocomplete/

Install: `npm i @svgrid/grid` (Svelte 5).

## About this example

SvAutoComplete in Svelte 5 as a search field: free text with a live-filtered suggestion list where any value is accepted and suggestions are shortcuts, shown as a city search with a label and a hint and as a filter-syntax helper. Controlled through value and onChange on the shared editor contract.

SvAutoComplete - a production search field: free text with a live-filtered suggestion list (accepts ANY value; suggestions are shortcuts). Copy-paste ready.

## Imports, features and API used

- Imports: `@svgrid/grid`

## Frequently asked questions

### Does the value have to match a suggestion?

No. Whatever the user types is the value; picking a suggestion just fills it in. Use SvComboBox when the value must come from a list.

### How are suggestions filtered?

By substring match on the suggestions array as you type; pass a new array to control them yourself, for example from a server.

### Which keys work?

Down opens and moves through suggestions, Enter accepts the highlighted one, Escape closes the list and keeps the typed text.

## Related documentation

- [SvAutoComplete](https://svgrid.com/docs/help/ui-components/sv-auto-complete.md)

## Related articles

- [A Tri-State Select-All Checkbox in SvGrid](https://svgrid.com/blog/tri-state-select-all/) - How SvGrid's rowSelectionFeature drives the header checkbox through unchecked, indeterminate, and checked states - and the pagination edge case that catches everyone.
- [Stable Row Identity in SvGrid (and why it matters)](https://svgrid.com/blog/stable-row-identity-getrowid/) - How SvGrid tracks rows across mutations, why object reference stability is the key to correct selection and editing, and the exact patterns to use with live data feeds.
- [A Fill Handle (Drag to Fill) in SvGrid](https://svgrid.com/blog/fill-handle-drag-to-fill/) - Build a working spreadsheet-style fill handle on top of SvGrid's cell selection and editing - pointer tracking, range highlighting, series fill, and undo/redo integration all covered.

## Source code (315-autocomplete.svelte)

```svelte
<script lang="ts">
  /**
   * SvAutoComplete - a production search field: free text with a live-filtered
   * suggestion list (accepts ANY value; suggestions are shortcuts). Copy-paste
   * ready.
   */
  import { SvAutoComplete } from '@svgrid/grid'

  const cities = [
    'Amsterdam', 'Austin', 'Bangalore', 'Barcelona', 'Berlin', 'Boston', 'Chicago',
    'Copenhagen', 'Dublin', 'Lisbon', 'London', 'Madrid', 'Melbourne', 'Munich',
    'New York', 'Paris', 'San Francisco', 'Seattle', 'Singapore', 'Stockholm', 'Sydney', 'Tokyo', 'Toronto',
  ]
  let city = $state('')
  let query = $state('')
</script>

<div class="wrap">
  <header>
    <h2>Autocomplete</h2>
    <p>A free-text field with suggestion shortcuts (unlike a combo box, any value is accepted). Search, tags, addresses.</p>
  </header>

  <div class="search">
    <SvAutoComplete
      value={city}
      suggestions={cities}
      placeholder="Search a city..."
      label="Office location"
      hint="Start typing - suggestions are just shortcuts"
      onChange={(v) => (city = v)}
    />
    <p class="out">{city ? `Selected: ${city}` : 'Nothing entered yet'}</p>
  </div>

  <div class="search">
    <SvAutoComplete
      value={query}
      suggestions={['status:open', 'assignee:me', 'label:bug', 'priority:high', 'is:archived']}
      minChars={1}
      placeholder="Filter issues..."
      label="Search syntax"
      onChange={(v) => (query = v)}
    />
  </div>
</div>

<style>
  .wrap { padding: 20px; max-width: 420px; display: flex; flex-direction: column; gap: 22px; }
  header h2 { margin: 0 0 4px; font-size: 20px; font-weight: 700; }
  header p { margin: 0; color: var(--sg-muted, #64748b); font-size: 13.5px; line-height: 1.5; }
  .search { display: flex; flex-direction: column; gap: 8px; }
  .out { margin: 0; font-size: 13px; color: var(--sg-muted, #64748b); }
</style>
```

[View this example on GitHub](https://github.com/sv-grid/sv-grid/blob/main/examples/src/demos/315-autocomplete.svelte)
