Class: NitroKit::Checkbox

Inherits:
Component
  • Object
show all
Includes:
ActionView::Helpers::FormTagHelper
Defined in:
app/components/nitro_kit/checkbox.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: "1", label: nil, **attrs) ⇒ Checkbox

Returns a new instance of Checkbox.



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

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

  @name = name
  @label_text = label
end

Instance Attribute Details

#label_textObject (readonly)

Returns the value of attribute label_text.



12
13
14
# File 'app/components/nitro_kit/checkbox.rb', line 12

def label_text
  @label_text
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'app/components/nitro_kit/checkbox.rb', line 12

def name
  @name
end

#valueObject (readonly)

Returns the value of attribute value.



12
13
14
# File 'app/components/nitro_kit/checkbox.rb', line 12

def value
  @value
end

Instance Method Details

#view_templateObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/components/nitro_kit/checkbox.rb', line 18

def view_template
  div(class: merge(["isolate inline-flex items-center gap-2", attrs[:class]])) do
    label(class: "relative flex shrink-0") do
      input(
        **attrs,
        type: "checkbox",
        class: class_names(
          "peer appearance-none shadow-sm size-4 rounded-sm border text-foreground",
          "checked:bg-primary checked:border-primary",
          "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
        )
      )
      checkmark
    end

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