Search documentation of Plushie

# Plushie.Selection (Plushie v0.7.2)

Copy Markdown View Source

Selection state for lists and tables. Pure data structure supporting single, multi, and range selection modes.

## Modes

- `:single` - at most one item selected at a time.
- `:multi` - multiple items selectable; `extend: true` adds to the set.
- `:range` - like multi, but `range_select/2` selects a contiguous slice of the `order` list between the anchor and the target.

## Example

    sel = Plushie.Selection.new(mode: :multi, order: ["a", "b", "c", "d"])
    sel = Plushie.Selection.select(sel, "b")
    sel = Plushie.Selection.select(sel, "d", extend: true)
    Plushie.Selection.selected(sel)
    #=> MapSet.new(["b", "d"])

## Summary

### Types

t()

### Functions

clear(sel)

Clears all selected items and resets the anchor.

deselect(sel, id)

Removes `id` from the selection.

new(opts \\ [])

Creates a new selection state.

range_select(sel, id)

Selects all items in `order` between the current anchor and `id` (inclusive). If there is no anchor, selects only `id`.

select(sel, id, opts \\ [])

Selects `id`. In `:single` mode, replaces the selection. In `:multi` and `:range` modes, replaces unless `extend: true` is passed, in which case `id` is added to the existing selection.

select_all(sel)

Selects all items in `order`.

selected(selection)

Returns the `MapSet` of currently selected item IDs.

selected?(selection, id)

Returns `true` if `id` is currently selected.

to_list(selection)

Returns the selected IDs as a list (for passing to table `selected:` prop).

toggle(sel, id)

Toggles `id` in the selection. If already selected, removes it; otherwise adds it. In `:single` mode, toggling a selected item clears the selection entirely.

## Types

# t()

    @type t() :: %Plushie.Selection{
      anchor: term() | nil,
      mode: :single | :multi | :range,
      order: [term()],
      selected: MapSet.t()
    }

## Functions

# clear(sel)

    @spec clear(sel :: t()) :: t()

Clears all selected items and resets the anchor.

# deselect(sel, id)

    @spec deselect(sel :: t(), id :: term()) :: t()

Removes `id` from the selection.

# new(opts \\ [])

    @spec new(opts :: keyword()) :: t()

Creates a new selection state.

## Options

- `:mode` - selection mode: `:single` (default), `:multi`, or `:range`.
- `:order` - ordered list of item IDs for range selection.

# range_select(sel, id)

    @spec range_select(sel :: t(), id :: term()) :: t()

Selects all items in `order` between the current anchor and `id` (inclusive). If there is no anchor, selects only `id`.

Requires `order` to have been set at creation time via `new/1`.

# select(sel, id, opts \\ [])

    @spec select(sel :: t(), id :: term(), opts :: keyword()) :: t()

Selects `id`. In `:single` mode, replaces the selection. In `:multi` and `:range` modes, replaces unless `extend: true` is passed, in which case `id` is added to the existing selection.

Sets the anchor to `id` for subsequent range selections.

# select_all(sel)

    @spec select_all(sel :: t()) :: t()

Selects all items in `order`.

Requires `order` to have been set at creation time via `new/1`.

# selected(selection)

    @spec selected(sel :: t()) :: MapSet.t()

Returns the `MapSet` of currently selected item IDs.

# selected?(selection, id)

    @spec selected?(sel :: t(), id :: term()) :: boolean()

Returns `true` if `id` is currently selected.

# to_list(selection)

    @spec to_list(sel :: t()) :: [term()]

Returns the selected IDs as a list (for passing to table `selected:` prop).

# toggle(sel, id)

    @spec toggle(sel :: t(), id :: term()) :: t()

Toggles `id` in the selection. If already selected, removes it; otherwise adds it. In `:single` mode, toggling a selected item clears the selection entirely.