Introducing a new way to review your PRs.

Read the announcement

petalframework / petal_components

Phoenix + Live View HEEX Components

View on GitHub

24 documents · Updated Sep 2, 2026

docs / text-and-structured-inputs

Text and structured inputs

Text and structured inputs let a user enter, review, and change scalar or list values in a form. They cover plain text fields, shared input surfaces, one-time-password entries, and searchable combo boxes while keeping visible states connected to native form behavior and LiveView updates. See Form controls for the wider form control model, Selection controls for checkboxes, radios, switches, and related choice controls, and Advanced form inputs and uploads for richer input and upload patterns.

Text input surfaces

Text input surfaces provide the common field treatment for single-line text, long text, search, contact values, passwords, dates, and other native input types. The plain input component supplies the control only. The field component adds the label, help text, and error region needed for a complete form field. lib/petal_components/input.ex#L5-L44 lib/petal_components/field.ex#L5-L16

Form values and wrappers

The input value comes from an explicit value or from a Phoenix.HTML.FormField. A form field supplies the control name, id, current value, and human-readable label, so the same control can render a standalone value or participate in a changeset-backed form. Textareas place their normalized value in their content. Select-like native controls use their option list to map the current value to the submitted choice. lib/petal_components/input.ex#L84-L115 lib/petal_components/field.ex#L163-L191 lib/petal_components/field.ex#L228-L248 lib/petal_components/field.ex#L335-L351

The field wrapper is the boundary for form feedback. It places the label before the control and renders translated errors and help text around it. Required fields use the label and native control state together, while unused form fields do not show validation errors until the form marks them as used. lib/petal_components/field.ex#L163-L176 lib/petal_components/field.ex#L599-L631

References: lib/…/input.ex• L84-115 lib/…/field.ex• L163-191 lib/…/field.ex• L228-248 lib/…/field.ex• L335-351

Visual states and field actions

Text inputs use one shared surface with a border, placeholder color, focus ring, and disabled treatment. An error wrapper changes the surrounding field to the danger state. Date and time controls show a muted indicator, while an action such as clearing, copying, or revealing a password uses a real button with its own focus target. assets/default.css• L1513-1516 assets/default.css• L1552-1649

The optional actions add small client behaviors without changing the submitted value model. A viewable password switches between hidden and visible text. A copyable text, URL, or email field stays read-only and reports a temporary copied state. A clearable text-like field shows its button only when it has a value, then clears the control and emits an input event so the form can see the change. lib/petal_components/input.ex#L144-L241 assets/…/petal_components.js• L285-359

References: lib/…/input.ex• L144-241 lib/…/field.ex• L592-730 assets/default.css• L1513-1619 assets/…/petal_components.js• L285-359

Input groups

An input group combines one input with nearby content on one field surface. The group owns the border, radius, and focus ring, so the child input can remain borderless and the whole surface reads as one control. lib/petal_components/input_group.ex#L4-L15 assets/default.css• L1691-1706

Shared surface and add-ons

Leading and trailing add-ons sit beside the control. They can show text, icons, keyboard hints, buttons, or native selects. The add-on gives context or an adjacent action; the input keeps the value and name that the form submits. lib/petal_components/input_group.ex#L35-L49 lib/petal_components/showcase/input_group.ex#L5-L80

The group also accepts full-width rows above and below the control. These rows support content such as a formatting toolbar or a character count without splitting the field into separate visual surfaces. lib/petal_components/input_group.ex#L41-L69 assets/default.css• L1758-1766

References: lib/…/input_group.ex• L35-69 lib/…/input_group.ex• L5-108 assets/default.css• L1691-1766

Focus and disabled states

Focus on any input inside the group rings the shared outer surface. If a child input, select, or textarea is disabled, the group fades and the child keeps the not-allowed treatment. When the group sits inside an error field wrapper, its border and ring use the error color. assets/default.css• L1693-1706 assets/default.css• L1764-1766

References: assets/default.css• L1693-1706 assets/default.css• L1764-1766 lib/…/input_group.ex• L5-27

One-time-password entries

The one-time-password input presents one value as a row of separate slots. The slots are visual only. One real text input receives the value, which preserves native paste, form submission, SMS or password-manager fill, and normal phx-change behavior. lib/petal_components/input_otp.ex#L4-L16

Single value and input rules

The control posts one field value under the supplied name. Its length defaults to six characters and limits the input. Numeric mode accepts digits and uses a numeric input mode. Alphanumeric mode accepts letters and digits and changes the input mode to text. A group size can add separators without creating extra values or fields. lib/petal_components/input_otp.ex#L26-L39 lib/petal_components/input_otp.ex#L55-L75 assets/…/petal_components.js• L1585-1631

