Class: Primer::Alpha::ToggleSwitch
- Defined in:
- app/components/primer/alpha/toggle_switch.rb
Overview
The ToggleSwitch component is a button that toggles between two boolean states. It is meant to be used for settings that should cause an immediate update. If configured with a “src” attribute, the component will make a POST request containing data of the form “value: 0 | 1”.
Constant Summary collapse
- SIZE_DEFAULT =
:medium- SIZE_MAPPINGS =
{ SIZE_DEFAULT => nil, :small => "ToggleSwitch--small" }.freeze
- SIZE_OPTIONS =
SIZE_MAPPINGS.keys.freeze
- STATUS_LABEL_POSITION_DEFAULT =
:start- STATUS_LABEL_POSITION_MAPPINGS =
{ STATUS_LABEL_POSITION_DEFAULT => nil, :end => "ToggleSwitch--statusAtEnd" }.freeze
- STATUS_LABEL_POSITION_OPTIONS =
STATUS_LABEL_POSITION_MAPPINGS.keys.freeze
Constants inherited from Component
Component::INVALID_ARIA_LABEL_TAGS
Constants included from Status::Dsl
Constants included from ViewHelper
Constants included from TestSelectorHelper
TestSelectorHelper::TEST_SELECTOR_TAG
Constants included from FetchOrFallbackHelper
FetchOrFallbackHelper::InvalidValueError
Constants included from Primer::AttributesHelper
Primer::AttributesHelper::PLURAL_ARIA_ATTRIBUTES, Primer::AttributesHelper::PLURAL_DATA_ATTRIBUTES
Instance Method Summary collapse
- #disabled? ⇒ Boolean
- #enabled? ⇒ Boolean
-
#initialize(src: nil, csrf_token: nil, checked: false, enabled: true, size: SIZE_DEFAULT, status_label_position: STATUS_LABEL_POSITION_DEFAULT, turbo: false, autofocus: nil, on_label: nil, off_label: nil, button_type: nil, **system_arguments) ⇒ ToggleSwitch
constructor
A new instance of ToggleSwitch.
- #on? ⇒ Boolean
Methods inherited from Component
Methods included from JoinStyleArgumentsHelper
Methods included from TestSelectorHelper
Methods included from FetchOrFallbackHelper
#fetch_or_fallback, #fetch_or_fallback_boolean, #silence_deprecations?
Methods included from ClassNameHelper
Methods included from Primer::AttributesHelper
#aria, #data, #extract_data, #merge_aria, #merge_data, #merge_prefixed_attribute_hashes
Methods included from ExperimentalSlotHelpers
Methods included from ExperimentalRenderHelpers
Constructor Details
#initialize(src: nil, csrf_token: nil, checked: false, enabled: true, size: SIZE_DEFAULT, status_label_position: STATUS_LABEL_POSITION_DEFAULT, turbo: false, autofocus: nil, on_label: nil, off_label: nil, button_type: nil, **system_arguments) ⇒ ToggleSwitch
Returns a new instance of ToggleSwitch.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'app/components/primer/alpha/toggle_switch.rb', line 44 def initialize( src: nil, csrf_token: nil, checked: false, enabled: true, size: SIZE_DEFAULT, status_label_position: STATUS_LABEL_POSITION_DEFAULT, turbo: false, autofocus: nil, on_label: nil, off_label: nil, button_type: nil, **system_arguments ) @src = src @csrf_token = csrf_token @checked = checked @enabled = enabled @turbo = turbo @system_arguments = system_arguments @size = fetch_or_fallback(SIZE_OPTIONS, size, SIZE_DEFAULT) @status_label_position = fetch_or_fallback( STATUS_LABEL_POSITION_OPTIONS, status_label_position, STATUS_LABEL_POSITION_DEFAULT ) @system_arguments[:classes] = class_names( @system_arguments.delete(:classes), "ToggleSwitch", on? ? "ToggleSwitch--checked" : nil, enabled? ? nil : "ToggleSwitch--disabled", STATUS_LABEL_POSITION_MAPPINGS[@status_label_position], SIZE_MAPPINGS[@size] ) @button_arguments = { type: , aria: merge_aria( @system_arguments, aria: { pressed: on? } ) } @button_arguments[:autofocus] = true if autofocus @system_arguments[:src] = @src if @src @on_label = on_label || "On" @off_label = off_label || "Off" end |
Instance Method Details
#disabled? ⇒ Boolean
102 103 104 |
# File 'app/components/primer/alpha/toggle_switch.rb', line 102 def disabled? !enabled? end |
#enabled? ⇒ Boolean
98 99 100 |
# File 'app/components/primer/alpha/toggle_switch.rb', line 98 def enabled? @enabled end |
#on? ⇒ Boolean
94 95 96 |
# File 'app/components/primer/alpha/toggle_switch.rb', line 94 def on? @checked end |