Class: NitroKit::CheckboxGroup

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

Constant Summary collapse

ORIENTATIONS =
%i[vertical horizontal].freeze
PRESENTATIONS =
%i[list cards].freeze

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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(legend:, options:, name:, value: [], id: nil, description: nil, orientation: :vertical, presentation: :list, unchecked_value: "", include_hidden: true, disabled: false, required: false, size: :md, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ CheckboxGroup

Returns a new instance of CheckboxGroup.

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
57
58
59
60
61
62
63
# File 'app/components/nitro_kit/checkbox_group.rb', line 10

def initialize(
  legend:,
  options:,
  name:,
  value: [],
  id: nil,
  description: nil,
  orientation: :vertical,
  presentation: :list,
  unchecked_value: "",
  include_hidden: true,
  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 = multiple_name(validate_text!(:name, name))
  @value = Array(value).map(&:to_s).freeze
  @id = id || deterministic_id(name)
  @description = validate_optional_text!(:description, description)
  @orientation = validate_choice!(:orientation, orientation, ORIENTATIONS)
  @presentation = validate_choice!(:presentation, presentation, PRESENTATIONS)
  @unchecked_value = unchecked_value
  @include_hidden = validate_boolean!(:include_hidden, include_hidden)
  @disabled = validate_boolean!(:disabled, disabled)
  @required = validate_boolean!(:required, required)
  @size = validate_choice!(:size, size, Checkbox::SIZES)

  raise ArgumentError, "options cannot be empty" if @options.empty?
  validate_unique_choices!
  if include_hidden && unchecked_value.nil?
    raise ArgumentError, "unchecked_value cannot be nil when include_hidden is true"
  end

  super(
    component: :checkbox_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.



65
66
67
# File 'app/components/nitro_kit/checkbox_group.rb', line 65

def description
  @description
end

#idObject (readonly)

Returns the value of attribute id.



65
66
67
# File 'app/components/nitro_kit/checkbox_group.rb', line 65

def id
  @id
end

#legendObject (readonly) Also known as: html_legend

Returns the value of attribute legend.



65
66
67
# File 'app/components/nitro_kit/checkbox_group.rb', line 65

def legend
  @legend
end

#nameObject (readonly)

Returns the value of attribute name.



65
66
67
# File 'app/components/nitro_kit/checkbox_group.rb', line 65

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



65
66
67
# File 'app/components/nitro_kit/checkbox_group.rb', line 65

def options
  @options
end

#orientationObject (readonly)

Returns the value of attribute orientation.



65
66
67
# File 'app/components/nitro_kit/checkbox_group.rb', line 65

def orientation
  @orientation
end

#presentationObject (readonly)

Returns the value of attribute presentation.



65
66
67
# File 'app/components/nitro_kit/checkbox_group.rb', line 65

def presentation
  @presentation
end

#sizeObject (readonly)

Returns the value of attribute size.



65
66
67
# File 'app/components/nitro_kit/checkbox_group.rb', line 65

def size
  @size
end

#valueObject (readonly)

Returns the value of attribute value.



65
66
67
# File 'app/components/nitro_kit/checkbox_group.rb', line 65

def value
  @value
end

Instance Method Details

#view_templateObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/components/nitro_kit/checkbox_group.rb', line 67

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

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