defmodule PlausibleWeb.Live.Components.ComboBox do @moduledoc """ Phoenix LiveComponent for a combobox UI element with search and selection functionality. The component allows users to select an option from a list of options, which can be searched by typing in the input field. The component renders an input field with a dropdown anchor and a hidden input field for submitting the selected value. In order to remain functional, the component must be embedded in a `
`. The number of options displayed in the dropdown is limited to 15 by default but can be customized. When a user types into the input field, the component searches the available options and provides suggestions based on the input. Any function can be supplied via `suggest_fun` attribute - see the provided `ComboBox.StaticSearch`. In most cases the `suggest_fun` runs an operation that could be deferred, so by default, the `async={true}` attr calls it in a background Task and updates the suggestions asynchronously. This way, you can render the component without having to wait for suggestions to load. If you explicitly need to make the operation synchronous, you may pass `async={false}` option. If your initial `options` are not provided up-front at initial render, lack of `options` attr value combined with `async=true` calls the `suggest_fun.("", [])` asynchronously - that special clause can be used to provide the initial set of suggestions updated right after the initial render. To simplify integration testing, suggestions load up synchronously during tests. This lets you skip waiting for suggestions messages to arrive. The asynchronous behaviour alone is already tested in ComboBox own test suite, so there is no need for additional verification. """ use PlausibleWeb, :live_component @default_suggestions_limit 15 def update(assigns, socket) do socket = socket |> assign(assigns) |> select_default() socket = if connected?(socket) do socket |> assign_options() else socket end |> assign_suggestions(assigns) {:ok, socket} end attr(:placeholder, :string, default: "Select option or search by typing") attr(:id, :any, required: true) attr(:options, :list, default: []) attr(:submit_name, :string, required: true) attr(:display_value, :string, default: "") attr(:submit_value, :string, default: "") attr(:selected, :any) attr(:suggest_fun, :any, required: true) attr(:suggestions_limit, :integer) attr(:class, :string, default: "") attr(:input_class, :string, default: "") attr(:dropdown_class, :string, default: "") attr(:required, :boolean, default: false) attr(:creatable, :boolean, default: false) attr(:errors, :list, default: []) attr(:async, :boolean, default: Mix.env() != :test) attr(:on_selection_made, :any) def render(assigns) do ~H"""
<.spinner class="spinner hidden absolute inset-y-3 right-8" /> <.spinner id={"selection-in-progress-#{@id}"} phx-update="ignore" x-show="selectionInProgress" class="spinner absolute inset-y-3 right-8" /> <.dropdown_anchor id={@id} />
<.combo_dropdown ref={@id} suggest_fun={@suggest_fun} suggestions={@suggestions} target={@myself} creatable={@creatable} display_value={@display_value} dropdown_class={@dropdown_class} />
""" end attr(:id, :any, required: true) def dropdown_anchor(assigns) do ~H"""
""" end attr(:ref, :string, required: true) attr(:suggestions, :list, default: []) attr(:suggest_fun, :any, required: true) attr(:target, :any) attr(:creatable, :boolean, required: true) attr(:display_value, :string, required: true) attr(:dropdown_class, :string, default: "") def combo_dropdown(assigns) do ~H""" """ end attr(:display_value, :string, required: true) attr(:submit_value, :string, required: true) attr(:ref, :string, required: true) attr(:target, :any) attr(:idx, :integer, required: true) attr(:creatable, :boolean, default: false) def option(assigns) do assigns = assign(assigns, :suggestions_limit, suggestions_limit(assigns)) ~H"""
  • <%= if @creatable do %> Create "{@display_value}" <% else %> {@display_value} <% end %>
  • Max results reached, type to refine search.
  • """ end def select_option(js \\ %JS{}, id, submit_value, display_value) do js |> JS.dispatch("phx:notify-selection-change", detail: %{ id: id, value: %{"submitValue" => submit_value, "displayValue" => display_value} } ) |> JS.push("select-option", value: %{"submit-value" => submit_value, "display-value" => display_value} ) end def handle_event( "select-option", %{"submit-value" => submit_value, "display-value" => display_value}, socket ) do socket = do_select(socket, submit_value, display_value) {:noreply, socket} end def handle_event( "search", %{"_target" => [target]} = params, %{assigns: %{options: options}} = socket ) do input = params[target] input_len = input |> String.trim() |> String.length() socket = if socket.assigns[:creatable] do assign(socket, display_value: input, submit_value: input) else socket end suggestions = if input_len > 0 do run_suggest_fun(input, options, socket.assigns, :suggestions) else options end |> Enum.take(suggestions_limit(socket.assigns)) {:noreply, assign(socket, %{suggestions: suggestions, searching?: input_len > 0})} end defp do_select(socket, submit_value, display_value) do id = socket.assigns.id socket = socket |> push_event("update-value", %{id: id, value: display_value, fire: false}) |> push_event("update-value", %{id: "submit-#{id}", value: submit_value, fire: true}) |> assign(:display_value, display_value) |> assign(:submit_value, submit_value) if socket.assigns[:on_selection_made] do socket.assigns.on_selection_made.(submit_value, id) end socket end defp suggestions_limit(assigns) do Map.get(assigns, :suggestions_limit, @default_suggestions_limit) end defp display_creatable_option?(assigns) do empty_input? = String.length(assigns.display_value) == 0 input_matches_suggestion? = Enum.any?(assigns.suggestions, fn {suggestion, _} -> assigns.display_value == suggestion end) assigns.creatable && not empty_input? && not input_matches_suggestion? end defp assign_options(socket) do assign_new(socket, :options, fn -> run_suggest_fun("", [], socket.assigns, :options) end) end defp assign_suggestions(socket, %{suggestions: _}), do: socket # A background suggest_fun task delivered a result for a different key # (namely, the initial options prefetch) - since the user has already # searched, this stale result must not clobber the up-to-date suggestions. defp assign_suggestions(%{assigns: %{searching?: true}} = socket, %{async_result?: true}), do: socket defp assign_suggestions(socket, _assigns), do: fill_suggestions_from_options(socket) defp fill_suggestions_from_options(socket) do suggestions = socket.assigns |> Map.get(:options, []) |> Enum.take(suggestions_limit(socket.assigns)) assign(socket, suggestions: suggestions) end defp select_default(socket) do case {socket.assigns[:selected], socket.assigns[:submit_value]} do {{submit_value, display_value}, nil} -> assign(socket, submit_value: submit_value, display_value: display_value) {submit_and_display_value, nil} when is_binary(submit_and_display_value) -> assign(socket, submit_value: submit_and_display_value, display_value: submit_and_display_value ) _ -> socket end end defp run_suggest_fun(input, options, %{id: id, suggest_fun: fun} = assigns, key_to_update) do if assigns[:async] do pid = self() Task.start(fn -> result = fun.(input, options) send_update( pid, __MODULE__, Keyword.new([ {:id, id}, {key_to_update, result}, {:async_result?, true} ]) ) end) # This prevents flashing the suggestions container # before the update is received on a subsequent render assigns[key_to_update] || [] else fun.(input, options) end end end