Skip to content

Commit c3b3bc0

Browse files
committed
pass status labels to the toggle switch
1 parent 492b84a commit c3b3bc0

3 files changed

Lines changed: 13 additions & 2 deletions

File tree

app/components/primer/alpha/toggle_switch.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
<%= render(Primer::Beta::Spinner.new(size: :small, hidden: "true", data: { target: "toggle-switch.loadingSpinner" })) %>
55
</span>
66
<%= render(Primer::Beta::Text.new(aria: { hidden: true }, classes: "ToggleSwitch-status")) do %>
7-
<%= render(Primer::Box.new(classes: "ToggleSwitch-statusOn").with_content("On")) %>
8-
<%= render(Primer::Box.new(classes: "ToggleSwitch-statusOff").with_content("Off")) %>
7+
<%= render(Primer::Box.new(classes: "ToggleSwitch-statusOn").with_content(@on_label)) %>
8+
<%= render(Primer::Box.new(classes: "ToggleSwitch-statusOff").with_content(@off_label)) %>
99
<% end %>
1010

1111
<%= render(Primer::BaseComponent.new(tag: :button, classes: "ToggleSwitch-track", disabled: disabled?, data: { target: "toggle-switch.switch", action: "click:toggle-switch#toggle" }, **@button_arguments)) do %>

app/components/primer/alpha/toggle_switch.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class ToggleSwitch < Primer::Component
2828
# @param status_label_position [Symbol] Which side of the toggle switch to render the status label. <%= one_of(Primer::Alpha::ToggleSwitch::STATUS_LABEL_POSITION_OPTIONS) %>
2929
# @param turbo [Boolean] Whether or not to request a turbo stream and render the response as such.
3030
# @param autofocus [Boolean] Whether switch should be autofocused when rendered.
31+
# @param on_label [String] Custom label to show when switch is ON. Defaults to I18n.t("label_switch.on").
32+
# @param off_label [String] Custom label to show when switch is OFF. Defaults to I18n.t("label_switch.off").
3133
# @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
3234
def initialize(
3335
src: nil,
@@ -38,6 +40,8 @@ def initialize(
3840
status_label_position: STATUS_LABEL_POSITION_DEFAULT,
3941
turbo: false,
4042
autofocus: nil,
43+
on_label: nil,
44+
off_label: nil,
4145
**system_arguments
4246
)
4347
@src = src
@@ -70,6 +74,9 @@ def initialize(
7074
@button_arguments[:autofocus] = true if autofocus
7175

7276
@system_arguments[:src] = @src if @src
77+
78+
@on_label = on_label || "On"
79+
@off_label = off_label || "Off"
7380
end
7481

7582
def on?

previews/primer/alpha/toggle_switch_preview.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ def with_turbo
6666
def with_autofocus
6767
render(Primer::Alpha::ToggleSwitch.new(src: UrlHelpers.primer_view_components.toggle_switch_index_path, autofocus: true))
6868
end
69+
70+
def with_custom_label
71+
render Primer::Alpha::ToggleSwitch.new(src: UrlHelpers.primer_view_components.toggle_switch_index_path, on_label: "Enabled", off_label: "Disabled")
72+
end
6973
end
7074
end
7175
end

0 commit comments

Comments
 (0)