Class: UI::ToggleGroupItemComponent
- Inherits:
-
ViewComponent::Base
- Object
- ViewComponent::Base
- UI::ToggleGroupItemComponent
- 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.
Instance Method Summary collapse
- #before_render ⇒ Object
- #call ⇒ Object
-
#initialize(value:, variant: nil, size: nil, pressed: false, disabled: false, classes: "", **attributes) ⇒ ToggleGroupItemComponent
constructor
A new instance of ToggleGroupItemComponent.
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.
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, {})) content_tag :button, content, **attrs.merge(@attributes.except(:data)) end |