Extract component to render a switch
We're going to use it in other places.
This commit is contained in:
@@ -3,9 +3,4 @@
|
||||
caption {
|
||||
@include element-invisible;
|
||||
}
|
||||
|
||||
[aria-pressed] {
|
||||
@include switch;
|
||||
margin-bottom: 0;
|
||||
}
|
||||
}
|
||||
|
||||
6
app/assets/stylesheets/admin/toggle_switch.scss
Normal file
6
app/assets/stylesheets/admin/toggle_switch.scss
Normal file
@@ -0,0 +1,6 @@
|
||||
.admin .toggle-switch {
|
||||
[aria-pressed] {
|
||||
@include switch;
|
||||
margin-bottom: 0;
|
||||
}
|
||||
}
|
||||
@@ -1 +1 @@
|
||||
<%= render Admin::ActionComponent.new(action, phase, options) %>
|
||||
<%= render Admin::ToggleSwitchComponent.new(action, phase, pressed: enabled?, **options) %>
|
||||
|
||||
@@ -9,14 +9,7 @@ class Admin::BudgetPhases::ToggleEnabledComponent < ApplicationComponent
|
||||
private
|
||||
|
||||
def options
|
||||
{
|
||||
text: text,
|
||||
method: :patch,
|
||||
remote: true,
|
||||
"aria-label": t("admin.budgets.edit.enable_phase", phase: phase.name),
|
||||
"aria-pressed": enabled?,
|
||||
form_class: "toggle-switch"
|
||||
}
|
||||
{ "aria-label": t("admin.budgets.edit.enable_phase", phase: phase.name) }
|
||||
end
|
||||
|
||||
def action
|
||||
@@ -26,12 +19,4 @@ class Admin::BudgetPhases::ToggleEnabledComponent < ApplicationComponent
|
||||
:enable
|
||||
end
|
||||
end
|
||||
|
||||
def text
|
||||
if enabled?
|
||||
t("shared.yes")
|
||||
else
|
||||
t("shared.no")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
1
app/components/admin/toggle_switch_component.html.erb
Normal file
1
app/components/admin/toggle_switch_component.html.erb
Normal file
@@ -0,0 +1 @@
|
||||
<%= render Admin::ActionComponent.new(action, record, **default_options.merge(options)) %>
|
||||
31
app/components/admin/toggle_switch_component.rb
Normal file
31
app/components/admin/toggle_switch_component.rb
Normal 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
|
||||
Reference in New Issue
Block a user