Money. Input. Components
(Money.Input v0.1.0)
View Source
HEEx components for locale-aware money input.
Two components ship from this module:
money_input/1— money input with a fixed currency or an embeddedcurrency_picker/1.currency_picker/1— first-class searchable currency picker (flag glyphs, recents inlocalStorage, mobile sheet variant, keyboard nav).
Both render their HTML baseline server-side and degrade
gracefully when JS is disabled. With the JS hook loaded
(priv/static/money_input.js), they upgrade to live
formatting and full picker behaviour.
For a plain number input (no currency), use
Localize.Inputs.Components.number_input/1
from the sibling localize_inputs package.
Setup
Add the JS hooks in your assets/js/app.js:
import { MoneyInput, CurrencyPicker } from "money_input"
let Hooks = {}
Hooks.MoneyInput = MoneyInput
Hooks.CurrencyPicker = CurrencyPickerAnd install the AutoNumeric peer dep:
npm install autonumericSee Money.Input for a full feature overview.
Summary
Functions
First-class searchable currency picker.
Renders as a trigger button (flag + ISO code). Clicking opens
an overlay with a search input, recent selections (persisted
in localStorage), pinned preferred currencies, and the full
sorted currency list. On mobile, the overlay becomes a
full-screen sheet.
Examples
<.currency_picker
form={@form}
field={:currency}
current={:USD}
preferred={[:USD, :EUR, :GBP, :JPY]}
/>Attributes
form(Phoenix.HTML.Form) - Defaults tonil.field(:atom) - Defaults tonil.name(:string) - Explicit hidden-input name. Overrides form+field. Used bymoney_input/1to inject a nested name likeprice[currency]. Defaults tonil.input_id(:string) - Explicit id for the hidden value input. Defaults tonil.current(:atom) (required)allowed(:list) - Defaults tonil.preferred(:list) - Defaults to[].recents_limit(:integer) - Defaults to5.locale(:any) - Defaults tonil.variant(:atom) - Defaults to:auto. Must be one of:auto,:dropdown, or:sheet.id(:string) - Defaults tonil.class(:string) - Defaults tonil.button_class(:string) - Defaults tonil.overlay_class(:string) - Defaults tonil.row_class(:string) - Defaults tonil.
Locale-aware money input.
Returns Money.t/0 on form submission. Renders the currency
symbol as an adornment outside the input — the user only
types digits and a decimal separator.
With currency_picker={true}, embeds the bundled
currency_picker/1 in the symbol-adornment position so the
user can switch currencies. Currency-aware precision: USD
accepts 2 decimals, JPY 0, BHD 3.
Attributes
:default_currency— the currency used when the form value doesn't carry one and the user hasn't (yet) picked one. With the picker off this is the only currency the field accepts. With the picker on it pre-selects the trigger and acts as the fallback when the form value is blank.
Examples
<.money_input form={@form} field={:price} default_currency={:USD} />
<.money_input
form={@form}
field={:price}
default_currency={:USD}
currency_picker={true}
preferred_currencies={[:USD, :EUR, :GBP]}
/>Attributes
form(Phoenix.HTML.Form) (required)field(:atom) (required)value(:any) - Defaults tonil.locale(:string) - Defaults tonil.default_currency(:atom) - The currency used when the form value doesn't carry one. Pre-selects the picker. Defaults tonil.currency_field(:atom) - Defaults tonil.min(:any) - Defaults tonil.max(:any) - Defaults tonil.align(:atom) - Defaults to:right. Must be one of:left,:right, or:center.placeholder(:string) - Defaults tonil.symbol_position(:atom) - Defaults to:auto. Must be one of:auto,:prefix, or:suffix.symbol_kind(:atom) - Defaults to:symbol. Must be one of:symbol,:iso,:narrow, or:none.js(:boolean) - Defaults totrue.class(:string) - Defaults tonil.input_class(:string) - Defaults tonil.symbol_class(:string) - Defaults tonil.currency_picker(:boolean) - Defaults tofalse.allowed_currencies(:list) - Defaults tonil.preferred_currencies(:list) - Defaults to[].- Global attributes are accepted. Supports all globals plus:
["disabled", "readonly", "required", "autofocus"].