Class: ShadcnPhlexcomponents::FormCheckboxGroup

Inherits:
Base
  • Object
show all
Includes:
FormHelpers
Defined in:
lib/shadcn_phlexcomponents/components/form/form_checkbox_group.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

#default_checked, #default_error, #default_value, #describedby, #hint, #hint_attributes, #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, collection: [], value_method: nil, text_method: nil, value: nil, name: nil, id: nil, label: nil, error: nil, hint: nil, disabled_items: nil, **attributes) ⇒ FormCheckboxGroup

Returns a new instance of FormCheckboxGroup.



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

def initialize(
  method = nil,
  model: false,
  object_name: nil,
  collection: [],
  value_method: nil,
  text_method: nil,
  value: nil,
  name: nil,
  id: nil,
  label: nil,
  error: nil,
  hint: nil,
  disabled_items: nil,
  **attributes
)
  @method = method
  @model = model
  @object_name = object_name

  @collection = if collection.first&.is_a?(Hash)
    convert_collection_hash_to_struct(collection, value_method: value_method, text_method: text_method)
  else
    collection
  end

  @value_method = value_method
  @text_method = text_method

  @value = value

  @model_value = if model
    model_collection = model.public_send(method)

    if model_collection.respond_to?(:map)
      model_collection.map { |item| item.public_send(value_method) }
    end
  end

  @name = name
  @id = id
  @label = label
  @error = default_error(error, method)
  @hint = hint
  @disabled_items = disabled_items
  @aria_id = "form-field-#{SecureRandom.hex(5)}"
  super(**attributes)
end

Instance Method Details

#aria_attributesObject



56
57
58
59
60
# File 'lib/shadcn_phlexcomponents/components/form/form_checkbox_group.rb', line 56

def aria_attributes
  attrs = super
  attrs[:labelledby] = "#{@aria_id}-label"
  attrs
end

#checkbox(**attributes) ⇒ Object



68
69
70
71
# File 'lib/shadcn_phlexcomponents/components/form/form_checkbox_group.rb', line 68

def checkbox(**attributes)
  @checkbox_attributes = attributes
  nil
end

#checkbox_label(**attributes) ⇒ Object



73
74
75
76
# File 'lib/shadcn_phlexcomponents/components/form/form_checkbox_group.rb', line 73

def checkbox_label(**attributes)
  @checkbox_label_attributes = attributes
  nil
end

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



62
63
64
65
66
# File 'lib/shadcn_phlexcomponents/components/form/form_checkbox_group.rb', line 62

def label_attributes(use_label_styles: false, **attributes)
  attrs = super(use_label_styles: use_label_styles, **attributes)
  attrs[:id] = "#{@aria_id}-label"
  attrs
end

#view_templateObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/shadcn_phlexcomponents/components/form/form_checkbox_group.rb', line 78

def view_template(&)
  vanish(&)

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

  FormField(data: label_and_hint_container_attributes) do
    render_label(&)

    CheckboxGroup(
      id: @id,
      name: @name,
      value: @value || @model_value || [],
      aria: aria_attributes,
      **@attributes,
    ) do |c|
      c.items(
        @collection,
        value_method: @value_method,
        text_method: @text_method,
        disabled_items: @disabled_items,
        id_prefix: @id,
      ) do
        if @checkbox_attributes
          c.checkbox(**@checkbox_attributes)
        end

        if @checkbox_label_attributes
          c.label(**@checkbox_label_attributes)
        end
      end
    end

    render_hint(&)
    render_error
  end
end