Class: NitroKit::ButtonGroup

Inherits:
Component
  • Object
show all
Defined in:
app/components/nitro_kit/button_group.rb

Defined Under Namespace

Classes: Entry

Constant Summary

Constants inherited from Component

Component::ADDITIVE_DATA_ATTRIBUTES, Component::COMPONENT_OWNED_DATA_ATTRIBUTES, Component::FORBIDDEN_ATTRIBUTES, Component::RESERVED_DATA_ATTRIBUTES

Instance Method Summary collapse

Constructor Details

#initialize(buttons: [], label: nil, variant: nil, size: nil, id: nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ ButtonGroup

Returns a new instance of ButtonGroup.

Raises:

  • (ArgumentError)


7
8
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
36
37
# File 'app/components/nitro_kit/button_group.rb', line 7

def initialize(
  buttons: [],
  label: nil,
  variant: nil,
  size: nil,
  id: nil,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil
)
  raise ArgumentError, "buttons must be an Array" unless buttons.is_a?(Array)
  unless label.nil? || (label.is_a?(String) && !label.strip.empty?)
    raise ArgumentError, "label must be a non-blank String or nil"
  end
  raise ArgumentError, "aria must be a Hash" unless aria.is_a?(Hash)

  @variant = variant && validate_choice!(:variant, variant, Button::VARIANTS)
  @size = size && validate_choice!(:size, size, Button::SIZES)
  @entries = []
  buttons.each { |button| add(button) }

  super(
    component: :button_group,
    attributes: { id:, role: "group" },
    html:,
    aria: label ? aria.merge(label:) : aria,
    data:,
    desperately_need_a_class:
  )
end

Instance Method Details

#add(button, &content) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'app/components/nitro_kit/button_group.rb', line 50

def add(button, &content)
  unless button.is_a?(NitroKit::Button)
    raise ArgumentError, "ButtonGroup accepts only NitroKit::Button children"
  end
  if @entries.any? { |entry| entry.button.equal?(button) }
    raise ArgumentError, "ButtonGroup cannot contain the same Button twice"
  end

  @entries << Entry.new(button:, content:)
  nil
end

#button(*arguments, **options, &content) ⇒ Object

Declares a member with Button's own signature. Arguments are forwarded verbatim so the group can never restate a stale copy of Button's keywords; unknown keywords raise from Button itself. Group-level variant: and size: fill in only where the member stays silent.



66
67
68
# File 'app/components/nitro_kit/button_group.rb', line 66

def button(*arguments, **options, &content)
  add(Button.new(*arguments, **member_defaults.merge(options)), &content)
end

#view_template {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
48
# File 'app/components/nitro_kit/button_group.rb', line 39

def view_template
  yield self if block_given?
  raise ArgumentError, "ButtonGroup requires at least one Button" if @entries.empty?

  div(**root_attributes) do
    @entries.each do |entry|
      render_in_slot(entry.button, :button, &entry.content)
    end
  end
end