Corex.Item (Corex v0.2.0)

View Source

The shared contract behind Corex.Content.Item, Corex.List.Item, and Corex.Tree.Item.

The three structs stay separate because their required fields differ: content items carry :content, tree items carry :children, list items carry neither. Everything else is shared, and this module is where the shared parts live: the field set, the generated :value, and the :items assign validator.

Shared fields

All three carry :value, :label, :disabled, and :meta. Corex.List.Item and Corex.Tree.Item add the navigation set: :to, :redirect, :new_tab, and :group.

Validation

:items is authored in a template, not received from params, so a wrong shape is a developer mistake and assert_items!/3 raises rather than coercing. This is the opposite of Corex.Value, which coerces because its input comes from handle_event.

Summary

Functions

Asserts that a component's :items assign holds structs of module.

Generates a unique :value for an item that was built without one.

The fields carried by the item structs that support redirect-on-select.

The fields every item struct carries.

Functions

assert_items!(assigns, module, opts)

@spec assert_items!(map(), module(), keyword()) :: map()

Asserts that a component's :items assign holds structs of module.

Returns assigns unchanged so it can sit in an assign pipeline. Options:

  • :component (required) - name used in the error message
  • :required - when true, a nil :items raises instead of passing through
  • :example (required) - the Corex.*.new/1 snippet shown in the error

generate_value(prefix)

@spec generate_value(String.t()) :: String.t()

Generates a unique :value for an item that was built without one.

shared_fields()

@spec shared_fields() :: [atom()]

The fields every item struct carries.