Skip to content

Commit

Permalink
add switch type to input component
Browse files Browse the repository at this point in the history
  • Loading branch information
woylie committed Dec 6, 2023
1 parent 18a3787 commit 03e3921
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion lib/doggo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +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 tel text textarea time url week)
password range radio search select switch tel text textarea time url
week)

attr :field, Phoenix.HTML.FormField,
doc: "A form field struct, for example: @form[:name]"
Expand Down Expand Up @@ -1016,6 +1017,47 @@ defmodule Doggo do
"""
end

def input(%{type: "switch"} = assigns) do
assigns =
assign_new(assigns, :checked, fn ->
Form.normalize_value("checkbox", assigns[:value])
end)

~H"""
<div class={["field", field_error_class(@errors)]} phx-feedback-for={@name}>
<.label required={@validations[:required] || false} class="switch">
<span class="switch-label"><%= @label %></span>
<input type="hidden" name={@name} value="false" />
<input
type="checkbox"
role="switch"
name={@name}
id={@id}
value={@checked_value}
checked={@checked}
aria-describedby={input_aria_describedby(@id, @errors, @description)}
{@validations}
{@rest}
/>
<span class="switch-state">
<span
class={if @checked, do: "switch-state-on", else: "switch-state-off"}
aria-hidden="true"
>
<%= if @checked do %>
On
<% else %>
Off
<% end %>
</span>
</span>
</.label>
<.field_errors for={@id} errors={@errors} />
<.field_description for={@id} description={@description} />
</div>
"""
end

def input(%{type: "select"} = assigns) do
~H"""
<div class={["field", field_error_class(@errors)]} phx-feedback-for={@name}>
Expand Down

0 comments on commit 03e3921

Please sign in to comment.