From d947dad4c0fa57643baa7e71c3fb3a251210ee5a Mon Sep 17 00:00:00 2001 From: Adam Rutkowski Date: Thu, 24 Aug 2023 15:21:18 +0200 Subject: [PATCH] Improve ComboBox component - make sure the list options are always of the parent input width - allow passing a suggestion function instead of a module --- .../live/components/combo_box.ex | 33 ++++++++++--------- .../components/combo_box/static_search.ex | 26 +++++++++++---- .../live/components/combo_box_test.exs | 6 ++-- 3 files changed, 40 insertions(+), 25 deletions(-) diff --git a/lib/plausible_web/live/components/combo_box.ex b/lib/plausible_web/live/components/combo_box.ex index 136f4a35d..2b4afdf3e 100644 --- a/lib/plausible_web/live/components/combo_box.ex +++ b/lib/plausible_web/live/components/combo_box.ex @@ -14,8 +14,8 @@ defmodule PlausibleWeb.Live.Components.ComboBox do field, the component searches the available options and provides suggestions based on the input. - Any module exposing suggest/2 function can be supplied via `suggest_mod` - attribute - see the provided `ComboBox.StaticSearch`. + Any function can be supplied via `suggest_fun` attribute + - see the provided `ComboBox.StaticSearch`. """ use Phoenix.LiveComponent alias Phoenix.LiveView.JS @@ -46,11 +46,12 @@ defmodule PlausibleWeb.Live.Components.ComboBox do attr(:submit_name, :string, required: true) attr(:display_value, :string, default: "") attr(:submit_value, :string, default: "") - attr(:suggest_mod, :atom, required: true) + attr(:suggest_fun, :any, required: true) attr(:suggestions_limit, :integer) attr(:class, :string, default: "") attr(:required, :boolean, default: false) attr(:creatable, :boolean, default: false) + attr(:errors, :list, default: []) def render(assigns) do ~H""" @@ -94,16 +95,16 @@ defmodule PlausibleWeb.Live.Components.ComboBox do id={"submit-#{@id}"} /> - - <.dropdown - ref={@id} - suggest_mod={@suggest_mod} - suggestions={@suggestions} - target={@myself} - creatable={@creatable} - display_value={@display_value} - /> + <.dropdown + ref={@id} + suggest_fun={@suggest_fun} + suggestions={@suggestions} + target={@myself} + creatable={@creatable} + display_value={@display_value} + /> + """ end @@ -133,7 +134,7 @@ defmodule PlausibleWeb.Live.Components.ComboBox do attr(:ref, :string, required: true) attr(:suggestions, :list, default: []) - attr(:suggest_mod, :atom, required: true) + attr(:suggest_fun, :any, required: true) attr(:target, :any) attr(:creatable, :boolean, required: true) attr(:display_value, :string, required: true) @@ -145,7 +146,7 @@ defmodule PlausibleWeb.Live.Components.ComboBox do id={"dropdown-#{@ref}"} x-show="isOpen" x-ref="suggestions" - class="max-w-xs md:max-w-md lg:max-w-lg dropdown z-50 absolute mt-1 max-h-60 overflow-auto rounded-md bg-white py-1 text-base shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none sm:text-sm dark:bg-gray-900" + class="w-full dropdown z-50 absolute mt-1 max-h-60 overflow-auto rounded-md bg-white py-1 text-base shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none sm:text-sm dark:bg-gray-900" > <.option :for={ @@ -239,7 +240,7 @@ defmodule PlausibleWeb.Live.Components.ComboBox do def handle_event( "search", %{"_target" => [target]} = params, - %{assigns: %{suggest_mod: suggest_mod, options: options}} = socket + %{assigns: %{suggest_fun: suggest_fun, options: options}} = socket ) do input = params[target] input_len = input |> String.trim() |> String.length() @@ -253,7 +254,7 @@ defmodule PlausibleWeb.Live.Components.ComboBox do suggestions = if input_len > 0 do - suggest_mod.suggest(input, options) + suggest_fun.(input, options) else options end diff --git a/lib/plausible_web/live/components/combo_box/static_search.ex b/lib/plausible_web/live/components/combo_box/static_search.ex index 4db7860bf..8ec4c7559 100644 --- a/lib/plausible_web/live/components/combo_box/static_search.ex +++ b/lib/plausible_web/live/components/combo_box/static_search.ex @@ -10,12 +10,26 @@ defmodule PlausibleWeb.Live.Components.ComboBox.StaticSearch do """ @spec suggest(String.t(), [{any(), any()}]) :: [{any(), any()}] - def suggest(input, options) do - options - |> Enum.map(fn {_, value} = option -> {option, weight(value, input)} end) - |> Enum.reject(fn {_option, weight} -> weight < 0.6 end) - |> Enum.sort_by(fn {_option, weight} -> weight end, :desc) - |> Enum.map(fn {option, _weight} -> option end) + def suggest(input, choices, opts \\ []) do + input = String.trim(input) + + if input != "" do + weight_threshold = Keyword.get(opts, :weight_threshold, 0.6) + + choices + |> Enum.map(fn + {_, value} = choice -> + {choice, weight(value, input)} + + value -> + {value, weight(value, input)} + end) + |> Enum.reject(fn {_choice, weight} -> weight < weight_threshold end) + |> Enum.sort_by(fn {_choice, weight} -> weight end, :desc) + |> Enum.map(fn {choice, _weight} -> choice end) + else + choices + end end defp weight(value, input) do diff --git a/test/plausible_web/live/components/combo_box_test.exs b/test/plausible_web/live/components/combo_box_test.exs index 70ff9ca46..cf55e548c 100644 --- a/test/plausible_web/live/components/combo_box_test.exs +++ b/test/plausible_web/live/components/combo_box_test.exs @@ -154,7 +154,7 @@ defmodule PlausibleWeb.Live.Components.ComboBoxTest do <.live_component submit_name="some_submit_name" module={PlausibleWeb.Live.Components.ComboBox} - suggest_mod={__MODULE__.SampleSuggest} + suggest_fun={&SampleSuggest.suggest/2} id="test-component" options={for i <- 1..20, do: {i, "Option #{i}"}} suggestions_limit={7} @@ -210,7 +210,7 @@ defmodule PlausibleWeb.Live.Components.ComboBoxTest do <.live_component submit_name="some_submit_name" module={PlausibleWeb.Live.Components.ComboBox} - suggest_mod={ComboBox.StaticSearch} + suggest_fun={&ComboBox.StaticSearch.suggest/2} id="test-creatable-component" options={for i <- 1..20, do: {i, "Option #{i}"}} creatable @@ -282,7 +282,7 @@ defmodule PlausibleWeb.Live.Components.ComboBoxTest do options: options, submit_name: "test-submit-name", id: "test-component", - suggest_mod: ComboBox.StaticSearch + suggest_fun: &ComboBox.StaticSearch.suggest/2 ], extra_opts )