Class: PhlexKit::ToggleGroup

Inherits:
BaseComponent show all
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

Methods inherited from BaseComponent

#on

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.

Raises:

  • (ArgumentError)


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

#item_contextObject



29
30
31
32
# 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 }
end

#ToggleGroupItem(**kwargs, &block) ⇒ Object



34
35
36
# File 'app/components/phlex_kit/toggle_group/toggle_group.rb', line 34

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)
    render_hidden_inputs
  end
end