diff --git a/lib/doggo.ex b/lib/doggo.ex index e0fd06c6..1861f034 100644 --- a/lib/doggo.ex +++ b/lib/doggo.ex @@ -929,8 +929,8 @@ defmodule Doggo do attr :type, :string, default: "text", values: ~w(checkbox color date datetime-local email file hidden month number - password range radio search select switch tel text textarea time url - week) + password range radio radio-group search select switch tel text textarea + time url week) attr :field, Phoenix.HTML.FormField, doc: "A form field struct, for example: @form[:name]" @@ -956,8 +956,9 @@ defmodule Doggo do attr :options, :list, doc: """ - A list of options for a select element. See - `Phoenix.HTML.Form.options_for_select/2`. + A list of options for a select element or a radio group. See + `Phoenix.HTML.Form.options_for_select/2`. Note that the radio group does + not support nesting. """ attr :multiple, :boolean, @@ -1017,6 +1018,29 @@ defmodule Doggo do """ end + def input(%{type: "radio-group"} = assigns) do + ~H""" +
+
+ <%= @label %> +
+ <.radio + :for={option <- @options} + option={option} + name={@name} + id={@id} + value={@value} + errors={@errors} + description={@description} + /> +
+
+ <.field_errors for={@id} errors={@errors} /> + <.field_description for={@id} description={@description} /> +
+ """ + end + def input(%{type: "switch"} = assigns) do assigns = assign_new(assigns, :checked, fn -> @@ -1145,6 +1169,35 @@ defmodule Doggo do defp field_error_class([]), do: nil defp field_error_class(_), do: "has-errors" + defp radio(%{option: {option_label, option_value}} = assigns) do + assigns = assign(assigns, label: option_label, option_value: option_value) + + ~H""" + <.label> + "_#{@option_value}"} + value={@option_value} + checked={checked?(@option_value, @value)} + aria-describedby={input_aria_describedby(@id, @errors, @description)} + /> + <%= @label %> + + """ + end + + defp checked?(option, value) when is_list(value) do + Phoenix.HTML.html_escape(option) in Enum.map( + value, + &Phoenix.HTML.html_escape/1 + ) + end + + defp checked?(option, value) do + Phoenix.HTML.html_escape(option) == Phoenix.HTML.html_escape(value) + end + @doc """ Renders the label for an input. """