Class: NitroKit::ControlGroup

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

Constant Summary

Constants inherited from Component

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

Instance Method Summary collapse

Constructor Details

#initialize(label: nil, id: nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ ControlGroup

Returns a new instance of ControlGroup.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/components/nitro_kit/control_group.rb', line 5

def initialize(label: nil, id: nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil)
  unless label.nil? || (label.is_a?(String) && label.present?)
    raise ArgumentError, "ControlGroup label: must be nil or a non-blank String"
  end

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

Instance Method Details

#addon(text = nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
42
43
# File 'app/components/nitro_kit/control_group.rb', line 33

def addon(text = nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil, &block)
  raise ArgumentError, "ControlGroup addon must be declared inside the render block" unless @rendering
  raise ArgumentError, "ControlGroup addon accepts text or a block, not both" if text && block
  unless block || (text.is_a?(String) && text.present?)
    raise ArgumentError, "ControlGroup addon requires non-blank text or a block"
  end

  span(**slot_attributes(:addon, html:, aria:, data:, desperately_need_a_class:)) do
    text_or_block(text, &block)
  end
end

#view_templateObject



24
25
26
27
28
29
30
31
# File 'app/components/nitro_kit/control_group.rb', line 24

def view_template
  raise ArgumentError, "ControlGroup requires a block" unless block_given?

  @rendering = true
  div(**root_attributes) { yield(self) }
ensure
  @rendering = false
end