LedgerUI.Components.Segmented (LedgerUI v0.2.0)

Copy Markdown View Source

A segmented control: a row of toggle buttons where exactly one is chosen.

<.segmented id="range" value={@range} on_change="set-range" options={~w(day month year)} />
<.segmented field={@form[:view]} options={[{"List", "list"}, {"Board", "board"}]} />

<.segmented id="view" value="board" options={[
  %{label: "Board", value: "board", icon: "kanban"},
  %{label: "All", value: "all", count: 128}
]} />

Options are strings, {label, value} tuples or maps with :label, :value, :icon, :count and :disabled. It is a role="group" (see role) of buttons with aria-pressed. The choice is made in the browser (hook LedgerUI.Segmented, priv/static/ledger_ui/hooks/segmented.js): a new value from the server wins, a patch that does not change it leaves the person's choice alone, arrow keys/Home/End move the focus (one segment is in the Tab order).

With a name (or field) the control submits through a hidden input that fires input, so a form's phx-change runs. on_change is an event name (pushed with %{"id" => id, "value" => value} to target) or a %JS{}.

Summary

Functions

segmented(assigns)

Attributes

  • id (:string) - Defaults to nil.
  • name (:string) - Defaults to nil.
  • value (:any) - Defaults to nil.
  • field (Phoenix.HTML.FormField) - Defaults to nil.
  • options (:list) - Strings, {label, value} tuples or maps; or write <:option> slots. Defaults to [].
  • label (:string) - Defaults to nil.
  • help (:string) - Defaults to nil.
  • errors (:list) - Defaults to [].
  • size (:string) - Defaults to "md". Must be one of "sm", "md", or "lg".
  • shape (:string) - Defaults to "rounded". Must be one of "rounded", or "pill".
  • full (:boolean) - Defaults to false.
  • stretch (:boolean) - The canvas form of a full-width control: width:100% on the group, flex:1 per button. Defaults to false.
  • role (:string) - group (default) or what the surface needs. Defaults to "group".
  • disabled (:boolean) - Defaults to false.
  • on_change (:any) - Defaults to nil.
  • target (:any) - Defaults to nil.
  • wrapper_class (:any) - Defaults to nil.
  • class (:any) - Defaults to nil.
  • Global attributes are accepted. Supports all globals plus: ["form"].

Slots

  • option - One segment, its content the label (instead of options). value defaults to the position; selected chooses it when value is not given. Accepts attributes:
    • value (:any)
    • label (:string)
    • icon (:string)
    • count (:any)
    • disabled (:boolean)
    • selected (:boolean)