Class: ShadcnPhlexcomponents::Checkbox

Inherits:
Base
  • Object
show all
Defined in:
lib/shadcn_phlexcomponents/components/checkbox.rb

Constant Summary

Constants inherited from Base

Base::SANITIZER_ALLOWED_ATTRIBUTES, Base::SANITIZER_ALLOWED_TAGS, Base::TAILWIND_MERGER

Instance Method Summary collapse

Methods inherited from Base

#before_template, #convert_collection_hash_to_struct, #find_as_child, #icon, #item_disabled?, #merge_default_attributes, #merged_as_child_attributes, #nokogiri_attributes_to_hash, #overlay, #sanitize_as_child

Constructor Details

#initialize(name: nil, value: "1", unchecked_value: "0", checked: false, include_hidden: true, **attributes) ⇒ Checkbox

Returns a new instance of Checkbox.



21
22
23
24
25
26
27
28
29
# File 'lib/shadcn_phlexcomponents/components/checkbox.rb', line 21

def initialize(name: nil, value: "1", unchecked_value: "0", checked: false, include_hidden: true,
  **attributes)
  @name = name
  @value = value
  @unchecked_value = unchecked_value
  @checked = checked
  @include_hidden = include_hidden
  super(**attributes)
end

Instance Method Details

#default_attributes ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/shadcn_phlexcomponents/components/checkbox.rb', line 31

def default_attributes
  {
    type: "button",
    role: "checkbox",
    aria: {
      checked: @checked.to_s,
    },
    data: {
      checked: @checked.to_s,
      controller: "checkbox",
      action: "        click->checkbox#toggle\n        keydown.enter->checkbox#preventDefault\n      HEREDOC\n      checkbox_is_checked_value: @checked,\n    },\n  }\nend\n",

#view_template ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/shadcn_phlexcomponents/components/checkbox.rb', line 50

def view_template(&)
  button(**@attributes) do
    CheckboxIndicator()

    if @include_hidden
      input(name: @name, type: "hidden", value: @unchecked_value, autocomplete: "off")
    end

    input(
      type: "checkbox",
      value: @value,
      class: "-translate-x-full pointer-events-none absolute top-0 left-0 size-4 opacity-0",
      name: @name,
      tabindex: -1,
      checked: @checked,
      aria: { hidden: true },
      data: {
        checkbox_target: "input",
      },
    )
  end
end