Class: UI::SwitchComponent

Inherits:
ViewComponent::Base
  • Object
show all
Includes:
SwitchBehavior
Defined in:
app/view_components/ui/switch_component.rb

Instance Method Summary collapse

Methods included from SwitchBehavior

#switch_classes, #switch_html_attributes, #switch_thumb_classes

Constructor Details

#initialize(checked: false, disabled: false, name: nil, id: nil, value: "1", **attributes) ⇒ SwitchComponent

Returns a new instance of SwitchComponent.



6
7
8
9
10
11
12
13
# File 'app/view_components/ui/switch_component.rb', line 6

def initialize(checked: false, disabled: false, name: nil, id: nil, value: "1", **attributes)
  @checked = checked
  @disabled = disabled
  @name = name
  @id = id
  @value = value
  @attributes = attributes
end

Instance Method Details

#call ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/view_components/ui/switch_component.rb', line 15

def call
  attrs = switch_html_attributes.deep_merge(@attributes.except(:class))
  attrs[:id] = @id if @id.present?

  # Merge Tailwind classes intelligently
  attrs[:class] = TailwindMerge::Merger.new.merge([
    switch_html_attributes[:class],
    @attributes[:class]
  ].compact.join(" "))

   :button, **attrs do
    safe_join([
      render_thumb,
      render_hidden_input
    ].compact)
  end
end