MishkaGervaz.Table.Web.Events.SelectionHandler behaviour (MishkaGervaz v0.0.1-alpha.3)

Copy Markdown View Source

Handles selection operations for Events module.

This module provides functions for managing row selection state, including individual selection, select-all, and clear selection.

Customization

You can create a custom SelectionHandler:

defmodule MyApp.CustomSelectionHandler do
  use MishkaGervaz.Table.Web.Events.SelectionHandler

  # Custom toggle that limits selection to 10 items
  def toggle_select(state, id) do
    if MapSet.size(state.selected_ids) >= 10 and
       not MapSet.member?(state.selected_ids, id) do
      {:error, :max_selection_reached}
    else
      super(state, id)
    end
  end
end

Then configure it in your resource's DSL:

mishka_gervaz do
  table do
    events do
      selection MyApp.CustomSelectionHandler
    end
  end
end

See MishkaGervaz.Table.Web.Events, MishkaGervaz.Table.Web.State, and the sibling handlers SanitizationHandler, RecordHandler, BulkActionHandler, HookRunner, RelationFilterHandler.

Summary

Callbacks

Clears all selection state.

Returns the effective selected IDs for bulk actions.

Toggles selection for a single row.

Toggles select-all state.

Types

state()

@type state() :: MishkaGervaz.Table.Web.State.t()

Callbacks

clear_selection(state)

@callback clear_selection(state :: state()) :: state()

Clears all selection state.

Resets select_all?, selected_ids, and excluded_ids. Returns the updated state.

get_selected_ids(state)

@callback get_selected_ids(state :: state()) :: list() | :all | {:all_except, list()}

Returns the effective selected IDs for bulk actions.

Returns one of:

  • list() - List of selected IDs
  • :all - All items selected
  • {:all_except, list()} - All items except the excluded ones

toggle_select(state, id)

@callback toggle_select(state :: state(), id :: String.t()) :: state()

Toggles selection for a single row.

When select_all? is true, toggles the ID in excluded_ids. When select_all? is false, toggles the ID in selected_ids.

Returns the updated state.

toggle_select_all(state)

@callback toggle_select_all(state :: state()) :: state()

Toggles select-all state.

Resets both selected_ids and excluded_ids when toggling. Returns the updated state.