Class: ShadcnPhlexcomponents::CheckboxGroup

Inherits:
Base
  • Object
show all
Defined in:
lib/shadcn_phlexcomponents/components/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 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:, value: [], include_hidden: true, **attributes) ⇒ CheckboxGroup

Returns a new instance of CheckboxGroup.



14
15
16
17
18
19
# File 'lib/shadcn_phlexcomponents/components/checkbox_group.rb', line 14

def initialize(name:, value: [], include_hidden: true, **attributes)
  @name = name
  @value = value
  @include_hidden = include_hidden
  super(**attributes)
end

Instance Method Details

#checkbox(**attributes) ⇒ Object



26
27
28
29
# File 'lib/shadcn_phlexcomponents/components/checkbox_group.rb', line 26

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

#default_attributesObject



65
66
67
68
69
# File 'lib/shadcn_phlexcomponents/components/checkbox_group.rb', line 65

def default_attributes
  {
    role: "group",
  }
end

#items(collection, value_method:, text_method:, container_class: nil, disabled_items: nil, id_prefix: nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/shadcn_phlexcomponents/components/checkbox_group.rb', line 31

def items(collection, value_method:, text_method:, container_class: nil, disabled_items: nil, id_prefix: nil, &)
  vanish(&)

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

  collection.each do |item|
    value = item.public_send(value_method)
    text = item.public_send(text_method)

    id = if id_prefix
      "#{id_prefix.parameterize.underscore}_#{value}"
    else
      "#{@name.parameterize.underscore}_#{value}"
    end

    CheckboxGroupItemContainer(class: container_class) do
      Checkbox(
        name: "#{@name}[]",
        id: id,
        value: value,
        checked: @value.include?(value),
        include_hidden: false,
        disabled: item_disabled?(disabled_items, value),
        **@checkbox_attributes,
      )
      Label(for: id, **@label_attributes) { text }
    end
  end

  nil
end

#label(**attributes) ⇒ Object



21
22
23
24
# File 'lib/shadcn_phlexcomponents/components/checkbox_group.rb', line 21

def label(**attributes)
  @label_attributes = attributes
  nil
end

#view_templateObject



71
72
73
74
75
76
77
78
79
# File 'lib/shadcn_phlexcomponents/components/checkbox_group.rb', line 71

def view_template(&)
  div(**@attributes) do
    yield

    if @include_hidden
      input(type: "hidden", name: "#{@name}[]", autocomplete: "off")
    end
  end
end