defmodule ZoeyscomputerWeb.GistLive.Select do use Phoenix.Component import Phoenix.HTML.Form, only: [input_value: 2] attr :id, :string, required: true attr :form, :any, required: true attr :field, :atom, required: true attr :options, :list, required: true attr :label, :string, default: nil attr :prompt, :string, default: "Select an option..." attr :class, :string, default: nil attr :disabled, :boolean, default: false def select(assigns) do selected_value = input_value(assigns.form, assigns.field) assigns = assign(assigns, :selected_value, selected_value) ~H"""
"-container"} phx-hook="SelectHook">
"-dropdown"} phx-update="ignore" class={ [ "absolute z-10 mt-1 w-full rounded-md py-1 shadow-lg", "bg-ctp-base border-ctp-surface0 border", # Initially hidden, toggled by JS "hidden" ] } >
"-search"} placeholder="Search..." class={[ "w-full rounded-md py-1.5 px-3", "bg-ctp-mantle text-ctp-text placeholder-ctp-overlay0", "focus:outline-none focus:ring-2 focus:ring-offset-0 focus:ring-ctp-lavender", "transition-colors duration-200" ]} />
""" end defp selected_option(_options, value) when is_nil(value), do: nil defp selected_option(options, value) do case Enum.find(options, fn {_label, val} -> to_string(val) == to_string(value) end) do {label, _value} -> label _ -> nil end end defp input_id(%{id: id}, field) when is_atom(field), do: "#{id}_#{field}" defp input_name(%{name: name}, field), do: "#{name}[#{field}]" end