LedgerUI.Components.Autocomplete (LedgerUI v0.2.0)

Copy Markdown View Source

A text input with a list of suggestions under it. The person types, the list narrows (or the server searches) and choosing a suggestion sets the field. Behaviour lives in the LedgerUI.Autocomplete hook (priv/static/ledger_ui/hooks/autocomplete.js).

<.autocomplete field={@form[:customer_id]} label="Customer" options={@customers}
               on_search="search_customers" placeholder="Pick a customer" clearable />

<.autocomplete field={@form[:city]} options={["Salvador", "Santos"]} free_text />

The choice is submitted through a hidden input named like the field; the visible input only shows the label. A bubbling input event fires from the hidden input, so a surrounding phx-change runs. With free_text the typed text is the value.

Without on_search the list filters itself on the client (ignoring case and accents). With it the hook pushes %{"id" => id, "query" => text} (after debounce ms and search_threshold characters) to target or the LiveView, shows loading_text until the reply and the server answers by re-rendering options. Options may carry :hint (a dimmed note at the right), :icon, :initials (an avatar, :tone "t1".."t5" picks its color, else taken from the label) and :disabled.

Summary

Functions

autocomplete(assigns)

Attributes

  • id (:string) - Defaults to nil.
  • name (:string) - Defaults to nil.
  • value (:any) - Defaults to nil.
  • field (Phoenix.HTML.FormField) - Defaults to nil.
  • label (:string) - Defaults to nil.
  • help (:string) - Defaults to nil.
  • errors (:list) - Defaults to [].
  • options (:list) - Defaults to [].
  • placeholder (:string) - Defaults to nil.
  • disabled (:boolean) - Defaults to false.
  • display_value (:string) - Defaults to nil.
  • free_text (:boolean) - Defaults to false.
  • on_search (:string) - Defaults to nil.
  • debounce (:integer) - Defaults to 300.
  • search_threshold (:integer) - Defaults to 0.
  • target (:any) - Defaults to nil.
  • open_on_focus (:boolean) - Defaults to false.
  • clearable (:boolean) - Defaults to false.
  • clear_label (:string) - Defaults to "Clear".
  • no_results (:string) - Defaults to "No results found".
  • loading_text (:string) - Defaults to "Searching…".
  • create (:string) - Defaults to nil.
  • on_create (:any) - Defaults to nil.
  • size (:string) - Defaults to "md". Must be one of "sm", "md", or "lg".
  • open (:boolean) - Renders the list open (static previews). Defaults to false.
  • class (:any) - Defaults to nil.
  • Global attributes are accepted. Supports all globals plus: ["autofocus", "form", "required"].

Slots

  • option
  • prefix