Class: Uchi::Ui::Form::Input::CollectionCheckboxes
- Inherits:
-
ViewComponent::Base
- Object
- ViewComponent::Base
- Uchi::Ui::Form::Input::CollectionCheckboxes
- 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
-
#attribute ⇒ Object
readonly
Returns the value of attribute attribute.
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#disabled ⇒ Object
readonly
Returns the value of attribute disabled.
-
#form ⇒ Object
readonly
Returns the value of attribute form.
-
#hint_text ⇒ Object
readonly
Returns the value of attribute hint_text.
-
#label_text ⇒ Object
readonly
Returns the value of attribute label_text.
-
#text_method ⇒ Object
readonly
Returns the value of attribute text_method.
-
#value_method ⇒ Object
readonly
Returns the value of attribute value_method.
Instance Method Summary collapse
- #checkbox_classes ⇒ Object
- #checkbox_item_classes ⇒ Object
- #checkbox_label_classes ⇒ Object
- #collection_check_boxes_options ⇒ Object
- #disabled? ⇒ Boolean
- #errors ⇒ Object
- #errors? ⇒ Boolean
- #hint_classes ⇒ Object
-
#initialize(attribute:, collection:, form:, label: nil, hint: nil, value_method: :id, text_method: :name, disabled: false, options: {}) ⇒ CollectionCheckboxes
constructor
A new instance of CollectionCheckboxes.
- #label_classes ⇒ Object
- #object ⇒ Object
- #render_hint? ⇒ Boolean
- #render_label? ⇒ Boolean
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 = || {} end |
Instance Attribute Details
#attribute ⇒ Object (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 |
#collection ⇒ Object (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 |
#disabled ⇒ Object (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 |
#form ⇒ Object (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_text ⇒ Object (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_text ⇒ Object (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_method ⇒ Object (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_method ⇒ Object (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_classes ⇒ Object
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_classes ⇒ Object
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_classes ⇒ Object
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_options ⇒ Object
118 119 120 |
# File 'app/components/uchi/ui/form/input/collection_checkboxes.rb', line 118 def @options end |
#disabled? ⇒ Boolean
58 59 60 |
# File 'app/components/uchi/ui/form/input/collection_checkboxes.rb', line 58 def disabled? !!disabled end |
#errors ⇒ Object
50 51 52 |
# File 'app/components/uchi/ui/form/input/collection_checkboxes.rb', line 50 def errors @errors ||= object.errors[attribute] || [] end |
#errors? ⇒ Boolean
54 55 56 |
# File 'app/components/uchi/ui/form/input/collection_checkboxes.rb', line 54 def errors? errors.any? end |
#hint_classes ⇒ Object
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_classes ⇒ Object
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 |
#object ⇒ Object
46 47 48 |
# File 'app/components/uchi/ui/form/input/collection_checkboxes.rb', line 46 def object @object ||= form.object end |
#render_hint? ⇒ 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
62 63 64 |
# File 'app/components/uchi/ui/form/input/collection_checkboxes.rb', line 62 def render_label? label_text.present? end |