-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
154 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
daisy_ui_components_site/storybook/components/toggle.story.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
defmodule Storybook.Components.Toggle do | ||
use PhoenixStorybook.Story, :component | ||
|
||
alias DaisyUIComponents.Utils | ||
|
||
def function, do: &DaisyUIComponents.Toggle.toggle/1 | ||
|
||
def imports, do: [] | ||
|
||
def variations do | ||
[ | ||
%Variation{ | ||
id: :default | ||
}, | ||
%VariationGroup{ | ||
id: :colors, | ||
variations: | ||
for color <- Utils.colors() do | ||
%Variation{ | ||
id: String.to_atom(color), | ||
attributes: %{ | ||
color: color, | ||
checked: "checked" | ||
} | ||
} | ||
end | ||
}, | ||
%VariationGroup{ | ||
id: :sizes, | ||
variations: | ||
for size <- Utils.sizes() do | ||
%Variation{ | ||
id: String.to_atom(size), | ||
attributes: %{ | ||
size: size | ||
} | ||
} | ||
end | ||
} | ||
] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
defmodule DaisyUIComponents.Toggle do | ||
@moduledoc """ | ||
Stat component | ||
https://daisyui.com/components/toggle | ||
""" | ||
|
||
use DaisyUIComponents.Component | ||
|
||
attr :class, :any, default: nil | ||
attr :color, :string, values: colors() | ||
attr :size, :string, values: sizes() | ||
attr :rest, :global, include: ~w(checked) | ||
|
||
def toggle(assigns) do | ||
assigns = | ||
assign( | ||
assigns, | ||
:class, | ||
join_classes( | ||
[ | ||
"toggle", | ||
toggle_color(assigns[:color]), | ||
toggle_size(assigns[:size]) | ||
], | ||
assigns.class | ||
) | ||
) | ||
|
||
~H""" | ||
<input type="checkbox" class={@class} {@rest} /> | ||
""" | ||
end | ||
|
||
# Color | ||
defp toggle_color("primary"), do: "toggle-primary" | ||
defp toggle_color("secondary"), do: "toggle-secondary" | ||
defp toggle_color("accent"), do: "toggle-accent" | ||
defp toggle_color("info"), do: "toggle-info" | ||
defp toggle_color("success"), do: "toggle-success" | ||
defp toggle_color("warning"), do: "toggle-warning" | ||
defp toggle_color("error"), do: "toggle-error" | ||
defp toggle_color(_color), do: nil | ||
|
||
# Size | ||
defp toggle_size("xs"), do: "toggle-xs" | ||
defp toggle_size("sm"), do: "toggle-sm" | ||
defp toggle_size("md"), do: "toggle-md" | ||
defp toggle_size("lg"), do: "toggle-lg" | ||
defp toggle_size(_size), do: nil | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
defmodule DaisyUIComponents.ToggleTest do | ||
use ExUnit.Case | ||
|
||
import Phoenix.Component | ||
import Phoenix.LiveViewTest | ||
import DaisyUIComponents.Toggle | ||
|
||
alias DaisyUIComponents.Utils | ||
|
||
test "toggle" do | ||
assigns = %{} | ||
|
||
toggle = | ||
rendered_to_string(~H""" | ||
<.toggle checked="checked" /> | ||
""") | ||
|
||
assert toggle =~ ~s(<input) | ||
assert toggle =~ ~s(class="toggle") | ||
assert toggle =~ ~s(type="checkbox") | ||
assert toggle =~ ~s(checked="checked") | ||
|
||
toggle = | ||
rendered_to_string(~H""" | ||
<.toggle /> | ||
""") | ||
|
||
assert toggle =~ ~s(<input) | ||
assert toggle =~ ~s(class="toggle") | ||
assert toggle =~ ~s(type="checkbox") | ||
end | ||
|
||
test "toggle colors" do | ||
for color <- Utils.colors() do | ||
assigns = %{color: color} | ||
|
||
assert rendered_to_string(~H""" | ||
<.toggle color={@color} /> | ||
""") =~ ~s(<input type="checkbox" class="toggle toggle-#{color}">) | ||
end | ||
end | ||
|
||
test "toggle sizes" do | ||
for size <- Utils.sizes() do | ||
assigns = %{size: size} | ||
|
||
assert rendered_to_string(~H""" | ||
<.toggle size={@size} /> | ||
""") =~ ~s(<input type="checkbox" class="toggle toggle-#{size}">) | ||
end | ||
end | ||
end |