defmodule RiverConnectWeb.Components.UI.Switch do @moduledoc """ Implementation of a switch/toggle component. A switch is a control that allows users to toggle between checked and not checked states. """ use RiverConnectWeb.Components.UI, :component @doc """ Renders a switch component. ## Props * `:id` - The id to be applied to the input element * `:name` - The name to be applied to the input element * `:class` - Additional CSS classes * `:value` - The current value of the switch * `:default-value` - The default value of the switch * `:field` - Phoenix form field * `:disabled` - Whether the switch is disabled * `:on-checked-changed` - Handler for value change event """ attr :id, :string, default: nil attr :name, :string, default: nil attr :class, :string, default: nil attr :checked, :boolean, default: false attr :disabled, :boolean, default: false attr :"on-checked-changed", :any, default: nil, doc: "Handler for value change event" attr :field, Phoenix.HTML.FormField, doc: "a form field struct retrieved from the form, for example: @form[:active]" attr :rest, :global def switch(assigns) do assigns = prepare_assign(assigns) # Normalize value for boolean assigns = assign(assigns, :checked, normalize_boolean(assigns.checked)) # Collect event mappings event_map = add_event_mapping(%{}, assigns, "checked-changed", :"on-checked-changed") assigns = assigns |> assign(:event_map, json(event_map)) |> assign(:initial_state, if(assigns.checked, do: "checked", else: "unchecked")) |> assign( :options, json(%{ disabled: assigns.disabled }) ) ~H"""
""" end end