mirror of
https://github.com/plausible/analytics.git
synced 2024-12-23 09:33:19 +03:00
Hide creatable option when input matches suggestion (#3217)
This commit fixes a bug where the `Create "apple"` combo box option would show up even when `apple` was in the suggestions list.
This commit is contained in:
parent
232bdd34a1
commit
06305cce95
@ -164,7 +164,7 @@ defmodule PlausibleWeb.Live.Components.ComboBox do
|
||||
/>
|
||||
|
||||
<.option
|
||||
:if={@creatable && String.length(@display_value) > 0}
|
||||
:if={display_creatable_option?(assigns)}
|
||||
idx={length(@suggestions)}
|
||||
submit_value={@display_value}
|
||||
display_value={@display_value}
|
||||
@ -287,4 +287,13 @@ defmodule PlausibleWeb.Live.Components.ComboBox do
|
||||
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
|
||||
end
|
||||
|
@ -240,6 +240,14 @@ defmodule PlausibleWeb.Live.Components.ComboBoxTest do
|
||||
~s(Create "my new option")
|
||||
end
|
||||
|
||||
test "does not suggest creating value when input exact matches a suggestion", %{conn: conn} do
|
||||
{:ok, lv, _html} = live_isolated(conn, CreatableView, session: %{})
|
||||
|
||||
assert lv
|
||||
|> type_into_combo("test-creatable-component", "Option 1")
|
||||
|> text_of_element("li#dropdown-test-creatable-component-option-0 a") == ~s(Option 1)
|
||||
end
|
||||
|
||||
test "stores new value by clicking on the dropdown custom option", %{conn: conn} do
|
||||
{:ok, lv, _html} = live_isolated(conn, CreatableView, session: %{})
|
||||
type_into_combo(lv, "test-creatable-component", "my new option")
|
||||
|
Loading…
Reference in New Issue
Block a user