Class: NitroKit::RadioButton

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

Constant Summary collapse

SIZES =
%i[md lg].freeze

Constants inherited from Component

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label: nil, description: nil, id: nil, name: nil, value: "1", checked: false, disabled: false, required: false, invalid: false, size: :md, html: {}, aria: {}, data: {}, control_html: {}, control_aria: {}, control_data: {}, desperately_need_a_class: nil) ⇒ RadioButton

Returns a new instance of RadioButton.



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
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.rb', line 7

def initialize(
  label: nil,
  description: nil,
  id: nil,
  name: nil,
  value: "1",
  checked: false,
  disabled: false,
  required: false,
  invalid: false,
  size: :md,
  html: {},
  aria: {},
  data: {},
  control_html: {},
  control_aria: {},
  control_data: {},
  desperately_need_a_class: nil
)
  @label = validate_optional_text!(:label, label)
  @description = validate_optional_text!(:description, description)
  @id = id
  @name = name
  @value = value
  @checked = validate_boolean!(:checked, checked)
  @disabled = validate_boolean!(:disabled, disabled)
  @required = validate_boolean!(:required, required)
  @invalid = validate_boolean!(:invalid, invalid)
  @size = validate_choice!(:size, size, SIZES)
  @control_html = control_html
  @control_aria = control_aria
  @control_data = control_data

  if description && (!id.is_a?(String) || id.strip.empty?)
    raise ArgumentError, "radio button requires a non-blank String id when description is present"
  end

  super(
    component: :radio_button,
    size: @size,
    attributes: {
      data: { disabled: @disabled ? "true" : nil }.compact
    },
    html:,
    aria:,
    data:,
    desperately_need_a_class:
  )
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



57
58
59
# File 'app/components/nitro_kit/radio_button.rb', line 57

def description
  @description
end

#idObject (readonly)

Returns the value of attribute id.



57
58
59
# File 'app/components/nitro_kit/radio_button.rb', line 57

def id
  @id
end

#labelObject (readonly)

Returns the value of attribute label.



57
58
59
# File 'app/components/nitro_kit/radio_button.rb', line 57

def label
  @label
end

#nameObject (readonly)

Returns the value of attribute name.



57
58
59
# File 'app/components/nitro_kit/radio_button.rb', line 57

def name
  @name
end

#sizeObject (readonly)

Returns the value of attribute size.



57
58
59
# File 'app/components/nitro_kit/radio_button.rb', line 57

def size
  @size
end

#valueObject (readonly)

Returns the value of attribute value.



57
58
59
# File 'app/components/nitro_kit/radio_button.rb', line 57

def value
  @value
end

Instance Method Details

#view_template(&block) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/components/nitro_kit/radio_button.rb', line 59

def view_template(&block)
  require_accessible_name!(&block)

  div(**root_attributes) do
    if label.nil? && !block
      render_control
      span(**slot_attributes(:indicator), aria: { hidden: "true" })
    else
      render_in_slot(Label.new(for: id), :label) do
        render_control
        span(**slot_attributes(:indicator), aria: { hidden: "true" })
        span(**slot_attributes(:content)) do
          span(**slot_attributes(:label_text)) { text_or_block(label, &block) }
          span(**slot_attributes(:description, attributes: { id: description_id })) { plain(description) } if description
        end
      end
    end
  end
end