Class: Uchi::Ui::Form::Input::CollectionCheckboxes

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/uchi/ui/form/input/collection_checkboxes.rb

Overview

A component that wraps Rails’ collection_check_boxes form helper to render multiple checkboxes from a collection using Flowbite styling.

Example usage:

<%= render Uchi::Ui::Form::Input::CollectionCheckboxes.new(
  attribute: :tag_ids,
  collection: @tags,
  form: form,
  label: "Select Tags",
  value_method: :id,
  text_method: :name
) %>

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute:, collection:, form:, label: nil, hint: nil, value_method: :id, text_method: :name, disabled: false, options: {}) ⇒ CollectionCheckboxes

Returns a new instance of CollectionCheckboxes.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/components/uchi/ui/form/input/collection_checkboxes.rb', line 23

def initialize(
  attribute:,
  collection:,
  form:,
  label: nil,
  hint: nil,
  value_method: :id,
  text_method: :name,
  disabled: false,
  options: {}
)
  super()
  @attribute = attribute
  @collection = collection
  @form = form
  @label_text = label
  @hint_text = hint
  @value_method = value_method
  @text_method = text_method
  @disabled = disabled
  @options = options || {}
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



20
21
22
# File 'app/components/uchi/ui/form/input/collection_checkboxes.rb', line 20

def attribute
  @attribute
end

#collectionObject (readonly)

Returns the value of attribute collection.



20
21
22
# File 'app/components/uchi/ui/form/input/collection_checkboxes.rb', line 20

def collection
  @collection
end

#disabledObject (readonly)

Returns the value of attribute disabled.



20
21
22
# File 'app/components/uchi/ui/form/input/collection_checkboxes.rb', line 20

def disabled
  @disabled
end

#formObject (readonly)

Returns the value of attribute form.



20
21
22
# File 'app/components/uchi/ui/form/input/collection_checkboxes.rb', line 20

def form
  @form
end

#hint_textObject (readonly)

Returns the value of attribute hint_text.



20
21
22
# File 'app/components/uchi/ui/form/input/collection_checkboxes.rb', line 20

def hint_text
  @hint_text
end

#label_textObject (readonly)

Returns the value of attribute label_text.



20
21
22
# File 'app/components/uchi/ui/form/input/collection_checkboxes.rb', line 20

def label_text
  @label_text
end

#text_methodObject (readonly)

Returns the value of attribute text_method.



20
21
22
# File 'app/components/uchi/ui/form/input/collection_checkboxes.rb', line 20

def text_method
  @text_method
end

#value_methodObject (readonly)

Returns the value of attribute value_method.



20
21
22
# File 'app/components/uchi/ui/form/input/collection_checkboxes.rb', line 20

def value_method
  @value_method
end

Instance Method Details

#checkbox_classesObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/components/uchi/ui/form/input/collection_checkboxes.rb', line 94

def checkbox_classes
  base = ["w-4", "h-4", "rounded-sm", "focus:ring-2", "focus:ring-offset-2"]
  if disabled?
    base + ["text-blue-600", "bg-gray-100", "border-gray-300",
      "dark:bg-gray-700", "dark:border-gray-600"]
  elsif errors?
    base + ["text-red-600", "bg-red-50", "border-red-500",
      "focus:ring-red-500", "dark:focus:ring-red-600",
      "dark:bg-gray-700", "dark:border-red-500"]
  else
    base + ["text-blue-600", "bg-gray-100", "border-gray-300",
      "focus:ring-blue-500", "dark:focus:ring-blue-600",
      "dark:bg-gray-700", "dark:border-gray-600"]
  end
end

#checkbox_item_classesObject



90
91
92
# File 'app/components/uchi/ui/form/input/collection_checkboxes.rb', line 90

def checkbox_item_classes
  ["flex", "items-start", "mb-3"]
end

#checkbox_label_classesObject



110
111
112
113
114
115
116
# File 'app/components/uchi/ui/form/input/collection_checkboxes.rb', line 110

def checkbox_label_classes
  if disabled?
    ["ms-2", "text-sm", "font-medium", "text-gray-400", "dark:text-gray-500"]
  else
    ["ms-2", "text-sm", "font-medium", "text-gray-900", "dark:text-gray-300"]
  end
end

#collection_check_boxes_optionsObject



118
119
120
# File 'app/components/uchi/ui/form/input/collection_checkboxes.rb', line 118

def collection_check_boxes_options
  @options
end

#disabled?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'app/components/uchi/ui/form/input/collection_checkboxes.rb', line 58

def disabled?
  !!disabled
end

#errorsObject



50
51
52
# File 'app/components/uchi/ui/form/input/collection_checkboxes.rb', line 50

def errors
  @errors ||= object.errors[attribute] || []
end

#errors?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'app/components/uchi/ui/form/input/collection_checkboxes.rb', line 54

def errors?
  errors.any?
end

#hint_classesObject



81
82
83
84
85
86
87
88
# File 'app/components/uchi/ui/form/input/collection_checkboxes.rb', line 81

def hint_classes
  base = ["text-xs", "font-normal", "mt-1"]
  if disabled?
    base + ["text-gray-400", "dark:text-gray-500"]
  else
    base + ["text-gray-500", "dark:text-gray-300"]
  end
end

#label_classesObject



70
71
72
73
74
75
76
77
78
79
# File 'app/components/uchi/ui/form/input/collection_checkboxes.rb', line 70

def label_classes
  base = ["block", "mb-2", "text-sm", "font-medium"]
  if disabled?
    base + ["text-gray-400", "dark:text-gray-500"]
  elsif errors?
    base + ["text-red-700", "dark:text-red-500"]
  else
    base + ["text-gray-900", "dark:text-white"]
  end
end

#objectObject



46
47
48
# File 'app/components/uchi/ui/form/input/collection_checkboxes.rb', line 46

def object
  @object ||= form.object
end

#render_hint?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'app/components/uchi/ui/form/input/collection_checkboxes.rb', line 66

def render_hint?
  hint_text.present?
end

#render_label?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'app/components/uchi/ui/form/input/collection_checkboxes.rb', line 62

def render_label?
  label_text.present?
end