Class: NitroKit::Checkbox

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

Constant Summary collapse

SIZES =
%i[md lg].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(label: nil, description: nil, id: nil, name: nil, value: "1", unchecked_value: "0", include_hidden: true, checked: false, indeterminate: false, disabled: false, required: false, invalid: false, size: :md, html: {}, aria: {}, data: {}, control_html: {}, control_aria: {}, control_data: {}, desperately_need_a_class: nil) ⇒ Checkbox

Returns a new instance of Checkbox.



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

def initialize(
  label: nil,
  description: nil,
  id: nil,
  name: nil,
  value: "1",
  unchecked_value: "0",
  include_hidden: true,
  checked: false,
  indeterminate: 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
  @unchecked_value = unchecked_value
  @include_hidden = validate_boolean!(:include_hidden, include_hidden)
  @checked = validate_boolean!(:checked, checked)
  @indeterminate = validate_boolean!(:indeterminate, indeterminate)
  @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 include_hidden && unchecked_value.nil?
    raise ArgumentError, "unchecked_value cannot be nil when include_hidden is true"
  end
  if description && (!id.is_a?(String) || id.strip.empty?)
    raise ArgumentError, "checkbox requires a non-blank String id when description is present"
  end

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

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



66
67
68
# File 'app/components/nitro_kit/checkbox.rb', line 66

def description
  @description
end

#idObject (readonly)

Returns the value of attribute id.



66
67
68
# File 'app/components/nitro_kit/checkbox.rb', line 66

def id
  @id
end

#labelObject (readonly)

Returns the value of attribute label.



66
67
68
# File 'app/components/nitro_kit/checkbox.rb', line 66

def label
  @label
end

#nameObject (readonly)

Returns the value of attribute name.



66
67
68
# File 'app/components/nitro_kit/checkbox.rb', line 66

def name
  @name
end

#sizeObject (readonly)

Returns the value of attribute size.



66
67
68
# File 'app/components/nitro_kit/checkbox.rb', line 66

def size
  @size
end

#unchecked_valueObject (readonly)

Returns the value of attribute unchecked_value.



66
67
68
# File 'app/components/nitro_kit/checkbox.rb', line 66

def unchecked_value
  @unchecked_value
end

#valueObject (readonly)

Returns the value of attribute value.



66
67
68
# File 'app/components/nitro_kit/checkbox.rb', line 66

def value
  @value
end

Instance Method Details

#view_template(&block) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/components/nitro_kit/checkbox.rb', line 68

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

  div(**root_attributes) do
    render_hidden_control

    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