|
| 1 | +defmodule PeekAppSDK.UI.Odyssey.Select do |
| 2 | + @moduledoc """ |
| 3 | + A generic dropdown LiveComponent for selecting items with search/filter support. |
| 4 | +
|
| 5 | + Each item must have an `id` and `name` field. Color is optional and can be |
| 6 | + specified via the `color_field` assign (defaults to `:color_hex`). |
| 7 | +
|
| 8 | + ## Examples |
| 9 | +
|
| 10 | + <.odyssey_select |
| 11 | + id="activity-picker" |
| 12 | + items={@activities} |
| 13 | + on_select={:activity_selected} |
| 14 | + title="Select Activity" |
| 15 | + /> |
| 16 | +
|
| 17 | + <.odyssey_select |
| 18 | + id="ticket-picker" |
| 19 | + items={@tickets} |
| 20 | + excluded_ids={@used_ids} |
| 21 | + on_select={:ticket_selected} |
| 22 | + context={@index} |
| 23 | + color_field={:colorHex} |
| 24 | + title="+ Add Another Ticket" |
| 25 | + /> |
| 26 | + """ |
| 27 | + |
| 28 | + use Phoenix.LiveComponent |
| 29 | + |
| 30 | + @impl true |
| 31 | + def mount(socket) do |
| 32 | + {:ok, |
| 33 | + socket |
| 34 | + |> assign(:open, false) |
| 35 | + |> assign(:search, "")} |
| 36 | + end |
| 37 | + |
| 38 | + @impl true |
| 39 | + def update(assigns, socket) do |
| 40 | + {:ok, |
| 41 | + socket |
| 42 | + |> assign(assigns) |
| 43 | + |> assign_new(:title, fn -> "Select Item" end) |
| 44 | + |> assign_new(:excluded_ids, fn -> [] end) |
| 45 | + |> assign_new(:color_field, fn -> :color_hex end) |
| 46 | + |> assign_new(:context, fn -> nil end)} |
| 47 | + end |
| 48 | + |
| 49 | + @impl true |
| 50 | + def render(assigns) do |
| 51 | + ~H""" |
| 52 | + <div |
| 53 | + id={@id} |
| 54 | + class="relative inline-block text-left" |
| 55 | + phx-click-away="close" |
| 56 | + phx-target={@myself} |
| 57 | + phx-hook="OdysseySelect" |
| 58 | + > |
| 59 | + <button |
| 60 | + type="button" |
| 61 | + class="inline-flex items-center gap-2 rounded-md border px-3 py-2 text-sm font-medium border-zinc-300 bg-white hover:bg-zinc-50 cursor-pointer" |
| 62 | + phx-click="toggle" |
| 63 | + phx-target={@myself} |
| 64 | + aria-expanded={@open} |
| 65 | + > |
| 66 | + <span>{@title}</span> |
| 67 | + <svg class="w-4 h-4 text-zinc-500" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true"> |
| 68 | + <path |
| 69 | + fill-rule="evenodd" |
| 70 | + d="M5.23 7.21a.75.75 0 0 1 1.06.02L10 11.168l3.71-3.938a.75.75 0 1 1 1.09 1.028l-4.24 4.5a.75.75 0 0 1-1.09 0l-4.24-4.5a.75.75 0 0 1 .02-1.06z" |
| 71 | + clip-rule="evenodd" |
| 72 | + /> |
| 73 | + </svg> |
| 74 | + </button> |
| 75 | +
|
| 76 | + <div |
| 77 | + :if={@open} |
| 78 | + data-dropdown |
| 79 | + class="absolute z-50 w-80 rounded-xl border border-zinc-200 bg-white shadow-lg ring-1 ring-black/10 overflow-hidden" |
| 80 | + > |
| 81 | + <div class="px-3 pt-3"> |
| 82 | + <div class="flex items-center gap-2 rounded-md border border-zinc-300 bg-zinc-50 px-3 py-2 text-sm text-zinc-600"> |
| 83 | + <svg class="w-4 h-4 opacity-70" viewBox="0 0 20 20" fill="currentColor"> |
| 84 | + <path |
| 85 | + fill-rule="evenodd" |
| 86 | + d="M12.9 14.32a8 8 0 1 1 1.414-1.414l3.39 3.39a1 1 0 0 1-1.414 1.414l-3.39-3.39ZM14 8a6 6 0 1 0-12 0 6 6 0 0 0 12 0Z" |
| 87 | + clip-rule="evenodd" |
| 88 | + /> |
| 89 | + </svg> |
| 90 | + <input |
| 91 | + type="text" |
| 92 | + class="flex-1 bg-transparent outline-none" |
| 93 | + placeholder="Search" |
| 94 | + value={@search} |
| 95 | + phx-keyup="search" |
| 96 | + phx-target={@myself} |
| 97 | + /> |
| 98 | + </div> |
| 99 | + </div> |
| 100 | +
|
| 101 | + <div class="py-2 max-h-[220px] overflow-y-auto"> |
| 102 | + <button |
| 103 | + :for={item <- filtered_items(@items, @search, @excluded_ids)} |
| 104 | + type="button" |
| 105 | + class="text-left flex w-full items-center gap-2 px-3 py-2 text-sm text-zinc-700 hover:bg-zinc-50 cursor-pointer" |
| 106 | + phx-click="select" |
| 107 | + phx-value-id={item.id} |
| 108 | + phx-target={@myself} |
| 109 | + > |
| 110 | + <span |
| 111 | + class="inline-block h-2.5 w-2.5 rounded-full mr-2 align-middle" |
| 112 | + aria-hidden="true" |
| 113 | + style={"background-color: #{get_color(item, @color_field)}"} |
| 114 | + /> |
| 115 | + <span>{item.name}</span> |
| 116 | + </button> |
| 117 | + </div> |
| 118 | + </div> |
| 119 | + </div> |
| 120 | + """ |
| 121 | + end |
| 122 | + |
| 123 | + @impl true |
| 124 | + def handle_event("toggle", _, socket) do |
| 125 | + {:noreply, assign(socket, :open, not socket.assigns.open)} |
| 126 | + end |
| 127 | + |
| 128 | + def handle_event("close", _, socket) do |
| 129 | + {:noreply, socket |> assign(:open, false) |> assign(:search, "")} |
| 130 | + end |
| 131 | + |
| 132 | + def handle_event("search", %{"value" => value}, socket) do |
| 133 | + {:noreply, assign(socket, :search, value)} |
| 134 | + end |
| 135 | + |
| 136 | + def handle_event("select", %{"id" => id}, socket) do |
| 137 | + %{on_select: on_select, context: context} = socket.assigns |
| 138 | + send(self(), {on_select, context, id}) |
| 139 | + {:noreply, socket |> assign(:open, false) |> assign(:search, "")} |
| 140 | + end |
| 141 | + |
| 142 | + defp get_color(item, field), do: Map.get(item, field) || "#CCCCCC" |
| 143 | + |
| 144 | + defp filtered_items(items, search, excluded_ids) do |
| 145 | + excluded_ids = Enum.map(excluded_ids, &to_string/1) |
| 146 | + |
| 147 | + items |
| 148 | + |> Enum.reject(&(to_string(&1.id) in excluded_ids)) |
| 149 | + |> Enum.filter(&matches_search?(&1, search)) |
| 150 | + end |
| 151 | + |
| 152 | + defp matches_search?(_, ""), do: true |
| 153 | + |
| 154 | + defp matches_search?(item, search) do |
| 155 | + String.contains?(String.downcase(item.name), String.downcase(search)) |
| 156 | + end |
| 157 | + |
| 158 | + @doc """ |
| 159 | + Renders an odyssey_select dropdown component. |
| 160 | +
|
| 161 | + ## Examples |
| 162 | +
|
| 163 | + <.odyssey_select |
| 164 | + id="activity-picker" |
| 165 | + items={@activities} |
| 166 | + on_select={:activity_selected} |
| 167 | + title="Select Activity" |
| 168 | + /> |
| 169 | + """ |
| 170 | + attr :id, :string, required: true |
| 171 | + attr :items, :list, required: true, doc: "list of items with :id and :name fields" |
| 172 | + attr :on_select, :atom, required: true, doc: "message atom sent when item is selected" |
| 173 | + attr :title, :string, default: "Select Item", doc: "button text" |
| 174 | + attr :excluded_ids, :list, default: [], doc: "list of ids to exclude" |
| 175 | + attr :color_field, :atom, default: :color_hex, doc: "atom for the color field on items" |
| 176 | + attr :context, :any, default: nil, doc: "additional context included in the message" |
| 177 | + |
| 178 | + def odyssey_select(assigns) do |
| 179 | + assigns = assign(assigns, :module, __MODULE__) |
| 180 | + |
| 181 | + ~H""" |
| 182 | + <.live_component {assigns} /> |
| 183 | + """ |
| 184 | + end |
| 185 | +end |
0 commit comments