Class: UI::ToggleGroupItem
- Inherits:
-
Phlex::HTML
- Object
- Phlex::HTML
- UI::ToggleGroupItem
- 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.
Instance Method Summary collapse
- #before_template ⇒ Object
-
#initialize(value:, variant: nil, size: nil, pressed: false, disabled: false, classes: "", **attributes) ⇒ ToggleGroupItem
constructor
A new instance of ToggleGroupItem.
- #view_template(&block) ⇒ Object
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.
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_template ⇒ 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 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) (**toggle_group_item_html_attributes.deep_merge(@attributes)) do yield if block_given? end end |