defmodule <%= @module %> do @moduledoc """ The `<%= @module %>` module provides a customizable input field component that integrates with Phoenix forms. This component supports a variety of input types, including text, email, password, select, textarea, and checkbox, making it versatile for different data entry scenarios. It includes features such as displaying error messages, handling various HTML attributes, and generating labels for inputs. This component is ideal for creating form fields with consistent styling and behavior across your application. ### Key Features: - **Flexible Input Types:** Supports multiple input types such as text, email, password, select, textarea, and checkbox. - **Form Integration:** Can be integrated with Phoenix.HTML.FormField for easier management of form data and error handling. - **Error Display:** Automatically displays error messages associated with the input field. - **Custom Styling:** Provides options for custom classes, labels, and other HTML attributes. This component is designed to simplify form handling and user input validation in Phoenix applications. """ use Phoenix.Component @doc """ Renders an `input_field` 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} <.error :for={msg <- @errors}>{msg} """ end def input_field(%{type: "select"} = assigns) do ~H"""
<.label for={@id}>{@label} <.error :for={msg <- @errors}>{msg}
""" end def input_field(%{type: "textarea"} = assigns) do ~H"""
<.label for={@id}>{@label} <.error :for={msg <- @errors}>{msg}
""" end # All other inputs text, datetime-local, url, password, etc. are handled here... def input_field(assigns) do ~H"""
<.label for={@id}>{@label} <.error :for={msg <- @errors}>{msg}
""" end @doc """ Renders a label. """ @doc type: :component attr :for, :string, default: nil, doc: "Specifies the form which is associated with" slot :inner_block, required: true, doc: "Inner block that renders HEEx content" def label(assigns) do ~H""" """ end @doc """ Generates a generic error message. """ @doc type: :component slot :inner_block, required: true, doc: "Inner block that renders HEEx content" def error(assigns) do ~H"""

<.icon name="hero-exclamation-circle-mini" class="mt-0.5 h-5 w-5 flex-none" /> {render_slot(@inner_block)}

""" end defp translate_error({msg, opts}) do # When using gettext, we typically pass the strings we want # to translate as a static argument: # # # Translate the number of files with plural rules # dngettext("errors", "1 file", "%{count} files", count) # # However the error messages in our forms and APIs are generated # dynamically, so we need to translate them by calling Gettext # with our gettext backend as first argument. Translations are # available in the errors.po file (as we use the "errors" domain). if count = opts[:count] do Gettext.dngettext(<%= inspect(@web_module) %>.Gettext, "errors", msg, msg, count, opts) else Gettext.dgettext(<%= inspect(@web_module) %>.Gettext, "errors", msg, opts) end end attr :name, :string, required: true, doc: "Specifies the name of the element" attr :class, :any, default: nil, doc: "Custom CSS class for additional styling" defp icon(%{name: "hero-" <> _, class: class} = assigns) when is_list(class) do ~H""" """ end defp icon(%{name: "hero-" <> _} = assigns) do ~H""" """ end end