JediHelpers.FormHelpers (jedi_helpers v0.3.0)

Copy Markdown

Helper functions for form inputs and options.

Summary

Functions

Generates {label, value} tuples for a list of maps or structs to be used in dropdowns.

Generates option tuples using configurable label and value selectors.

Functions

options_for(list, label)

@spec options_for([map()], atom() | (map() -> term())) :: [{term(), term()}]

Generates {label, value} tuples for a list of maps or structs to be used in dropdowns.

Labels and values may be field names or unary functions. The value defaults to the :id field.

Examples

iex> users = [%{id: 1, name: "Alice"}, %{id: 2, name: "Bob"}]
iex> JediHelpers.FormHelpers.options_for(users, :name)
[{"Alice", 1}, {"Bob", 2}]

iex> JediHelpers.FormHelpers.options_for(users, &String.upcase(&1.name), :id)
[{"ALICE", 1}, {"BOB", 2}]

Raises if an element is not a map or struct, or if the arguments are invalid.

options_for(list, label, value)

@spec options_for([map()], atom() | (map() -> term()), atom() | (map() -> term())) ::
  [{term(), term()}]

Generates option tuples using configurable label and value selectors.

Each selector can be an atom naming a map field or a unary function.