Class: NitroKit::Checkbox

Inherits:
Component
  • Object
show all
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

#builder, from_template

Constructor Details

#initialize(label: nil, id: nil, wrapper: {}, **attrs) ⇒ Checkbox

Returns a new instance of Checkbox.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'app/components/nitro_kit/checkbox.rb', line 5

def initialize(label: nil, id: nil, wrapper: {}, **attrs)
  @id = id || "nk--" + SecureRandom.hex(4)
  @label = label
  @wrapper = wrapper

  super(
    attrs,
    id: @id,
    type: "checkbox",
    class: input_class
  )
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



20
21
22
# File 'app/components/nitro_kit/checkbox.rb', line 20

def id
  @id
end

#labelObject (readonly) Also known as: html_label

Returns the value of attribute label.



20
21
22
# File 'app/components/nitro_kit/checkbox.rb', line 20

def label
  @label
end

#wrapperObject (readonly)

Returns the value of attribute wrapper.



20
21
22
# File 'app/components/nitro_kit/checkbox.rb', line 20

def wrapper
  @wrapper
end

Instance Method Details

#view_templateObject



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

def view_template
  div(**mattr(wrapper, class: wrapper_class)) do
    html_label(
      class: "inline-grid *:[grid-area:1/1] shrink-0 place-items-center group/checkbox has-checked:not-has-indeterminate:[&>[data-check]]:visible has-indeterminate:[&>[data-indeterminate]]:visible"
    ) do
      input(**attrs)
      checkmark
      dash
    end

    if label.present? || block_given?
      render(Label.new(for: id)) do
        label || (block_given? ? yield : nil)
      end
    end
  end
end