Class: NitroKit::RadioButtonGroup

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

Constant Summary collapse

ORIENTATIONS =
%i[vertical horizontal].freeze
PRESENTATIONS =
%i[list cards segmented].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(legend:, options:, name:, value: nil, id: nil, description: nil, orientation: :vertical, presentation: :list, disabled: false, required: false, size: :md, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ RadioButtonGroup

Returns a new instance of RadioButtonGroup.

Raises:

  • (ArgumentError)


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
56
# File 'app/components/nitro_kit/radio_button_group.rb', line 10

def initialize(
  legend:,
  options:,
  name:,
  value: nil,
  id: nil,
  description: nil,
  orientation: :vertical,
  presentation: :list,
  disabled: false,
  required: false,
  size: :md,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil
)
  @legend = validate_text!(:legend, legend)
  @options = Array(options).map { |choice| Choice.coerce(choice) }.freeze
  @name = validate_text!(:name, name)
  @value = value
  @id = id || deterministic_id(name)
  @description = validate_optional_text!(:description, description)
  @orientation = validate_choice!(:orientation, orientation, ORIENTATIONS)
  @presentation = validate_choice!(:presentation, presentation, PRESENTATIONS)
  @disabled = validate_boolean!(:disabled, disabled)
  @required = validate_boolean!(:required, required)
  @size = validate_choice!(:size, size, RadioButton::SIZES)

  raise ArgumentError, "options cannot be empty" if @options.empty?
  validate_unique_choices!

  super(
    component: :radio_button_group,
    size: @size,
    attributes: {
      id: @id,
      disabled: @disabled,
      aria: { required: @required ? "true" : nil }.compact,
      data: { orientation: @orientation, presentation: @presentation }
    }.compact,
    html:,
    aria:,
    data:,
    desperately_need_a_class:
  )
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



58
59
60
# File 'app/components/nitro_kit/radio_button_group.rb', line 58

def description
  @description
end

#idObject (readonly)

Returns the value of attribute id.



58
59
60
# File 'app/components/nitro_kit/radio_button_group.rb', line 58

def id
  @id
end

#legendObject (readonly) Also known as: html_legend

Returns the value of attribute legend.



58
59
60
# File 'app/components/nitro_kit/radio_button_group.rb', line 58

def legend
  @legend
end

#nameObject (readonly)

Returns the value of attribute name.



58
59
60
# File 'app/components/nitro_kit/radio_button_group.rb', line 58

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



58
59
60
# File 'app/components/nitro_kit/radio_button_group.rb', line 58

def options
  @options
end

#orientationObject (readonly)

Returns the value of attribute orientation.



58
59
60
# File 'app/components/nitro_kit/radio_button_group.rb', line 58

def orientation
  @orientation
end

#presentationObject (readonly)

Returns the value of attribute presentation.



58
59
60
# File 'app/components/nitro_kit/radio_button_group.rb', line 58

def presentation
  @presentation
end

#sizeObject (readonly)

Returns the value of attribute size.



58
59
60
# File 'app/components/nitro_kit/radio_button_group.rb', line 58

def size
  @size
end

#valueObject (readonly)

Returns the value of attribute value.



58
59
60
# File 'app/components/nitro_kit/radio_button_group.rb', line 58

def value
  @value
end

Instance Method Details

#view_templateObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/components/nitro_kit/radio_button_group.rb', line 60

def view_template
  fieldset(**root_attributes) do
    html_legend(**slot_attributes(:legend)) { plain(legend) }
    if description
      p(**slot_attributes(:description, attributes: { id: description_id }.compact)) do
        plain(description.to_s)
      end
    end

    div(**slot_attributes(:choices)) do
      options.each_with_index { |choice, index| render_choice(choice, index) }
    end
  end
end