diff --git a/assets/js/app.js b/assets/js/app.js
index 14160ad39..7d16bfddd 100644
--- a/assets/js/app.js
+++ b/assets/js/app.js
@@ -27,6 +27,12 @@ window.addEventListener(`phx:update-value`, (e) => {
el.value = e.detail.value
})
+window.addEventListener(`phx:hide`, (e) => {
+ console.info(e)
+ let el = document.getElementById(e.detail.id)
+ el.style.display = 'none'
+})
+
const triggers = document.querySelectorAll('[data-dropdown-trigger]')
for (const trigger of triggers) {
diff --git a/lib/plausible_web/live/funnel_settings/form.ex b/lib/plausible_web/live/funnel_settings/form.ex
index 7d8642d1d..e2ba53ee1 100644
--- a/lib/plausible_web/live/funnel_settings/form.ex
+++ b/lib/plausible_web/live/funnel_settings/form.ex
@@ -29,7 +29,7 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
options={Enum.map(@goals, fn goal -> {goal.id, Plausible.Goal.display_name(goal)} end)}
/>
- + Add another step
+ + Add another step
diff --git a/lib/plausible_web/live/funnel_settings/input_picker.ex b/lib/plausible_web/live/funnel_settings/input_picker.ex
index b5b418c85..606732b03 100644
--- a/lib/plausible_web/live/funnel_settings/input_picker.ex
+++ b/lib/plausible_web/live/funnel_settings/input_picker.ex
@@ -1,182 +1,205 @@
defmodule PlausibleWeb.Live.FunnelSettings.InputPicker do
use Phoenix.LiveComponent
+ alias Phoenix.LiveView.JS
- attr :placeholder, :string, default: "Select a Goal"
+ def update(assigns, socket) do
+ socket =
+ socket
+ |> assign(assigns)
+ |> assign(:candidate, 0)
+ |> assign_new(:display_value, fn -> "" end)
+ |> assign_new(:submit_value, fn -> "" end)
+ |> assign_new(:choices, fn -> assigns.options end)
+
+ {:ok, socket}
+ end
+
+ attr :placeholder, :string, default: "Select option"
attr :id, :any, default: nil
- attr :options, :list, default: []
- attr :choices, :list, default: nil
- attr :show_picker?, :boolean, default: false
- attr :value, :string, default: ""
- attr :goal_id, :string, default: ""
- attr :higlighted, :integer, default: nil
+ attr :options, :list, required: true
- ## XXX handle phx-blur properly on tab
def render(assigns) do
~H"""
- <.picker id={"picker-#{@id}"} :if={@show_picker?} options={@options} choices={@choices || @options} target={@myself} higlighted={@higlighted} />
+ <.dropdown ref={@id} options={@options} choices={@choices} target={@myself} candidate={@candidate} />
"""
end
- attr :id, :any, default: nil
+ attr :id, :any, required: true
+
+ def dropdown_anchor(assigns) do
+ ~H"""
+
+ """
+ end
+
+ def open_dropdown(js \\ %JS{}, id) do
+ JS.show(js, to: "#dropdown-#{id}")
+ end
+
+ def close_dropdown(js \\ %JS{}, id) do
+ JS.hide(js, to: "#dropdown-#{id}")
+ end
+
+ attr :ref, :string, required: true
attr :options, :list, default: []
attr :choices, :list, default: []
- attr :higlighted, :any
+ attr :candidate, :integer, required: true
attr :target, :any
- def picker(assigns) do
+ def dropdown(assigns) do
~H"""
-
- <%= if @choices != [] do %>
- <%= for {goal_id, goal_name} <- @choices do %>
- <.li goal_id={goal_id} goal_name={goal_name} target={@target} higlighted={@higlighted}/>
- <% end %>
- <% else %>
- No matches found in the current goals. Try searching for something different.
- <% end %>
+
+ <.option :if={@choices != []} :for={{{submit_value, display_value}, idx} <- Enum.with_index(@choices)} idx={idx} submit_value={submit_value} display_value={display_value} target={@target} ref={@ref} candidate={@candidate} />
+
+ No matches found. Try searching for something different.
+
"""
end
- attr :higlighted, :any, default: nil
- attr :goal_name, :string, required: true
- attr :goal_id, :integer, required: true
+ attr :display_value, :string, required: true
+ attr :submit_value, :integer, required: true
+ attr :ref, :string, required: true
attr :target, :any
+ attr :idx, :integer, required: true
+ attr :candidate, :integer, required: true
- def li(assigns) do
+ def option(assigns) do
~H"""
- <%= if @higlighted == @goal_id do %>
- -
- <%= @goal_name %>
-
- <% else %>
- -
- <%= @goal_name %>
-
- <% end %>
+ -
+
+ <%= @display_value %>
+
+
"""
end
- def handle_event("selected", %{"goal-name" => goal_name}, socket) do
- send(self(), :goal_picked)
- {:noreply, assign(socket, %{show_picker?: false, value: goal_name})}
+ def select_option(js \\ %JS{}, id, submit_value, display_value) do
+ js
+ |> JS.push("select-option",
+ value: %{"submit-value" => submit_value, "display-value" => display_value}
+ )
+ |> close_dropdown(id)
end
- def handle_event("keypress", %{"key" => "ArrowDown"}, socket) do
- choices = socket.assigns.choices
- current = socket.assigns.higlighted
-
- next =
- Enum.drop_while(choices, fn {id, _} -> id != current end)
- |> Enum.take(2)
- |> List.last()
-
- higlighted =
- case next do
- {next_id, _} ->
- next_id
-
- nil ->
- current
- end
-
- {:noreply, assign(socket, %{higlighted: higlighted, show_picker?: true})}
+ 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("keypress", %{"key" => "ArrowUp"}, socket) do
- choices = socket.assigns.choices
- current = socket.assigns.higlighted
+ def handle_event(
+ "keypress",
+ %{"key" => "ArrowDown"},
+ %{
+ assigns: %{candidate: c, choices: choices}
+ } = socket
+ )
+ when c < length(choices) - 1 do
+ {:noreply, assign(socket, candidate: c + 1)}
+ end
- next =
- choices
- |> Enum.reverse()
- |> Enum.drop_while(fn {id, _} -> id != current end)
- |> Enum.take(2)
- |> List.last()
+ def handle_event(
+ "keypress",
+ %{"key" => "ArrowUp"},
+ %{
+ assigns: %{candidate: c}
+ } = socket
+ )
+ when c > 0 do
+ {:noreply, assign(socket, candidate: c - 1)}
+ end
- higlighted =
- case next do
- {next_id, _} ->
- next_id
+ def handle_event(
+ "keypress",
+ %{"key" => "Enter"},
+ %{assigns: %{candidate: c, choices: choices}} = socket
+ ) do
+ socket =
+ case Enum.at(choices, c) do
+ {submit_value, display_value} ->
+ do_select(socket, submit_value, display_value)
nil ->
- current
+ assign(socket, candidate: 0)
end
- {:noreply, assign(socket, %{higlighted: higlighted, show_picker?: true})}
- end
-
- def handle_event("keypress", %{"key" => "Enter"}, socket) do
- choices = socket.assigns.choices
- current = socket.assigns.higlighted
-
- goal_name =
- Enum.find_value(choices, fn {id, goal_name} ->
- if id == current, do: goal_name
- end)
-
- socket =
- socket
- |> assign(%{show_picker?: false, value: goal_name, higlighted: nil})
- |> push_event("update-value", %{id: socket.assigns.id, value: goal_name})
-
{:noreply, socket}
end
def handle_event("keypress", %{"key" => _other, "value" => typed}, socket) do
if String.length(typed) > 0 do
choices =
- Enum.filter(socket.assigns.options, fn {_id, goal_name} ->
- String.contains?(goal_name, typed) ||
- String.jaro_distance(String.upcase(goal_name), String.upcase(typed)) > 0.6
+ Enum.filter(socket.assigns.options, fn {_submit_value, display_value} ->
+ String.contains?(display_value, typed) ||
+ String.jaro_distance(String.upcase(display_value), String.upcase(typed)) > 0.6
end)
- |> Enum.sort_by(fn {_id, goal_name} -> String.jaro_distance(goal_name, typed) end, :desc)
+ |> Enum.sort_by(
+ fn {_, display_value} -> String.jaro_distance(display_value, typed) end,
+ :desc
+ )
|> Enum.take(10)
- first_id =
- case List.first(choices) do
- {id, _} -> id
- nil -> nil
- end
-
- {:noreply, assign(socket, %{choices: choices, show_picker?: true, higlighted: first_id})}
+ {:noreply, assign(socket, %{choices: choices, candidate: 0})}
else
- {:noreply, socket}
+ {:noreply, assign(socket, %{choices: Enum.take(socket.assigns.options, 10), candidate: 0})}
end
end
- def handle_event("hide-picker", _, socket) do
- {:noreply, assign(socket, %{show_picker?: false})}
- end
-
- def handle_event("show-picker", _, socket) do
- socket =
- if socket.assigns[:choices] == [] do
- assign(socket, %{show_picker?: true, choices: socket.assigns.options})
- else
- assign(socket, %{show_picker?: true})
- end
-
- {:noreply, socket}
- end
-
def handle_event(event, payload, socket) do
IO.inspect(event, label: :event)
IO.inspect(payload, label: :payload)
{:noreply, socket}
end
+
+ defp do_select(socket, submit_value, display_value) do
+ id = socket.assigns.id
+
+ socket
+ |> push_event("update-value", %{id: id, value: display_value})
+ |> push_event("hide", %{id: "dropdown-#{id}"})
+ |> assign(:candidate, 0)
+ |> assign(:display_value, display_value)
+ |> assign(:submit_value, submit_value)
+ end
end