Improve ComboBox component

- make sure the list options are always of the parent input width
- allow passing a suggestion function instead of a module
pull/3310/head
Adam Rutkowski 3 years ago
parent 5b9d40b9a9
commit d947dad4c0

@ -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}"}
/>
</div>
</div>
<.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}
/>
</div>
</div>
"""
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

@ -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

@ -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
)

Loading…
Cancel
Save