Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Hide creatable option when input matches suggestion (#3217)

Vini Brasil06305cce9519
diff --git a/lib/plausible_web/live/components/combo_box.ex b/lib/plausible_web/live/components/combo_box.ex
index 2267d66be3..136f4a35d3 100644
--- a/lib/plausible_web/live/components/combo_box.ex
+++ b/lib/plausible_web/live/components/combo_box.ex
@@ -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
diff --git a/test/plausible_web/live/components/combo_box_test.exs b/test/plausible_web/live/components/combo_box_test.exs
index c2f834ae4d..70ff9ca46e 100644
--- a/test/plausible_web/live/components/combo_box_test.exs
+++ b/test/plausible_web/live/components/combo_box_test.exs
@@ -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")