Corex.Tree (Corex v0.2.0)

View Source

Tree items for hierarchical/nested structures to be used with:

Use Corex.Tree.new/1 to build a list of items from a list of maps.

What the items attr accepts

Menu and TreeView accept Corex.Tree.Item structs only, and this is the whole contract. A plain map raises with the new/1 call that would have built it, unlike Corex.List, which coerces: a tree is authored in a template rather than mapped from a query result, so a wrong shape is a developer mistake to surface rather than a bad row to skip.

Each item requires :label. :value is generated when absent, :children nests further items, and :to, :redirect, :new_tab, :disabled, :group and :meta are optional. See Corex.Item for the fields shared with Corex.List.Item.

Summary

Functions

Creates a list of Tree.Item structs from a list of maps. Recursively processes :children if present.

Functions

new(items)

@spec new([map()]) :: [Corex.Tree.Item.t()]

Creates a list of Tree.Item structs from a list of maps. Recursively processes :children if present.

Fields

  • :label - (required) Display text
  • :value - (optional) Unique node value, auto-generated if not provided
  • :to - (optional) Destination (path or URL)
  • :children - (optional) Nested items (list of maps)
  • :disabled - (optional) Whether the item is disabled
  • :group - (optional) Group identifier
  • :meta - (optional) Additional metadata
  • :redirect - (optional) :href | :patch | :navigate | false

  • :new_tab - (optional) Open the destination in a new tab

Examples

Corex.Tree.new([
  %{label: "File", children: [
    %{label: "New"},
    %{label: "Open"}
  ]},
  %{label: "Edit"}
])

Raises ArgumentError if items is not a list of maps.