Class: UI::ToggleGroupItem

Inherits:
Phlex::HTML
  • Object
show all
Includes:
ToggleGroupItemBehavior
Defined in:
app/components/ui/toggle_group_item.rb

Overview

Item - Phlex implementation

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

Examples:

Basic usage

render UI::Item.new(value: "left") { "Left" }

With icon

render UI::Item.new(value: "bold") do
  render Icon.new("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) ⇒ ToggleGroupItem

Returns a new instance of ToggleGroupItem.

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/components/ui/toggle_group_item.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_templateObject



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
65
# File 'app/components/ui/toggle_group_item.rb', line 39

def before_template
  # Try to inherit from parent ToggleGroup context using thread-local
  # This works across both Phlex internal render and Rails render calls
  parent_context = Thread.current[:ui_toggle_group_context]
  if parent_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

#view_template(&block) ⇒ Object



67
68
69
70
71
# File 'app/components/ui/toggle_group_item.rb', line 67

def view_template(&block)
  button(**toggle_group_item_html_attributes.deep_merge(@attributes)) do
    yield if block_given?
  end
end