Refactor toggles to work without JS, add specs

This commit is contained in:
Râu Cao
2023-03-15 14:04:12 +07:00
committed by Gitea
parent ca1221e9f3
commit fa56d6b772
12 changed files with 86 additions and 50 deletions

View File

@@ -2,7 +2,7 @@
module FormElements
class FieldsetComponent < ViewComponent::Base
def initialize(tag: "div", title:, description: nil)
def initialize(tag: "li", title:, description: nil)
@tag = tag
@title = title
@descripton = description

View File

@@ -11,13 +11,16 @@
<%= render FormElements::ToggleComponent.new(
enabled: @enabled,
input_enabled: @input_enabled,
class_names: @form.present? ? "hidden" : nil,
data: {
:'settings--toggle-target' => "button",
action: "settings--toggle#toggleSwitch"
}) %>
<% if @form.present? %>
<%= @form.hidden_field @attribute, value: @enabled.to_s,
data: { :'settings--toggle-target' => "input" } %>
<%= @form.check_box @attribute, {
checked: @enabled,
data: { :'settings--toggle-target' => "checkbox" }
}, "true", "false" %>
<% end %>
</div>
<% end %>

View File

@@ -1,8 +1,8 @@
<%= button_tag type: "button",
data: @data,
<%= button_tag type: "button", name: "toggle", data: @data,
role: "switch", aria: { checked: @enabled.to_s },
disabled: !@input_enabled,
class: "#{ @enabled ? 'bg-blue-600' : 'bg-gray-200' }
#{ @class_names.present? ? @class_names : '' }
relative inline-flex h-6 w-11 flex-shrink-0 cursor-pointer
rounded-full border-2 border-transparent transition-colors
duration-200 ease-in-out focus:outline-none focus:ring-2

View File

@@ -2,10 +2,11 @@
module FormElements
class ToggleComponent < ViewComponent::Base
def initialize(enabled:, input_enabled: true, data: nil)
def initialize(enabled:, input_enabled: true, data: nil, class_names: nil)
@enabled = !!enabled
@input_enabled = input_enabled
@data = data
@class_names = class_names
end
end
end