Class: RubyUI::ToggleGroup

Inherits:
Base
  • Object
show all
Defined in:
lib/ruby_ui/toggle_group/toggle_group.rb

Constant Summary collapse

SPACING_GAP =
{0 => nil, 1 => "gap-1", 2 => "gap-2", 3 => "gap-3", 4 => "gap-4"}.freeze
VALID_TYPES =
[:single, :multiple].freeze
VALID_ORIENTATIONS =
[:horizontal, :vertical].freeze

Constants inherited from Base

Base::TAILWIND_MERGER

Instance Attribute Summary

Attributes inherited from Base

#attrs

Instance Method Summary collapse

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)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ruby_ui/toggle_group/toggle_group.rb', line 9

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)

  @orientation = orientation.to_sym
  raise ArgumentError, "orientation must be :horizontal or :vertical" unless VALID_ORIENTATIONS.include?(@orientation)

  raise ArgumentError, "spacing must be an Integer 0..4" unless spacing.is_a?(Integer) && (0..4).cover?(spacing)

  @name = name
  @value = value
  @variant = variant.to_sym
  @size = size.to_sym
  @disabled = disabled
  @spacing = spacing
  super(**attrs)
end

Instance Method Details

#item_contextObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ruby_ui/toggle_group/toggle_group.rb', line 44

def item_context
  {
    type: @type,
    variant: @variant,
    size: @size,
    disabled: @disabled,
    selected_values: selected_values,
    spacing: @spacing,
    orientation: @orientation
  }
end

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



56
57
58
# File 'lib/ruby_ui/toggle_group/toggle_group.rb', line 56

def ToggleGroupItem(**kwargs, &block)
  render RubyUI::ToggleGroupItem.new(group_context: item_context, **kwargs), &block
end

#view_template(&block) ⇒ Object



37
38
39
40
41
42
# File 'lib/ruby_ui/toggle_group/toggle_group.rb', line 37

def view_template(&block)
  div(**attrs) do
    yield(self)
    render_hidden_inputs
  end
end