View Source Cldr.HTML.Unit (Cldr HTML v1.6.0)

Implements an HTML Form select specifically localised unit display.

Summary

Functions

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

Generate a list of options for a unit list that can be used with Phoenix.HTML.Form.options_for_select/2 or to create a <datalist>.

Types

@type select_options() :: [
  units: [atom() | binary(), ...],
  locale: Cldr.Locale.locale_name() | Cldr.LanguageTag.t(),
  collator: function(),
  mapper: (Cldr.Unit.t() -> String.t()),
  backend: module(),
  selected: atom() | binary()
]

Functions

Link to this function

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

View Source
@spec 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/0.

Arguments

Options

For select options see Phoenix.HTML.Form.options_for_select/2

  • :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

  • :collator is a function used to sort the units in the selection list. It is passed a list of tuples where each tuple is in the form {unit_display_name, unit}. The default collator sorts by name_1 < name_2. As a result, default collation sorts by code point which will not return expected results for scripts other than Latin.

  • :mapper is a function that creates the text to be displayed in the select tag for each unit. It is passed the a tuple of the form {unit_display_name, unit}, The default is the identity function &(&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 the form generator.

  • :prompt is a prompt displayed at the top of the select box. This is passed unmodified to the form generator.

Examples

 => Cldr.HTML.Unit.select(:my_form, :unit, selected: :foot)
 => Cldr.HTML.Unit.select(:my_form, :unit, units: [:foot, :inch])
Link to this function

unit_options(options \\ [])

View Source
@spec unit_options(select_options()) ::
  [tuple()]
  | {:error, {Cldr.UnknownUnitError, binary()}}
  | {:error, {Cldr.UnknownLocaleError, binary()}}

Generate a list of options for a unit list that can be used with Phoenix.HTML.Form.options_for_select/2 or to create a <datalist>.

Arguments

Options

See Cldr.HTML.Unit.select/3 for options.