Skip to content

Commit

Permalink
Switch component implemented (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoluigi authored Dec 2, 2024
1 parent 79901e2 commit 6c79d37
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ bin/rails g ruby_ui:component Accordion

## Documentation 📖

Visit https://rubyui.com to view the full documentation, including:
Visit https://rubyui.com/docs/introduction to view the full documentation, including:

- Detailed component guides
- Themes
Expand Down
24 changes: 24 additions & 0 deletions lib/ruby_ui/switch/switch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module RubyUI
class Switch < Base
def initialize(include_hidden: true, checked_value: "1", unchecked_value: "0", **attrs)
@include_hidden = include_hidden
@checked_value = checked_value
@unchecked_value = unchecked_value
super(**attrs)
end

def view_template
label(
role: "switch",
class: "peer inline-flex h-6 w-11 shrink-0 cursor-pointer items-center rounded-full border-2 border-transparent transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background has-[:disabled]:cursor-not-allowed has-[:disabled]:opacity-50 bg-input has-[:checked]:bg-primary"
) do
input(type: "hidden", name: attrs[:name], value: @unchecked_value) if @include_hidden
input(**attrs.merge(type: "checkbox", class: "hidden peer", value: @checked_value))

span(class: "pointer-events-none block h-5 w-5 rounded-full bg-background shadow-lg ring-0 transition-transform translate-x-0 peer-checked:translate-x-5 ")
end
end
end
end
21 changes: 21 additions & 0 deletions test/ruby_ui/switch_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

require "test_helper"

class RubyUI::SwitchTest < ComponentTest
def test_render_with_all_items
output = phlex do
RubyUI.Switch(name: "toggle")
end

assert_match(/toggle/, output)
end

def test_render_checked
output = phlex do
RubyUI.Switch(name: "toggle", checked: true)
end

assert_match(/checked/, output)
end
end

0 comments on commit 6c79d37

Please sign in to comment.