Forms.autocomplete
Progressive
Renders a filterable autocomplete input backed by a hidden form value.
This is intended for searching and selecting from a known set of options. Use
select/1 when you want a trigger-driven listbox instead of a text input.
When to use it
Use autocomplete/1 when the person typing should search by label, but the
form needs to submit a separate stable value through the hidden input.
Add search-only terms with the option's :keywords attribute. Keywords are
not rendered or announced and are only considered when the visible label
does not match the current query.
Prefer combobox/1 for simpler label-in/label-out filtering where the typed
text itself is the selected value and you do not need a separate hidden form
field.
For server-backed search, keep the current query in LiveView assigns and
update the option list on phx-keyup. The event includes the typed text as
"value". The hidden input holds the selected value. The visible input
supports debounced LiveView events.
Set filter={false} to show all server results in their supplied order,
without matching the query against labels or keywords. This works with both
:input and :popup variants. Client-side filtering defaults to true.
For on-demand server search, set search_event="search-owners". Typing does
not send input/change events to LiveView; Enter in the text field or the
built-in Search button pushes that event with %{"query" => query}. Use
search_label to change the button text and phx-target={@myself} to target
a LiveComponent. Both input and popup variants support this mode.
In this mode, use the arrow keys to focus a result, then Enter to select it, or click a result. Enter in the text field always searches, even when old results are visible. Searches are ignored while disabled or loading. Escape and clicking outside restore the committed selection without searching.
Server search
Start with an empty @owners list and replace it with each search result.
Accounts.search_owners/1 is your application’s query, returning records with
id, label, description, and keywords.
Debounced phx-keyup sends "value"; on-demand search_event sends "query".
This handler supports both examples:
def mount(_params, _session, socket) do
{:ok, assign(socket, owner_id: nil, owners: [])}
end
def handle_event("search-owners", params, socket) do
query = params["query"] || params["value"]
{:noreply, assign(socket, :owners, Accounts.search_owners(query))}
endThe previews use Futurama fixtures and simulate latency. Try robot,
captain, or doctor; an unmatched query shows the empty state.
Autocomplete
<.autocomplete id="team-owner" name="owner" value="fry">
<:option
:for={character <- @characters}
value={character.id}
label={character.label}
description={character.description}
keywords={character.keywords}
/>
</.autocomplete>
Loading state
<.autocomplete id="repo-search" name="repo" loading={true}>
<:option value="cinder" label="cinder_ui" />
</.autocomplete>
LiveView server search
<.form for={%{}}>
<.autocomplete
id="owner-search"
name="owner"
value={@owner_id}
placeholder="Search characters..."
filter={false}
phx-keyup="search-owners"
phx-debounce="300"
aria-label="Search owners"
>
<:option
:for={owner <- @owners}
value={owner.id}
label={owner.label}
description={owner.description}
keywords={owner.keywords}
/>
<:empty>No characters match the current query.</:empty>
</.autocomplete>
</.form>
On-demand server search
<.autocomplete
id="on-demand-owner-search"
name="owner"
search_event="search-owners"
filter={false}
placeholder="Search characters..."
aria-label="Search characters on demand"
>
<:option
:for={owner <- @owners}
value={owner.id}
label={owner.label}
description={owner.description}
keywords={owner.keywords}
/>
</.autocomplete>
Autocomplete popup
<.autocomplete
id="country-search"
name="country"
value="au"
variant={:popup}
placeholder="Search countries..."
>
<:option :for={country <- @countries} value={country.id} label={country.label} />
</.autocomplete>
Grouped autocomplete
<.autocomplete
id="crew-directory-search"
name="character"
placeholder="Search characters..."
>
<:option
:for={character <- Enum.sort_by(@characters, & &1.group)}
value={character.id}
label={character.label}
description={character.description}
group={character.group}
keywords={character.keywords}
/>
</.autocomplete>
Custom autocomplete items
<.autocomplete
id="character-search"
name="character"
>
<:option :for={character <- @characters} value={character.id} label={character.label} keywords={character.keywords}>
<.avatar size={:sm} src={character.avatar} alt={character.label} />
<span class="flex flex-col">
<span class="font-medium">{character.label}</span>
<span class="text-muted-foreground text-xs">{character.description}</span>
</span>
</:option>
</.autocomplete>
With FormField
Phoenix shim
<.autocomplete field={@form[:owner]}>
<:option
:for={character <- @characters}
value={character.id}
label={character.label}
description={character.description}
keywords={character.keywords}
/>
</.autocomplete>
With label
Phoenix shim
<.autocomplete field={@form[:owner]} label="Owner">
<:option
:for={character <- @characters}
value={character.id}
label={character.label}
description={character.description}
keywords={character.keywords}
/>
</.autocomplete>
With explicit errors
Phoenix shimis required
<.autocomplete field={@form[:owner]} label="Owner" errors={["is required"]}>
<:option
:for={character <- @characters}
value={character.id}
label={character.label}
description={character.description}
keywords={character.keywords}
/>
</.autocomplete>
Inside field composition
Phoenix shimAssign a teammate to this project.
<.field>
<:label for={@form[:owner].id}>Owner</:label>
<.autocomplete field={@form[:owner]}>
<:option
:for={character <- @characters}
value={character.id}
label={character.label}
description={character.description}
keywords={character.keywords}
/>
</.autocomplete>
<:description>Assign a teammate to this project.</:description>
</.field>
Attributes
| Name | Type | Default | Values | Global Includes |
|---|---|---|---|---|
class
|
:string |
— | — | — |
content_class
|
:string |
— | — | — |
disabled
|
:boolean |
false
|
— | — |
errors
|
:list |
— | — | — |
field
|
{:struct, Phoenix.HTML.FormField} |
— | — | — |
filter
|
:boolean |
true
|
— | — |
id
|
:string |
— | — | — |
label
|
:string |
— | — | — |
loading
|
:boolean |
false
|
— | — |
loading_text
|
:string |
"Loading..."
|
— | — |
name
|
:string |
— | — | — |
open
|
:boolean |
false
|
— | — |
placeholder
|
:string |
"Search options..."
|
— | — |
rest
|
:global |
— | — |
required
,
aria-label
|
search_event
|
:string |
— | — | — |
search_label
|
:string |
"Search"
|
— | — |
value
|
:string |
— | — | — |
variant
|
:atom |
:input
|
:input
,
:popup
|
— |
Slots
| Slot | Slot Attributes |
|---|---|
empty
|
— |
option
Required
|
value
(:string)
Required
label
(:string)
Required
keywords
(:list)
description
(:string)
disabled
(:boolean)
group
(:string)
|
trigger
|
— |