LvStates.WithFilter (lv-states v0.1.1) View Source
LvStates.WithFilter stores any number of multi-value or single-value state keys that you choose
inside the :lvs_filters namespace.
It is useful when we want to filter our data based on a fixed set of values/categories.
Using the LvStates.WithFilter macro will have two consequences for our LiveView.Socket:
It populates
:lvs_filterswith a map for each of your filterable keys.A few event handles are generated to manipulate the state of your filters.
# In case of :multiple, it adds the value to the specified filter. In case of :single filter, it overwrites the current value
def handle_event("lvs-filter-set", %{"field" => field, "value" => value}, %Socket{} = socket) do # Removes the value of a particular filter from the state
def handle_event("lvs-filter-remove", %{"field" => field, "value" => value}, %Socket{} = socket) do # Clears all values from a particular filter, both for :single and :multiple filters.
def handle_event("lvs-filter-clear", %{"field" => field}, %Socket{} = socket) do # Clears all values from all filters under the :lvs_filters namespace
def handle_event("lvs-filter-clear-all", _, %Socket{} = socket) doAnd lastly an indirect consequence is that LvStates.WithSearch assumes that our LiveView implements the following method:
def fetch(%LiveView.Socket{})Note: if the assumed fetch/1 function is not implemented the functionality will not work as expected.
Examples
defmodule CarInventory do
use LvStates.WithSearch, [:model]
end