defmodule PhoenixDuskmoon.Component.Form do @moduledoc """ render appbar """ use PhoenixDuskmoon.Component, :html import PhoenixDuskmoon.Component.Icons @doc """ Renders a simple form. ## Examples <.dm_form :let={f} for={@form} phx-change="validate" phx-submit="save"> <.dm_input field={f[:email]} label="Email"/> <.dm_input field={f[:username]} label="Username" /> <:actions> <.button>Save """ attr(:id, :any, default: nil) attr(:class, :any, default: nil) attr(:for, :any, doc: "the datastructure for the form") attr(:as, :any, default: nil, doc: "the server side parameter to collect all input under") attr(:rest, :global, include: ~w(autocomplete name rel action enctype method novalidate target multipart), doc: "the arbitrary HTML attributes to apply to the form tag" ) slot(:inner_block, required: true) slot(:actions, doc: "the slot for form actions, such as a submit button") def dm_form(assigns) do assigns = assigns |> assign_new(:for, fn -> to_form(%{}) end) ~H""" <.form id={@id} class={["dm-form", @class]} :let={f} for={@for} as={@as} {@rest} > <%= render_slot(@inner_block, f) %>
<%= render_slot(action, f) %>
""" end @doc """ Renders an input with label and error messages. A `Phoenix.HTML.FormField` may be passed as argument, which is used to retrieve the input name, id, and values. Otherwise all attributes may be passed explicitly. ## Types This function accepts all HTML input types, considering that: * You may also set `type="select"` to render a ` <%= @label %> <.dm_error :for={msg <- @errors}><%= msg %> """ end def dm_input(%{type: "toggle", value: value} = assigns) do assigns = assign_new(assigns, :checked, fn -> Phoenix.HTML.Form.normalize_value("checkbox", value) end) ~H"""
<.dm_label for={@id}><%= @label %>
<.dm_error :for={msg <- @errors}><%= msg %>
""" end def dm_input(%{type: "select"} = assigns) do ~H"""
<.dm_label for={@id} class={@errors != [] && "text-error"}><%= @label %>
<.dm_error :for={msg <- @errors}><%= msg %>
""" end def dm_input(%{type: "checkbox_group"} = assigns) do ~H"""
<.dm_label for={@id}><%= @label %>
<.dm_error :for={msg <- @errors}><%= msg %>
""" end def dm_input(%{type: "radio_group"} = assigns) do ~H"""
<.dm_label for={@id}><%= @label %>
<.dm_error :for={msg <- @errors}><%= msg %>
""" end def dm_input(%{type: "textarea"} = assigns) do ~H"""
<.dm_label for={@id} class={@errors != [] && "text-error"}><%= @label %>
<.dm_error :for={msg <- @errors}><%= msg %>
""" end def dm_input(%{type: "file"} = assigns) do ~H"""
<.dm_label for={@id} class={@errors != [] && "text-error"}><%= @label %>
<.dm_error :for={msg <- @errors}><%= msg %>
""" end # All other inputs text, datetime-local, url, password, etc. are handled here... def dm_input(assigns) do ~H"""
<.dm_label for={@id} class={@errors != [] && "text-error"}><%= @label %>
<.dm_error :for={msg <- @errors}><%= msg %>
""" end @doc """ Renders a label. """ attr(:id, :any, default: nil) attr(:class, :any, default: nil) attr(:for, :string, default: nil) slot(:inner_block, required: true) def dm_label(assigns) do ~H""" """ end @doc """ Generates a generic error message. """ attr(:id, :any, default: nil) attr(:class, :any, default: nil) slot(:inner_block, required: true) def dm_error(assigns) do ~H""" <.dm_bsi name="exclamation-circle" class="h-3 w-3 flex-none" /> <%= render_slot(@inner_block) %> """ end @doc """ Renders an input with label and error messages. A `Phoenix.HTML.FormField` may be passed as argument, which is used to retrieve the input name, id, and values. Otherwise all attributes may be passed explicitly. ## Types This function accepts all HTML input types, considering that: * You may also set `type="select"` to render a ` <%= Phoenix.HTML.Form.options_for_select(@options, @value) %> <.dm_error :for={msg <- @errors}><%= msg %> """ end def dm_compact_input(assigns) do ~H"""
<%= render_slot(@inner_block) %>
<.dm_error :for={msg <- @errors}><%= msg %> """ end end