A2UICore.State.Component (A2UICore v0.1.1)

Copy Markdown View Source

One component instance on a surface: its id, catalog type, and properties.

The wire form is a flat object — {"id": "title", "component": "Text", "text": "Hello"}. A Component splits that into id, type and props (everything else), and tree/1 puts it back together.

A component optionally records the catalog_id it was created against, so a surface holding components from several catalogs can look up each one's schema and child-reference spec.

Child references are derived from a A2UICore.Catalog.RefSpec, which the catalog supplies for the component's type (see child_references/3).

iex> alias A2UICore.State.Component
iex> component = Component.from_wire(%{"id" => "c", "component" => "Card", "child" => "t"})
iex> {component.id, component.type, component.props}
{"c", "Card", %{"child" => "t"}}
iex> Component.tree(component)
%{"id" => "c", "component" => "Card", "child" => "t"}

Summary

Types

What a catalog knows about a component type: the child-reference spec and the composition constraints (nil or [] means unconstrained).

t()

Functions

The {child_id, property_path} pairs this component references.

Builds a component from a wire object (id, component, and the properties).

Builds a component from its parts.

The wire object: the properties plus id and component.

Types

info()

(since 0.1.0)
@type info() :: %{
  optional(:ref_spec) => A2UICore.Catalog.RefSpec.t() | nil,
  optional(:allowed_parents) => [String.t()] | nil,
  optional(:allowed_children) => [String.t()] | nil
}

What a catalog knows about a component type: the child-reference spec and the composition constraints (nil or [] means unconstrained).

t()

(since 0.1.0)
@type t() :: %A2UICore.State.Component{
  catalog_id: String.t() | nil,
  id: String.t(),
  props: map(),
  type: String.t()
}

Functions

child_references(component, spec, known_ids \\ nil)

(since 0.1.0)
@spec child_references(t(), A2UICore.Catalog.RefSpec.t() | nil, Enumerable.t() | nil) ::
  [
    A2UICore.Catalog.RefSpec.child_ref()
  ]

The {child_id, property_path} pairs this component references.

A catalog's spec is authoritative when it names child properties. Without one (an unknown type, or a schema with no ComponentId / ChildList references) the references are inferred from property names and shapes: child, children, *Child, values holding a componentId / child key, and — when known_ids is given — strings naming a component on the surface.

from_wire(wire, options \\ [])

(since 0.1.0)
@spec from_wire(map(), keyword()) :: t()

Builds a component from a wire object (id, component, and the properties).

new(id, type, props \\ %{}, options \\ [])

(since 0.1.0)
@spec new(String.t(), String.t(), map(), keyword()) :: t()

Builds a component from its parts.

tree(component)

(since 0.1.0)
@spec tree(t()) :: map()

The wire object: the properties plus id and component.