References: lib/…/input_otp.ex• L26-39 lib/…/input_otp.ex• L55-75 assets/…/petal_components.js• L1585-1631

Painted states and completion event

The client hook copies each character into its visual slot and marks filled and active slots. The active empty slot shows the caret state. Disabled entries fade the slots, and an enclosing form-field error changes their borders to the danger state. When the value reaches the configured length, the component emits a bubbling petal:otp-complete event with the code in detail.value. assets/…/petal_components.js• L1585-1652 assets/default.css• L1767-1821

References: assets/…/petal_components.js• L1585-1652 assets/default.css• L1767-1821 lib/…/input_otp.ex• L21-29

Combo boxes

A combo box is a searchable select for lists that are easier to search than to scan. It separates the display surface from the form value: the visible control supports search and keyboard use, while a hidden native select remains the form source of truth. This keeps changesets, phx-change, constraint validation, and LiveView form recovery aligned with a plain select. lib/petal_components/combo_box.ex#L1-L25

Native values and option structure

The component accepts a scalar value for single choice and a list for multiple choice. Options can use the same label/value shapes as a native select. They can also carry disabled state, extra metadata for rich content, or a group label. The visible list and the hidden select use the same choices, so the label is for display and the value is what the form posts. lib/petal_components/combo_box.ex#L47-L55 lib/petal_components/combo_box.ex#L258-L326

Multiple mode posts a real multiple-select value. The field name gains [], each selected option becomes a removable chip, and the chosen order is preserved in the chip row. A max_items limit blocks further choices after the cap and announces that state. lib/petal_components/combo_box.ex#L31-L38 lib/petal_components/combo_box.ex#L93-L100 assets/…/petal_components.js• L5194-5223 assets/…/petal_components.js• L5287-5351

References: lib/…/combo_box.ex• L31-55 lib/…/combo_box.ex• L64-104 lib/…/combo_box.ex• L258-326 assets/…/petal_components.js• L5194-5223 assets/…/petal_components.js• L5287-5351

Search and selection flow

The combo box filters options in the browser and keeps their server-defined order. A query hides non-matches and ranks the best visible match for keyboard selection. Arrow keys move the virtual highlight, Enter chooses it, Escape closes the panel, and Tab leaves it. The chosen option is marked separately from the highlight, so the display always distinguishes current value from keyboard focus. lib/petal_components/combo_box.ex#L13-L25 assets/…/petal_components.js• L5075-5105 assets/…/petal_components.js• L5678-5752 assets/…/petal_components.js• L5777-5858

The component can accept typed values or offer a create row. In these modes, a committed value is added to the hidden select so it posts like another option. The server must render the value again if it needs to persist after a LiveView patch. A remote mode instead sends a debounced search event to a LiveView and renders the returned result rows. lib/petal_components/combo_box.ex#L106-L143 assets/…/petal_components.js• L5496-5566 assets/…/petal_components.js• L5601-5636

References: lib/…/combo_box.ex• L13-25 lib/…/combo_box.ex• L106-143 assets/…/petal_components.js• L5496-5566 assets/…/petal_components.js• L5601-5636 assets/…/petal_components.js• L5678-5858

Display variants and accessible states

The input variant keeps the search input in the field surface. The trigger variant uses a select-like button for the closed state and moves the search input into the opened panel. Single choice shows its label. Multiple choice shows a count in the trigger or chips in the input variant. Rich option, selected, and chip slots change presentation without changing filtering or submitted values. lib/petal_components/combo_box.ex#L79-L104 lib/petal_components/combo_box.ex#L212-L255 lib/petal_components/showcase/combo_box.ex#L75-L201

The visible input or trigger carries the accessible name and combobox state. The panel is a listbox with option rows. Disabled options stay visible but cannot be chosen. Required state stays on the native select for constraint checking and is also exposed on the visible control. The hook moves validation feedback to the visible control when the hidden select fails. lib/petal_components/combo_box.ex#L328-L360 lib/petal_components/combo_box.ex#L418-L576 assets/…/petal_components.js• L4824-4827 assets/…/petal_components.js• L5662-5675

References: lib/…/combo_box.ex• L79-104 lib/…/combo_box.ex• L328-360 lib/…/combo_box.ex• L418-576 assets/…/petal_components.js• L4824-4827 assets/…/petal_components.js• L5662-5675

Client-side integrations

