Erabi.Live (Erabi v0.1.0)

Copy Markdown View Source

The server side of the pickers in a LiveView: the searches of the pickers with search, and the events that change a picker from the server.

defmodule MyAppWeb.TaskLive do
  use MyAppWeb, :live_view

  on_mount Erabi.Live
  ...
end

(or on_mount Erabi.Live in a live_session). The LiveView then answers the searches of its pickers itself: the app writes no event clauses.

A picker's search is a function of the query:

<.combobox field={@form[:city_id]} search={&Places.cities/1} selected={@city} />

def cities(query), do: Repo.all(from c in City, where: ilike(c.name, ^"%#{query}%"), limit: 50)

It gives options (anything a picker takes, see Erabi.Option) for the query, in the order to show them; an empty query is "as the list opens" (recent or suggested options). The picker shows the first 50, and the next 50 as the list scrolls.

A function of two arguments gets the page too, %{offset: n, limit: 50}, and gives {options, more?}: for a search that pages in the database.

The search runs in the LiveView's process, so a slow one holds it: with async, it runs in a task (start_async) and its answer comes when it is ready. A query that changed meanwhile drops the older answer.

To rank options as the picker does in the browser, use Erabi.Match.rank/2.

Summary

Functions

Closes a picker's list.

Moves the focus to a picker.

Opens a picker's list.

Forgets the cached answers of a picker's search: its options on the server changed.

Sets the value of a picker from the server (as text, as the picker posts it), without a phx-change: for a value the server changed while the picker is on the page.

Functions

close(socket, id)

Closes a picker's list.

focus(socket, id)

Moves the focus to a picker.

open(socket, id)

Opens a picker's list.

refresh(socket, id)

Forgets the cached answers of a picker's search: its options on the server changed.

set(socket, id, value)

Sets the value of a picker from the server (as text, as the picker posts it), without a phx-change: for a value the server changed while the picker is on the page.