# `LedgerUI.Components.ViewSwitcher`
[🔗](https://github.com/devaction-labs/ledger_ui/blob/v0.2.0/lib/ledger_ui/components/view_switcher.ex#L1)

The views of a collection (List, Board, Table...) as a segmented control (`lu-seg`), with an
optional "+ View" menu to add one and a menu of saved views.

    <.view_switcher
      id="invoice-views"
      value={@view}
      on_change="set_view"
      on_add="add_view"
      views={[%{label: "List", value: "list", icon: "list"}, %{label: "Board", value: "board", icon: "kanban"}]}
      options={[%{label: "Table", value: "table", icon: "table", description: "Table view"}]}
    />

`views` are maps with `:label`, `:value` and optionally `:icon`, `:count`, `:disabled`,
`:patch`/`:navigate`/`:href` (a link, `aria-current="page"` when chosen); plain strings
stand for `%{label: s, value: s}`. A tab sends `on_change` with `%{"view" => value}`.

`options` are the views the menu offers (`:label`, `:value`, `:icon`, `:description`, `:added`,
`:disabled`); choosing one sends `on_add` with `%{"view" => value}`. The menu is a
`dropdown/1` with id `"#{id}-add"`. `saved` (same shape) adds a menu of saved views
(`on_saved`, `saved_value`, `saved_label`).

Without `options` and `saved` the root is the `lu-seg` itself; otherwise a `lu-row` wraps it.
Link views carry `aria-current="page"` when chosen, button views `aria-pressed`; the menu
triggers carry `aria-haspopup`/`aria-expanded`/`aria-controls` (rendered by `dropdown/1`).
`data-lu-single` is set on the control when a `value` is chosen.

# `view_switcher`

## Attributes

* `id` (`:string`) (required)
* `value` (`:any`) - Defaults to `nil`.
* `current` (`:any`) - Alias of `value`. Defaults to `nil`.
* `views` (`:list`) - Defaults to `[]`.
* `options` (`:list`) - Defaults to `[]`.
* `saved` (`:list`) - Defaults to `[]`.
* `saved_value` (`:any`) - Defaults to `nil`.
* `saved_label` (`:string`) - Defaults to `"Saved views"`.
* `on_change` (`:any`) - Defaults to `nil`.
* `on_add` (`:any`) - Defaults to `nil`.
* `on_saved` (`:any`) - Defaults to `nil`.
* `target` (`:any`) - Defaults to `nil`.
* `add_label` (`:string`) - Defaults to `"View"`.
* `menu_title` (`:string`) - Defaults to `"Add a new view"`.
* `columns` (`:integer`) - Defaults to `2`. Must be one of `1`, or `2`.
* `placement` (`:string`) - Defaults to `"bottom-start"`.
* `class` (`:any`) - Defaults to `nil`.
* Global attributes are accepted.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