Client-side hooks add behavior where a native control alone cannot provide the visual interaction. They do not replace the form value contract: text actions operate on the real input, OTP uses one real input, and the combo box synchronizes its display with its native select. The hooks are exported from the bundled JavaScript entry point. assets/…/petal_components.js• L285-359 assets/…/petal_components.js• L1585-1652 assets/…/petal_components.js• L4629-4947

Hook responsibilities

The main client contracts are:

ControlClient behaviorForm effect
Viewable passwordToggles password visibility and its accessible labelKeeps the same password value and name
Copyable inputCopies a read-only value and announces temporary successDoes not edit the value
Clearable inputShows a value-dependent clear action and emits an input event after clearingLets form listeners observe the empty value
OTP inputPaints slots, filters characters, and emits completionPosts one code field
Combo boxFilters, opens, selects, clears, and syncs the display with the native selectPreserves native select submission and LiveView events

References: assets/…/petal_components.js• L285-359 assets/…/petal_components.js• L1585-1652 assets/…/petal_components.js• L4629-4947 assets/…/petal_components.js• L5496-5636

LiveView synchronization

LiveView remains the source of server state after a control change. Combo box selection changes dispatch input and change events from the native select, while the hook re-reads that select after a patch or form reset. This lets the panel remain open during a multi-select update while the selected values still come from the server-rendered form state. assets/…/petal_components.js• L4909-4916 assets/…/petal_components.js• L4949-5007 assets/…/petal_components.js• L5267-5285 assets/…/petal_components.js• L5389-5494

References: lib/…/combo_box.ex• L1-11 assets/…/petal_components.js• L4909-4916 assets/…/petal_components.js• L4949-5007 assets/…/petal_components.js• L5267-5285 assets/…/petal_components.js• L5389-5494

Change checklist

When these files or sections are changed, remember to consider these:

  • Change the value or name rules in input.ex, field.ex, or combo_box.ex together. A multiple field must keep the [] name agreement across hidden inputs and the native select.
  • Change combo box option shapes or remote results together with the display labels, submitted values, disabled state, and metadata used by rich option and chip content.
  • Change combo box ids or variants together with field label targets. The label must point to the visible input or trigger rather than the roleless wrapper.
  • Change OTP length or pattern together with the real input limits, slot count, sanitization rule, input mode, and completion event condition.
  • Add or rename a hook-backed action only when its export and the application LiveSocket registration stay aligned with the rendered hook name.
  • Change error or disabled styles together with both the field wrapper state and the visual surface. Group, OTP, and combo box controls do not all carry the state on the same element.

Important failure modes

TriggerConsequenceGuard
The JavaScript bundle does not register a rendered input, OTP, or combo box hookThe server-rendered control remains, but its extra action or visual interaction does not workKeep the rendered phx-hook name and the LiveSocket hook export in sync
A combo box has no id, form field, or non-empty nameThe hook has no stable mount identity and rendering raises an argument errorSupply an explicit id, a Phoenix.HTML.FormField, or a non-empty name
A combo box visible query is treated as the submitted fieldSearch text can reach validation or form handlers before a choice existsRead the hidden native select as the value source; the visible input is display and query chrome
A multiple combo box uses a bare field name or omits the matching empty hidden valueSubmitted parameters can have a mixed shape or lose the empty stateKeep the [] name and the multiple-field hidden value in agreement
A free-text or remote combo value is not rendered again by the serverA later LiveView patch can drop the client-added optionPersist accepted values in the server option list when they must survive a patch
A required combo box fails while its hidden select stays inertNative validation blocks submission without placing useful feedback on the visible controlKeep the required state on the native select and keep the validation handoff hook active

Key files

FileWhy to read it
lib/petal_components/input.exDefines the plain input value contract and the password, copy, clear, date, textarea, and select surfaces
lib/petal_components/field.exShows how labels, errors, help text, form fields, and combo boxes compose into complete fields
lib/petal_components/input_group.exDefines the shared surface and the inline and block add-on model
lib/petal_components/input_otp.exDefines the one-value OTP contract, character modes, grouping, and completion event surface
lib/petal_components/combo_box.exDefines the searchable-select model, native form value, variants, option groups, and remote modes
assets/…/petal_components.jsContains the client behavior for text actions, OTP painting, and combo box synchronization
assets/default.cssShows the shared visual states for text inputs, groups, OTP slots, and combo box panels
lib/petal_components/showcase/combo_box.exGives product-level examples of multiple choice, trigger mode, creation, rich display, and grouped options
lib/petal_components/showcase/input_group.exShows how text, buttons, selects, and block rows compose on one surface