Extract component to render a switch

We're going to use it in other places.
This commit is contained in:
Javi Martín
2022-12-16 15:35:08 +01:00
parent 72704d7761
commit 0d18e25e99
6 changed files with 40 additions and 22 deletions

View File

@@ -3,9 +3,4 @@
caption { caption {
@include element-invisible; @include element-invisible;
} }
[aria-pressed] {
@include switch;
margin-bottom: 0;
}
} }

View File

@@ -0,0 +1,6 @@
.admin .toggle-switch {
[aria-pressed] {
@include switch;
margin-bottom: 0;
}
}

View File

@@ -1 +1 @@
<%= render Admin::ActionComponent.new(action, phase, options) %> <%= render Admin::ToggleSwitchComponent.new(action, phase, pressed: enabled?, **options) %>

View File

@@ -9,14 +9,7 @@ class Admin::BudgetPhases::ToggleEnabledComponent < ApplicationComponent
private private
def options def options
{ { "aria-label": t("admin.budgets.edit.enable_phase", phase: phase.name) }
text: text,
method: :patch,
remote: true,
"aria-label": t("admin.budgets.edit.enable_phase", phase: phase.name),
"aria-pressed": enabled?,
form_class: "toggle-switch"
}
end end
def action def action
@@ -26,12 +19,4 @@ class Admin::BudgetPhases::ToggleEnabledComponent < ApplicationComponent
:enable :enable
end end
end end
def text
if enabled?
t("shared.yes")
else
t("shared.no")
end
end
end end

View File

@@ -0,0 +1 @@
<%= render Admin::ActionComponent.new(action, record, **default_options.merge(options)) %>

View File

@@ -0,0 +1,31 @@
class Admin::ToggleSwitchComponent < ApplicationComponent
attr_reader :action, :record, :pressed, :options
alias_method :pressed?, :pressed
def initialize(action, record, pressed:, **options)
@action = action
@record = record
@pressed = pressed
@options = options
end
private
def text
if pressed?
t("shared.yes")
else
t("shared.no")
end
end
def default_options
{
text: text,
method: :patch,
remote: true,
"aria-pressed": pressed?,
form_class: "toggle-switch"
}
end
end