Class: MaquinaComponents::ToggleGroupHelper::ToggleGroupBuilder

Inherits:
Object
  • Object
show all
Defined in:
app/helpers/maquina_components/toggle_group_helper.rb

Overview

Builder class for toggle group

Instance Method Summary collapse

Constructor Details

#initialize(view_context, type:, variant:, size:, value:, disabled:) ⇒ ToggleGroupBuilder

Returns a new instance of ToggleGroupBuilder.



102
103
104
105
106
107
108
109
110
111
# File 'app/helpers/maquina_components/toggle_group_helper.rb', line 102

def initialize(view_context, type:, variant:, size:, value:, disabled:)
  @view = view_context
  @type = type
  @variant = variant
  @size = size
  @value = value
  @disabled = disabled
  @items = []
  @selected_values = normalize_value(value)
end

Instance Method Details

#item(value:, label: nil, icon: nil, disabled: false, aria_label: nil, **options, &block) ⇒ Object

Add an item to the toggle group



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/helpers/maquina_components/toggle_group_helper.rb', line 114

def item(value:, label: nil, icon: nil, disabled: false, aria_label: nil, **options, &block)
  is_pressed = @selected_values.include?(value.to_s)

  @items << {
    value: value,
    label: label,
    icon: icon,
    disabled: disabled || @disabled,
    aria_label: aria_label,
    pressed: is_pressed,
    options: options,
    block: block
  }
end

#to_htmlObject



129
130
131
# File 'app/helpers/maquina_components/toggle_group_helper.rb', line 129

def to_html
  @view.safe_join(@items.map { |item| render_item(item) })
end