Class: Fluxbit::Form::ToggleComponent
- Inherits:
-
FieldComponent
- Object
- FieldComponent
- Fluxbit::Form::ToggleComponent
- Includes:
- Config::Form::ToggleComponent
- Defined in:
- app/components/fluxbit/form/toggle_component.rb
Overview
The ‘Fluxbit::Form::ToggleComponent` renders a styled switch/toggle (on/off) input. It supports custom label placement, color and sizing options, helper text, and is fully compatible with Rails form builders. Additional options allow you to invert label order, customize colors for checked/unchecked/button states, and provide an extra label via a slot.
Instance Method Summary collapse
-
#initialize(**props) ⇒ ToggleComponent
constructor
Initializes the toggle component with the given properties.
- #input_class ⇒ Object
- #label_class ⇒ Object
- #toggle_class ⇒ Object
- #valid_color(color) ⇒ Object
Constructor Details
#initialize(**props) ⇒ ToggleComponent
Initializes the toggle component with the given properties.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'app/components/fluxbit/form/toggle_component.rb', line 37 def initialize(**props) super(**props) @other_label = props.delete(:other_label) @sizing = @props.delete(:sizing) || @@sizing @sizing = (styles[:toggle][:sizes].count - 1) if @sizing > (styles[:toggle][:sizes].count - 1) @color = valid_color(@props.delete(:color)) @unchecked_color = ( @props.delete(:unchecked_color), collection: styles[:toggle][:unchecked].keys, default: @@unchecked_color ) @button_color = (@props.delete(:button_color), collection: styles[:toggle][:button].keys, default: @@button_color) @invert_label = (@props.delete(:invert_label), collection: [ true, false ], default: @@invert_label) add to: @props, first_element: true, class: styles[:input] end |
Instance Method Details
#input_class ⇒ Object
66 67 68 |
# File 'app/components/fluxbit/form/toggle_component.rb', line 66 def input_class styles[:input] end |
#label_class ⇒ Object
62 63 64 |
# File 'app/components/fluxbit/form/toggle_component.rb', line 62 def label_class styles[:label] end |
#toggle_class ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 |
# File 'app/components/fluxbit/form/toggle_component.rb', line 70 def toggle_class [ (@invert_label || @other_label || other_label?) ? styles[:toggle][:invert_label] : nil, styles[:toggle][:base], styles[:toggle][:unchecked][@unchecked_color], styles[:toggle][:checked][@color], styles[:toggle][:button][@button_color], styles[:toggle][:sizes][@sizing], styles[:toggle][:active][(@props[:disabled] ? :off : :on)] ].compact.join(" ") end |
#valid_color(color) ⇒ Object
55 56 57 58 59 60 |
# File 'app/components/fluxbit/form/toggle_component.rb', line 55 def valid_color(color) return color if styles[:toggle][:checked].key?(color) return :failure if errors.present? @@color end |