Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turn checkbox into toggle 🎚️ #29

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion lib/quick_average_web/components/core_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ defmodule QuickAverageWeb.CoreComponents do
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)
range radio search select tel text textarea time url week toggle)

attr :value, :any

Expand Down Expand Up @@ -358,6 +358,30 @@ defmodule QuickAverageWeb.CoreComponents do
"""
end

def input(%{type: "toggle"} = assigns) do
assigns =
assign_new(assigns, :checked, fn ->
input_equals?(assigns.value, "true")
end)

~H"""
<label class="relative inline-flex items-center gap-2 cursor-pointer">
<input
type="checkbox"
id={@id || @name}
name={@name}
value="true"
checked={@checked}
class="sr-only peer"
{@rest}
/>
<div class="w-11 h-6 bg-gray-200 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-blue-300 dark:peer-focus:ring-blue-800 rounded-full peer dark:bg-gray-700 peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all dark:border-gray-600 peer-checked:bg-blue-600">
</div>
<%= @label %>
</label>
"""
end

def input(%{type: "select"} = assigns) do
~H"""
<div phx-feedback-for={@name}>
Expand Down
6 changes: 5 additions & 1 deletion lib/quick_average_web/live/average_live.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
>
<.input field={{f, :name}} type="text" label="Name" maxlength={30} />

<.input field={{f, :only_viewing}} type="checkbox" label="Only Viewing" />
<.input
field={{f, :only_viewing}}
type="toggle"
label="Only Viewing 📺"
/>

<%= if !@only_viewing do %>
<.input
Expand Down
Loading