Skip to content

Commit e939417

Browse files
committed
pass status labels to the toggle switch
1 parent b201fa6 commit e939417

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
@@ -29,6 +29,8 @@ class ToggleSwitch < Primer::Component
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.
3131
# @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
32+
# @param on_label [String] Custom label to show when switch is ON. Defaults to On.
33+
# @param off_label [String] Custom label to show when switch is OFF. Defaults to Off.
3234
def initialize(
3335
src: nil,
3436
csrf_token: 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
@@ -67,5 +67,9 @@ def with_autofocus
6767
render(Primer::Alpha::ToggleSwitch.new(src: UrlHelpers.primer_view_components.toggle_switch_index_path, autofocus: true))
6868
end
6969
end
70+
71+
def with_custom_status_label
72+
render Primer::Alpha::ToggleSwitch.new(src: UrlHelpers.primer_view_components.toggle_switch_index_path, on_label: "Enabled", off_label: "Disabled")
73+
end
7074
end
7175
end

0 commit comments

Comments
 (0)