A text input that suggests values from a list in a dropdown right under
its left edge: header and query parameter names, JSON paths, store names,
{{VARIABLE}} references.
The list is a colocated hook's, written in the template below: the hook sits on the input, reads data-suggestions (a JSON array of
strings) on every update and builds the dropdown inside the sibling
#<id>-suggestions container, which is rendered empty with
phx-update="ignore" so LiveView leaves it to the hook. This component
owns that contract: the relative wrapper, the container's id and
placement, the JSON and autocomplete="off".
A native <datalist> is not used because the browser places its popup
itself, off to the right on a wide input.
Accepting a suggestion sets the value and dispatches input (so a
phx-change sends it) and, on an input with phx-keyup, a keyup too.
A phx-blur input reports when it loses focus, as with typed text.
mode says what a pick completes: the whole value ("value"), the item
of a comma-separated list the cursor is in ("list"), the line the
cursor is in, for a textarea of one item per line ("lines"), a {{NAME
reference before the cursor, written out as {{NAME}} ("variable"),
or a ${ref reference before the cursor, written out as ${ref}
("template", the references TemplateSuggestions lists). A template
field also shows the whole list while it is empty, so the references
can be found without knowing to type ${; an entry with literal: true
there is a whole value (a header's typical value, a constant such as
true), offered while the
field holds no ${ and written out as it is.
type="textarea" renders a textarea in the input's place, for a value
that may run over lines (the hook reads its value and selectionStart
as an input's); there Enter with nothing highlighted is a new line and
the arrow keys move between lines until the list is open.
Without suggestions (nil or []) the hook is still attached and
shows nothing: LiveView starts a hook only on an element it adds, so an
input whose suggestions arrive later (a To picker switched to another
kind) would otherwise never get its dropdown. The wrapper is rendered
either way, so layout classes belong on wrapper_class, and the input
usually takes w-full.
Summary
Functions
A text input with a suggestion dropdown.
Functions
A text input with a suggestion dropdown.
suggestions is a list of strings, or the list already JSON-encoded
(for a caller that encodes once and shares it between many inputs). An
entry may also be a map %{value: ..., label: ..., description: ...}:
the label (default: the value) is shown, with the description on a line
under it. A search text is matched too, after the value: an entry
whose value is a generated name is found by the name people know it by
(a proxy user by its display name). In "template" mode an entry with literal: true is written
as its value, not as a ${value} reference.
The inner block renders inside the wrapper, after the input.
Examples
<.suggest_input
id="header-name"
name="header_name"
value={@name}
suggestions={@header_names}
phx-change="change"
class="w-full font-mono"
/>Attributes
id(:string) (required)value(:any) - Defaults tonil.type(:string) - the input's type, or "textarea" for a textarea in its place. Defaults to"text".suggestions(:any) - a list of strings or option maps, the same list as JSON, or nil/[] for none. Defaults tonil.mode(:string) - Defaults to"value". Must be one of"value","list","lines","variable", or"template".class(:any) - the input's classes. Defaults tonil.wrapper_class(:any) - classes besiderelativeon the wrapper. Defaults tonil.- Global attributes are accepted. Supports all globals plus:
["name", "placeholder", "disabled", "readonly", "required", "form", "spellcheck", "maxlength", "autofocus", "rows"].
Slots
inner_block