Class: NitroKit::RadioButtonGroup

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

Instance Attribute Summary collapse

Attributes inherited from Component

#attrs

Instance Method Summary collapse

Methods inherited from Component

#builder, from_template

Constructor Details

#initialize(options_arg = nil, options: [], name: nil, value: nil, **attrs) ⇒ RadioButtonGroup

Returns a new instance of RadioButtonGroup.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'app/components/nitro_kit/radio_button_group.rb', line 5

def initialize(options_arg = nil, options: [], name: nil, value: nil, **attrs)
  @options = options_arg || options

  @name = name
  @group_value = value

  super(
    attrs,
    name: @name,
    class: "flex items-start flex-col gap-2"
  )
end

Instance Attribute Details

#group_valueObject (readonly)

Returns the value of attribute group_value.



18
19
20
# File 'app/components/nitro_kit/radio_button_group.rb', line 18

def group_value
  @group_value
end

#nameObject (readonly)

Returns the value of attribute name.



18
19
20
# File 'app/components/nitro_kit/radio_button_group.rb', line 18

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



18
19
20
# File 'app/components/nitro_kit/radio_button_group.rb', line 18

def options
  @options
end

Instance Method Details

#item(text = nil, value_as_arg = nil, value: nil, **attrs, &block) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/components/nitro_kit/radio_button_group.rb', line 38

def item(text = nil, value_as_arg = nil, value: nil, **attrs, &block)
  builder do
    value ||= value_as_arg

    render(
      RadioButton.new(
        **mattr(
          attrs,
          name: attrs.fetch(:name, name),
          value:,
          checked: group_value.presence == value
        )
      )
    ) do
      text_or_block(text, &block)
    end
  end
end

#title(text = nil, **attrs, &block) ⇒ Object



30
31
32
33
34
35
36
# File 'app/components/nitro_kit/radio_button_group.rb', line 30

def title(text = nil, **attrs, &block)
  builder do
    render(Label.new(**attrs)) do
      text_or_block(text, &block)
    end
  end
end

#view_templateObject



20
21
22
23
24
25
26
27
28
# File 'app/components/nitro_kit/radio_button_group.rb', line 20

def view_template
  div(**attrs) do
    if block_given?
      yield
    else
      options.map { |o| item(*o) }
    end
  end
end