Class: UI::ToggleGroupItemComponent

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

Overview

ItemComponent - ViewComponent implementation

An individual toggle button within a Toggle Group. Inherits variant, size, and type from parent ToggleGroup.

Examples:

Basic usage

<%= render UI::ItemComponent.new(value: "left") { "Left" } %>

With icon

<%= render UI::ItemComponent.new(value: "bold") do %>
  <%= icon("bold") %>
<% end %>

Instance Method Summary collapse

Methods included from ToggleGroupItemBehavior

#toggle_group_item_classes, #toggle_group_item_html_attributes

Constructor Details

#initialize(value:, variant: nil, size: nil, pressed: false, disabled: false, classes: "", **attributes) ⇒ ToggleGroupItemComponent

Returns a new instance of ToggleGroupItemComponent.

Parameters:

  • value (String) —

    Unique identifier for this item (required)

  • variant (String) (defaults to: nil) —

    Visual style variant (inherits from parent if not specified)

  • size (String) (defaults to: nil) —

    Size variant (inherits from parent if not specified)

  • pressed (Boolean) (defaults to: false) —

    Whether the item is pressed/active

  • disabled (Boolean) (defaults to: false) —

    Whether the item is disabled

  • classes (String) (defaults to: "") —

    Additional CSS classes to merge

  • attributes (Hash) —

    Additional HTML attributes



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/view_components/ui/toggle_group_item_component.rb', line 25

def initialize(value:, variant: nil, size: nil, pressed: false, disabled: false, classes: "", **attributes)
  @value = value
  @variant = variant
  @size = size
  @pressed = pressed
  @disabled = disabled
  @classes = classes
  @attributes = attributes

  # These will be set from parent context if available
  @group_type = nil
  @spacing = nil
end

Instance Method Details

#before_render ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/view_components/ui/toggle_group_item_component.rb', line 39

def before_render
  # Try to inherit from parent ToggleGroup context
  if helpers.instance_variable_defined?(:@toggle_group_context)
    parent_context = helpers.instance_variable_get(:@toggle_group_context)
    @variant ||= parent_context[:variant]
    @size ||= parent_context[:size]
    @group_type = parent_context[:type]
    @spacing = parent_context[:spacing]

    # Check if this item should be pressed based on parent value
    parent_value = parent_context[:value]
    if parent_value
      @pressed = if @group_type == "multiple"
        Array(parent_value).include?(@value)
      else
        parent_value.to_s == @value.to_s
      end
    end
  end

  # Fallback defaults
  @variant ||= "default"
  @size ||= "default"
  @group_type ||= "single"
  @spacing ||= 0
end

#call ⇒ Object



66
67
68
69
70
71
# File 'app/view_components/ui/toggle_group_item_component.rb', line 66

def call
  attrs = toggle_group_item_html_attributes
  attrs[:data] = attrs[:data].merge(@attributes.fetch(:data, {}))

   :button, content, **attrs.merge(@attributes.except(:data))
end