LedgerUI.Components.PhoneInput (LedgerUI v0.2.0)

Copy Markdown View Source

A phone number field with a country picker: a button at the left with the ISO code and the calling code (BR +55), and the number input. The number is written in the national format of the country as it is typed ((11) 91234-5678); what the form submits is the E.164 number (+5511912345678).

<.phone_input field={@form[:phone]} label="Telefone" />
<.phone_input name="phone" country="US" label="Phone number" />
<.phone_input field={@form[:phone]} country="PT" country_order={~w(PT BR)} />
<.phone_input field={@form[:phone]} countries={~w(BR PT AR UY)} clearable />
<.phone_input name="phone" value="+5511912345678" size="lg" variant="filled" />

country is the country the field starts with ("BR" by default); a value in E.164 selects its own country. The picker is a searchable list (by name in Portuguese or English, ISO code or calling code); country_order lists the countries shown first, countries limits the list and except removes some. ISO codes are shown instead of flag emoji unless flags is set.

A hidden input with the field's name carries the E.164 number (empty while there are no digits) and fires a bubbling input event when it changes, so phx-change runs; the visible input has no name. phx-debounce and phx-throttle go to the hidden input.

Formatting is not validation

The format only helps reading. Validate on the server with LedgerUI.Components.PhoneInput.Format.e164/2:

case LedgerUI.Components.PhoneInput.Format.e164(params["phone"], "BR") do
  {:ok, e164} -> e164
  {:error, reason} -> reason
end

The behaviour lives in the LedgerUI.PhoneInput hook (phone_input.js), so the field needs a stable id (from field or name).

Summary

Functions

Renders the phone input.

Functions

phone_input(assigns)

Renders the phone input.

Attributes

  • id (:string) - Defaults to nil.
  • name (:string) - Defaults to nil.
  • value (:any) - Defaults to nil.
  • field (Phoenix.HTML.FormField) - Defaults to nil.
  • country (:string) - Defaults to "BR".
  • country_order (:list) - Defaults to [].
  • countries (:list) - Defaults to [].
  • except (:list) - Defaults to [].
  • locale (:string) - Defaults to "pt-BR".
  • flags (:boolean) - Defaults to false.
  • label (:string) - Defaults to nil.
  • label_class (:any) - Defaults to nil.
  • help (:string) - Defaults to nil.
  • errors (:list) - Defaults to [].
  • state (:string) - Defaults to nil.Must be one of nil, "", "success", or "error".
  • disabled (:boolean) - Defaults to false.
  • variant (:string) - Defaults to "outlined". Must be one of "outlined", or "filled".
  • size (:string) - Defaults to "md". Must be one of "sm", "md", or "lg".
  • shape (:string) - Defaults to "rounded". Must be one of "rounded", or "pill".
  • clearable (:boolean) - Defaults to false.
  • clear_label (:string) - Defaults to "Clear".
  • picker_label (:string) - Defaults to "Country".
  • search_placeholder (:string) - Defaults to "Search…".
  • no_results (:string) - Defaults to "No results found".
  • on_country_change (:string) - Defaults to nil.
  • target (:any) - Defaults to nil.
  • wrapper_class (:any) - Defaults to nil.
  • class (:any) - Defaults to nil.
  • style (:string) - Inline style of the .lu-input box (e.g. a width). Defaults to nil.
  • Global attributes are accepted. Supports all globals plus: ["autocomplete", "autofocus", "form", "placeholder", "readonly", "required"].