Skip to content

Commit 096d5d5

Browse files
committed
Adjustments in the Switch component suggested by @stephannv
1 parent 73d2d6f commit 096d5d5

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

app/components/ruby_ui/switch.rb

+9-15
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,23 @@
22

33
module RubyUI
44
class Switch < Base
5-
def view_template
6-
attrs => { include_hidden:, **input_attrs }
5+
def initialize(include_hidden: true, checked_value: "1", unchecked_value: "0", **attrs)
6+
@include_hidden = include_hidden
7+
@checked_value = checked_value
8+
@unchecked_value = unchecked_value
9+
super(**attrs)
10+
end
711

12+
def view_template
813
label(
914
role: "switch",
1015
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"
1116
) do
12-
input(type: "hidden", name: attrs[:name], value: "0") if include_hidden
13-
input(**input_attrs)
17+
input(type: "hidden", name: attrs[:name], value: @unchecked_value) if @include_hidden
18+
input(**attrs.merge(type: "checkbox", class: "hidden peer", value: @checked_value))
1419

1520
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 ")
1621
end
1722
end
18-
19-
private
20-
21-
def default_attrs
22-
{
23-
class: "hidden peer",
24-
type: "checkbox",
25-
include_hidden: true,
26-
value: "1"
27-
}
28-
end
2923
end
3024
end

0 commit comments

Comments
 (0)