LiveFilter.Sort (LiveFilter v0.2.0)

Copy Markdown View Source

Sort state — a list of LiveFilter.Sort.Entry ordered by priority.

An orthogonal, URL-shareable concern (like LiveFilter.Pagination). The list is multi-column ready even though the bundled UI sets a single active sort. entries: [] means "no explicit sort" — the consumer's default ordering applies.

Example

{sort, remaining} = LiveFilter.sort_from_params(params, sortable_fields)
LiveFilter.Sort.to_params(sort)            # => %{"order" => "clicks.desc"}
LiveFilter.Sort.direction_for(sort, :clicks) # => :desc

Summary

Functions

Clears all sort entries.

Removes the entry for field (no-op if absent).

Direction of field in the current sort, or nil if it is not sorted.

Sets a single explicit sort entry, replacing any current sort.

Serializes to a PostgREST-compatible param map. Returns %{} (no order key) when the sort is empty, keeping default-state URLs clean.

Tri-state single-sort toggle for a header click.

Tri-state toggle that reads the field's default_direction/nulls from a LiveFilter.SortField list. A field not present in the list is treated as not sortable and the sort is returned unchanged (no-op).

Types

t()

@type t() :: %LiveFilter.Sort{entries: [LiveFilter.Sort.Entry.t()]}

Functions

clear(sort)

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

Clears all sort entries.

clear(sort, field)

@spec clear(t(), atom()) :: t()

Removes the entry for field (no-op if absent).

direction_for(sort, field)

@spec direction_for(t(), atom()) :: LiveFilter.Sort.Entry.direction() | nil

Direction of field in the current sort, or nil if it is not sorted.

put(sort, field, direction, opts \\ [])

@spec put(t(), atom(), LiveFilter.Sort.Entry.direction(), keyword()) :: t()

Sets a single explicit sort entry, replacing any current sort.

to_params(sort)

@spec to_params(t()) :: map()

Serializes to a PostgREST-compatible param map. Returns %{} (no order key) when the sort is empty, keeping default-state URLs clean.

toggle(sort, field)

@spec toggle(t(), atom()) :: t()

Tri-state single-sort toggle for a header click.

Cycle: none -> default_direction -> opposite -> none. Replaces any other active sort (single-sort UI).

Options

  • :default_direction - first-click direction (default :asc)
  • :nulls - nulls placement to carry onto the new entry (default nil)

toggle(sort, field, sortable_fields)

@spec toggle(t(), atom(), [LiveFilter.SortField.t()] | keyword()) :: t()

Tri-state toggle that reads the field's default_direction/nulls from a LiveFilter.SortField list. A field not present in the list is treated as not sortable and the sort is returned unchanged (no-op).