Class: PhlexKit::ToggleGroup
- Inherits:
-
BaseComponent
- Object
- Phlex::HTML
- BaseComponent
- PhlexKit::ToggleGroup
- Defined in:
- app/components/phlex_kit/toggle_group/toggle_group.rb
Overview
A set of related toggles (single- or multi-select). Ported from ruby_ui's RubyUI::ToggleGroup. Yields itself so items are declared with the group's shared context: ToggleGroup(type:) { |g| g.ToggleGroupItem(value:) { … } }.
Constant Summary collapse
- VALID_TYPES =
%i[single multiple].freeze
Instance Method Summary collapse
-
#claim_tab_stop(item_disabled) ⇒ Object
Roving-tabindex initial stop (single-type, nothing selected): the FIRST enabled item claims tabindex="0" server-side, mirroring what toggle_group_controller.js's reconcile() computes at runtime (
pressed || enabledItems()[0]). -
#initialize(type: :single, name: nil, value: nil, variant: :default, size: :default, disabled: false, spacing: 0, orientation: :horizontal, **attrs) ⇒ ToggleGroup
constructor
A new instance of ToggleGroup.
- #item_context ⇒ Object
- #ToggleGroupItem(**kwargs, &block) ⇒ Object
- #view_template(&block) ⇒ Object
Methods inherited from BaseComponent
Constructor Details
#initialize(type: :single, name: nil, value: nil, variant: :default, size: :default, disabled: false, spacing: 0, orientation: :horizontal, **attrs) ⇒ ToggleGroup
Returns a new instance of ToggleGroup.
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'app/components/phlex_kit/toggle_group/toggle_group.rb', line 8 def initialize(type: :single, name: nil, value: nil, variant: :default, size: :default, disabled: false, spacing: 0, orientation: :horizontal, **attrs) @type = type.to_sym raise ArgumentError, "type must be :single or :multiple" unless VALID_TYPES.include?(@type) @name = name @value = value @variant = variant.to_sym @size = size.to_sym @disabled = disabled @spacing = spacing.to_i @orientation = orientation.to_sym @attrs = attrs end |
Instance Method Details
#claim_tab_stop(item_disabled) ⇒ Object
Roving-tabindex initial stop (single-type, nothing selected): the FIRST
enabled item claims tabindex="0" server-side, mirroring what
toggle_group_controller.js's reconcile() computes at runtime
(pressed || enabledItems()[0]). Items call this once each, in render
order, as they're constructed — a single-threaded sequential claim, not
a lookahead over siblings not yet rendered.
45 46 47 48 49 50 51 |
# File 'app/components/phlex_kit/toggle_group/toggle_group.rb', line 45 def claim_tab_stop(item_disabled) return false unless selected_values.empty? return false if item_disabled return false if @tab_stop_claimed @tab_stop_claimed = true true end |
#item_context ⇒ Object
29 30 31 32 33 |
# File 'app/components/phlex_kit/toggle_group/toggle_group.rb', line 29 def item_context { type: @type, variant: @variant, size: @size, disabled: @disabled, selected_values: selected_values, spacing: @spacing, orientation: @orientation, group: self } end |
#ToggleGroupItem(**kwargs, &block) ⇒ Object
35 36 37 |
# File 'app/components/phlex_kit/toggle_group/toggle_group.rb', line 35 def ToggleGroupItem(**kwargs, &block) render PhlexKit::ToggleGroupItem.new(group_context: item_context, **kwargs), &block end |
#view_template(&block) ⇒ Object
22 23 24 25 26 27 |
# File 'app/components/phlex_kit/toggle_group/toggle_group.rb', line 22 def view_template(&block) div(**mix(group_default_attrs, @attrs)) do yield(self) if block render_hidden_inputs end end |