Aurora.Uix.Renderer behaviour (Aurora UIX v0.1.5)

Copy Markdown

Behaviour for predefined field renderers selectable by a single atom.

A renderer is one arity-1 render/1 function. It receives the LiveView assigns and decides what to draw by pattern-matching @auix.layout_type — one of :index, :show or :form. It defines a clause per layout type it supports; for a layout type where the field's default rendering is adequate it delegates explicitly (Aurora.Uix.Renderers.default/1); a layout type it neither handles nor delegates simply crashes — a misplaced renderer is a bug, surfaced loudly.

A field selects a renderer by atom name in any of its renderer slots:

field :active, renderer: :toggle_switch
field :stock, index_renderer: :progress_bar

The atom is resolved to its render/1 function through Aurora.Uix.Renderers, which applies a per-layout-type slot precedence (see that module). Built-in renderers are listed by Aurora.Uix.Renderers.BuiltIn; host applications add their own via a registrar module (see Aurora.Uix.RendererRegistrar).

Reading the field value

The value lives in a different assign per layout type; use the helpers:

@auix.layout_typeValue sourceHelper
:index@entity[@field.key]display_value/1
:show@auix.entity[@field.key]display_value/1
:form@auix.form[@field.key]form_field/1 (bind) / display_value/1 (value)

Writing a renderer

use Aurora.Uix.Renderer injects the behaviour, the Aurora UIX core components, gettext, and the value helpers. Define a render/1 clause per layout type you support:

defmodule MyApp.Renderers.Uppercase do
  use Aurora.Uix.Renderer

  @impl true
  def render(%{auix: %{layout_type: lt}} = assigns) when lt in [:index, :show] do
    assigns = assign(assigns, :value, display_value(assigns))

    ~H"""
    <span class="my-upper">{String.upcase(to_string(@value || ""))}</span>
    """
  end

  # Editing is not special for this renderer — fall back to the default input.
  @impl true
  def render(%{auix: %{layout_type: :form}} = assigns),
    do: Aurora.Uix.Renderers.default(assigns)
end

Summary

Callbacks

Renders the field for the current layout type (@auix.layout_type).

Functions

Reads the field's display value for the current layout type: @entity[key] in :index, @auix.entity[key] in :show, and @auix.form[key].value in :form.

Returns the Phoenix.HTML.FormField for the field in the :form layout, suitable for binding to <.input field={...} />.

Types

layout_type()

@type layout_type() :: :index | :show | :form

Callbacks

render(assigns)

@callback render(assigns :: map()) :: Phoenix.LiveView.Rendered.t()

Renders the field for the current layout type (@auix.layout_type).

Define one clause per layout type the renderer supports; delegate to Aurora.Uix.Renderers.default/1 where the default rendering is adequate.

Functions

display_value(assigns)

@spec display_value(map()) :: term()

Reads the field's display value for the current layout type: @entity[key] in :index, @auix.entity[key] in :show, and @auix.form[key].value in :form.

form_field(map)

@spec form_field(map()) :: Phoenix.HTML.FormField.t() | nil

Returns the Phoenix.HTML.FormField for the field in the :form layout, suitable for binding to <.input field={...} />.