Class: ShadcnPhlexcomponents::FormCheckbox

Inherits:
Base
  • Object
show all
Includes:
FormHelpers
Defined in:
lib/shadcn_phlexcomponents/components/form/form_checkbox.rb

Constant Summary

Constants inherited from Base

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

Instance Method Summary collapse

Methods included from FormHelpers

#aria_attributes, #default_checked, #default_error, #default_value, #describedby, #hint, #label, #label_and_hint_container_attributes, #render_error, #render_hint, #render_label

Methods inherited from Base

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

Constructor Details

#initialize(method = nil, model: false, object_name: nil, value: nil, name: nil, id: nil, label: nil, error: nil, hint: nil, checked: nil, **attributes) ⇒ FormCheckbox

Returns a new instance of FormCheckbox.



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
# File 'lib/shadcn_phlexcomponents/components/form/form_checkbox.rb', line 7

def initialize(
  method = nil,
  model: false,
  object_name: nil,
  value: nil,
  name: nil,
  id: nil,
  label: nil,
  error: nil,
  hint: nil,
  checked: nil,
  **attributes
)
  @method = method
  @model = model
  @object_name = object_name
  @value = value || "1"
  @name = name
  @id = id
  @label = label
  @error = default_error(error, method)
  @hint = hint
  @aria_id = "form-field-#{SecureRandom.hex(5)}"
  @checked = default_checked(checked, method)
  super(**attributes)
end

Instance Method Details

#hint_attributes(**attributes) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/shadcn_phlexcomponents/components/form/form_checkbox.rb', line 44

def hint_attributes(**attributes)
  attributes[:class] = [
    "ml-6",
    attributes[:class],
  ].compact.join(" ")
  attributes
end

#label_attributes(use_label_styles: false, **attributes) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/shadcn_phlexcomponents/components/form/form_checkbox.rb', line 34

def label_attributes(use_label_styles: false, **attributes)
  attributes[:class] = [
    use_label_styles ? Label.new.class_variants : nil,
    "ml-6",
    attributes[:class],
  ].compact.join(" ")
  attributes[:for] ||= @id
  attributes
end

#view_templateObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/shadcn_phlexcomponents/components/form/form_checkbox.rb', line 52

def view_template(&)
  vanish(&)

  @id ||= field_id(@object_name, @method)
  @name ||= field_name(@object_name, @method)

  FormField do
    div(class: "flex items-top space-x-2") do
      div(class: "grid gap-1.5 relative", data: label_and_hint_container_attributes) do
        @attributes[:class] = "#{@attributes[:class]} -mt-[1.5px] absolute top-0 left-0"

        Checkbox(
          id: @id,
          name: @name,
          value: @value,
          checked: @checked,
          aria: aria_attributes,
          disabled: @disabled,
          **@attributes,
        )

        render_label(&)
        render_hint(&)
      end
    end

    render_error
  end
end