Aurora.Uix.Selection (Aurora UIX v0.1.4-rc.0)

Copy Markdown

Manages selection state for paginated data structures with per-page tracking.

This module provides a data structure and functions to handle item selections across multiple pages, maintaining both global selection state and per-page selection tracking for UI components.

Key Features

  • Global selection tracking across all pages.
  • Per-page selection state for efficient UI updates
  • Automatic state derivation for selection counts and page indicators

Summary

Functions

Creates a new empty selection struct.

Adds selection state to an item map for UI rendering.

Sets the selection state for a specific item on a given page.

Updates derived state fields based on current selections.

Types

t()

@type t() :: %Aurora.Uix.Selection{
  selected: MapSet.t(),
  selected_any_in_page?: boolean(),
  selected_count: integer(),
  selected_in_page: map(),
  toggle_all_mode: atom()
}

Functions

new()

@spec new() :: t()

Creates a new empty selection struct.

Returns

t() - A new selection struct with empty selection state.

set_item_select_state(item, item_id, selection)

@spec set_item_select_state(map(), term(), t()) :: map()

Adds selection state to an item map for UI rendering.

Takes an item map and adds a :selected_check__ field indicating whether the item is currently selected based on the global selection state.

Parameters

  • item (map()) - The item map to enhance with selection state
  • item_id (term()) - The unique identifier to check for selection
  • selection (t()) - The selection struct containing current selections

Returns

map() - The item map with added :selected_check__ boolean field.

Examples

iex> selection = %Aurora.Uix.Selection{selected: MapSet.new([1, 2])}
iex> item = %{id: 1, name: "Item 1"}
iex> Aurora.Uix.Selection.set_item_select_state(item, 1, selection)
%{id: 1, name: "Item 1", selected_check__: true}

set_selected(item_id, selection, arg3, page)

@spec set_selected(term(), t(), boolean(), integer()) :: t()

Sets the selection state for a specific item on a given page.

Adds or removes an item from both global and per-page selection tracking. When state is true, the item is added to selections; otherwise it's removed.

Parameters

  • item_id (term()) - The unique identifier for the item to select/deselect
  • selection (t()) - The current selection struct
  • state (boolean()) - true to select the item, false to deselect
  • page (integer()) - The page number where this selection occurs (page is 1 when rendering infinite scroll)

Returns

t() - Updated selection struct with the new selection state.

update_states(selection, page \\ 1)

@spec update_states(t(), integer()) :: t()

Updates derived state fields based on current selections.

Recalculates selected_count and selected_any_in_page? based on the current selection state.

Parameters

  • selection (t()) - The selection struct to update
  • page (integer()) - The page number to check for any selections (defaults to 1 - infinite scroll case)

Returns

t() - Updated selection struct with recalculated state fields.