Cldr.HTML.Unit (Cldr HTML v0.4.0) View Source

Implements Phoenix.HTML.Form.select/4 specifically for localised unit display.

Link to this section Summary

Functions

Generate an HTML select tag for a unit list that can be used with a Phoenix.HTML.Form.t.

Link to this section Types

Specs

select_options() :: [
  units: [atom() | binary(), ...],
  locale: Cldr.Locale.locale_name() | Cldr.LanguageTag.t(),
  mapper: function(),
  backend: module(),
  selected: atom() | binary()
]

Link to this section Functions

Link to this function

select(form, field, options \\ [])

View Source

Specs

select(
  form :: Phoenix.HTML.Form.t(),
  field :: Phoenix.HTML.Form.field(),
  select_options()
) ::
  Phoenix.HTML.safe()
  | {:error, {Cldr.UnknownUnitError, binary()}}
  | {:error, {Cldr.UnknownLocaleError, binary()}}

Generate an HTML select tag for a unit list that can be used with a Phoenix.HTML.Form.t.

Arguments

  • A t:Phoenix.HTML.Form form

  • A t:Phoenix.HTML.Form.field field

  • A t:Keyword list of options

Options

For select options see Phoenix.HTML.Form.select/4

  • :units is a list of units to be displayed in the select. See Cldr.Unit.known_units/0 and Cldr.Unit.known_units_for_category/1

  • :style is the style of unit name to be displayed and must be one of the styles returned by Cldr.Unit.known_styles/0. The current styles are :long, :short and :narrow. The default is style: :long.

  • :locale defines the locale to be used to localise the description of the units. The default is the locale returned by Cldr.get_locale/0

  • :backend is any backend module. The default is Cldr.default_backend!/0

  • :mapper is a function that creates the text to be displayed in the select tag for each unit. It is passed the unit name. The default function is &({Cldr.Unit.display_name(&1), &1})

  • :selected identifies the unit that is to be selected by default in the select tag. The default is nil. This is passed unmodified to Phoenix.HTML.Form.select/4

  • :prompt is a prompt displayed at the top of the select box. This is passed unmodified to Phoenix.HTML.Form.select/4

Examples

 => Cldr.HTML.Unit.select(:my_form, :unit, selected: :foot)
 => Cldr.HTML.Unit.select(:my_form, :unit,
     units: [:foot, :inch], mapper: &{Cldr.Unit.display_name(&1, &2), &1})