defmodule Noora.Select do @moduledoc """ A select dropdown component for choosing from a list of options. ## Example ```elixir <.select id="country" label="Select Country" name="country" value="us"> <:item value="us" label="United States" icon="flag" /> <:item value="ca" label="Canada" icon="flag" /> <:item value="uk" label="United Kingdom" icon="flag" /> ``` """ use Phoenix.Component import Noora.Dropdown import Noora.Icon alias Phoenix.HTML.FormField attr(:id, :string, required: true, doc: "Unique identifier for the dropdown component") attr(:label, :string, required: true, doc: "Main text displayed in the dropdown trigger") attr(:field, FormField, doc: "A Phoenix form field") attr(:name, :string, doc: "The name attribute for the select input") attr(:value, :string, doc: "The currently selected value") attr(:hint, :string, default: nil, doc: "Hint text for the dropdown") attr(:disabled, :boolean, default: nil, doc: "Whether the dropdown is disabled") attr(:on_value_change, :string, default: nil, doc: "Event handler for when an option is selected" ) slot(:inner_block, doc: "Content to be rendered inside the dropdown menu") slot :item do attr(:icon, :string) attr(:label, :string) attr(:value, :string) end def select(%{field: %FormField{} = field} = assigns) do assigns |> assign(field: nil, id: Map.get(assigns, :id, field.id)) |> assign_new(:name, fn -> field.name end) |> assign_new(:value, fn -> field.value end) |> select() end def select(assigns) do ~H"""
<.dropdown_item :for={item <- @item} data-part="item" value={item.value} label={item.label} class="noora-dropdown-item" > <:left_icon :if={Map.has_key?(item, :icon)}><.icon name={item.icon} /> {item.label}
<.info_circle /> {@hint}
""" end end