Class: NitroKit::RadioButton

Inherits:
Component
  • Object
show all
Includes:
ActionView::Helpers::FormTagHelper
Defined in:
app/components/nitro_kit/radio_button.rb

Instance Attribute Summary collapse

Attributes inherited from Component

#attrs

Instance Method Summary collapse

Methods inherited from Component

#data_merge, merge, #merge

Constructor Details

#initialize(name, value:, label: nil, **attrs) ⇒ RadioButton

Returns a new instance of RadioButton.



5
6
7
8
9
10
11
12
# File 'app/components/nitro_kit/radio_button.rb', line 5

def initialize(name, value:, label: nil, **attrs)
  super(**attrs)

  @name = name
  @value = value
  @label_text = label
  @id = "#{sanitize_to_id(name)}_#{sanitize_to_id(value)}"
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



14
15
16
# File 'app/components/nitro_kit/radio_button.rb', line 14

def id
  @id
end

#label_textObject (readonly)

Returns the value of attribute label_text.



14
15
16
# File 'app/components/nitro_kit/radio_button.rb', line 14

def label_text
  @label_text
end

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'app/components/nitro_kit/radio_button.rb', line 14

def name
  @name
end

#valueObject (readonly)

Returns the value of attribute value.



14
15
16
# File 'app/components/nitro_kit/radio_button.rb', line 14

def value
  @value
end

Instance Method Details

#view_templateObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/components/nitro_kit/radio_button.rb', line 21

def view_template
  div(class: merge("inline-flex items-center gap-2", attrs[:class])) do
    label(class: "grid grid-cols-1 place-items-center shrink-0") do
      input(
        **attrs,
        type: "radio",
        name:,
        value:,
        id:,
        class: class_names(
          "peer row-start-1 col-start-1",
          "appearance-none size-5 shadow-sm rounded-full border text-foreground bg-background",
          "[&[aria-checked='true']]:bg-primary",
          "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
        )
      )
      dot
    end

    if label_text.present?
      render(Label.new(for: id)) { label_text }
    end
  end
end