defmodule PetalComponents.Field do use Phoenix.Component @doc """ Renders an input with label and error messages. If you just want an input, check out input.ex A `%Phoenix.HTML.FormField{}` and type may be passed to the field to build input names and error messages, or all the attributes and errors may be passed explicitly. ## Examples <.field field={@form[:email]} type="email" /> <.field label="Name" value="" name="name" errors={["oh no!"]} /> """ attr :id, :any, default: nil, doc: "the id of the input. If not passed, it will be generated automatically from the field" attr :name, :any, doc: "the name of the input. If not passed, it will be generated automatically from the field" attr :label, :string, doc: "the label for the input. If not passed, it will be generated automatically from the field" attr :value, :any, doc: "the value of the input. If not passed, it will be generated automatically from the field" attr :type, :string, default: "text", values: ~w(checkbox checkbox-group color date datetime-local email file hidden month number password range radio-group search select switch tel text textarea time url week), doc: "the type of input" attr :field, Phoenix.HTML.FormField, doc: "a form field struct retrieved from the form, for example: @form[:email]" attr :errors, :list, default: [], doc: "a list of errors to display. If not passed, it will be generated automatically from the field. Format is a list of strings." attr :checked, :any, doc: "the checked flag for checkboxes and checkbox groups" attr :prompt, :string, default: nil, doc: "the prompt for select inputs" attr :options, :list, doc: "the options to pass to Phoenix.HTML.Form.options_for_select/2" attr :multiple, :boolean, default: false, doc: "the multiple flag for select inputs" attr :disabled_options, :list, default: [], doc: "the options to disable in a checkbox group" attr :group_layout, :string, values: ["row", "col"], default: "row", doc: "the layout of the inputs in a group (checkbox-group or radio-group)" attr :class, :string, default: nil, doc: "the class to add to the input" attr :wrapper_class, :string, default: nil, doc: "the wrapper div classes" attr :help_text, :string, default: nil, doc: "context/help for your field" attr :label_class, :string, default: nil, doc: "extra CSS for your label" attr :rest, :global, include: ~w(autocomplete disabled form max maxlength min minlength list pattern placeholder readonly required size step value name multiple prompt selected default year month day hour minute second builder options layout cols rows wrap checked accept), doc: "All other props go on the input" def field(%{field: %Phoenix.HTML.FormField{} = field} = assigns) do assigns |> assign(field: nil, id: assigns.id || field.id) |> assign(:errors, Enum.map(field.errors, &translate_error(&1))) |> assign_new(:name, fn -> if assigns.multiple && assigns.type not in ["checkbox-group", "radio-group"], do: field.name <> "[]", else: field.name end) |> assign_new(:value, fn -> field.value end) |> assign_new(:label, fn -> Phoenix.HTML.Form.humanize(field.field) end) |> field() end def field(%{type: "checkbox", value: value} = assigns) do assigns = assign_new(assigns, :checked, fn -> Phoenix.HTML.Form.normalize_value("checkbox", value) end) ~H""" <.field_wrapper errors={@errors} name={@name} class={@wrapper_class}> <.field_error :for={msg <- @errors}><%= msg %> <.field_help_text help_text={@help_text} /> """ end def field(%{type: "select"} = assigns) do ~H""" <.field_wrapper errors={@errors} name={@name} class={@wrapper_class}> <.field_label for={@id} class={@label_class}><%= @label %> <.field_error :for={msg <- @errors}><%= msg %> <.field_help_text help_text={@help_text} /> """ end def field(%{type: "textarea"} = assigns) do ~H""" <.field_wrapper errors={@errors} name={@name} class={@wrapper_class}> <.field_label for={@id} class={@label_class}><%= @label %> <.field_error :for={msg <- @errors}><%= msg %> <.field_help_text help_text={@help_text} /> """ end def field(%{type: "switch", value: value} = assigns) do assigns = assign_new(assigns, :checked, fn -> Phoenix.HTML.Form.normalize_value("checkbox", value) end) ~H""" <.field_wrapper errors={@errors} name={@name} class={@wrapper_class}> <.field_error :for={msg <- @errors}><%= msg %> <.field_help_text help_text={@help_text} /> """ end def field(%{type: "checkbox-group"} = assigns) do assigns = assigns |> assign_new(:checked, fn -> values = case assigns.value do value when is_binary(value) -> [value] value when is_list(value) -> value _ -> [] end Enum.map(values, &to_string/1) end) ~H""" <.field_wrapper errors={@errors} name={@name} class={@wrapper_class}> <.field_label for={@id} class={@label_class}><%= @label %>
<%= render_slot(@inner_block) %>
""" end attr :class, :string, default: "", doc: "extra classes for the help text" attr :help_text, :string, default: nil, doc: "context/help for your field" slot :inner_block, required: false attr :rest, :global def field_help_text(assigns) do ~H"""