Class: Panda::Core::FormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- Panda::Core::FormBuilder
- Includes:
- ActionView::Helpers::FormTagHelper, ActionView::Helpers::TagHelper
- Defined in:
- app/builders/panda/core/form_builder.rb
Instance Method Summary collapse
- #button(value = nil, options = {}, &block) ⇒ Object
- #check_box(method, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object
- #collection_select(method, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object
- #date_field(method, options = {}) ⇒ Object
- #datetime_field(method, options = {}) ⇒ Object
- #email_field(method, options = {}) ⇒ Object
- #file_field(method, options = {}) ⇒ Object
- #label(attribute, text = nil, options = {}) ⇒ Object
- #meta_text(options) ⇒ Object
- #password_field(attribute, options = {}) ⇒ Object
- #radio_button_group(method, choices, options = {}) ⇒ Object
- #section_heading(text, options = {}) ⇒ Object
- #select(method, choices = nil, options = {}, html_options = {}) ⇒ Object
- #submit(value = nil, options = {}) ⇒ Object
- #text_area(method, options = {}) ⇒ Object
- #text_field(attribute, options = {}) ⇒ Object
- #time_zone_select(method, priority_zones = nil, options = {}, html_options = {}) ⇒ Object
Instance Method Details
#button(value = nil, options = {}, &block) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'app/builders/panda/core/form_builder.rb', line 229 def (value = nil, = {}, &block) value ||= submit_default_value = .dup # Handle formmethod specially if [:formmethod] == "delete" [:name] = "_method" [:value] = "delete" end base_classes = [ "inline-flex items-center rounded-md", "px-3 py-2", "text-base font-semibold", "shadow-sm" ] # Only add fa-circle-check for non-block buttons base_classes << "fa-circle-check" unless block_given? [:class] = [ *base_classes, [:class] ].compact.join(" ") if block_given? @template.(, &block) else @template.(value, ) end end |
#check_box(method, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object
285 286 287 288 289 290 291 292 |
# File 'app/builders/panda/core/form_builder.rb', line 285 def check_box(method, = {}, checked_value = "1", unchecked_value = "0") # Extract custom label if provided custom_label = .delete(:label) content_tag :div, class: container_styles do label(method, custom_label) + () + super(method, .reverse_merge(class: "border-gray-300 ml-2"), checked_value, unchecked_value) end end |
#collection_select(method, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'app/builders/panda/core/form_builder.rb', line 87 def collection_select(method, collection, value_method, text_method, = {}, = {}) # Extract custom label if provided custom_label = .delete(:label) content_tag :div, class: container_styles do label(method, custom_label) + () + super(method, collection, value_method, text_method, , .reverse_merge(class: input_styles)) + (method) end end |
#date_field(method, options = {}) ⇒ Object
294 295 296 297 298 299 300 301 |
# File 'app/builders/panda/core/form_builder.rb', line 294 def date_field(method, = {}) # Extract custom label if provided custom_label = .delete(:label) content_tag :div, class: container_styles do label(method, custom_label) + () + super(method, .reverse_merge(class: input_styles)) end end |
#datetime_field(method, options = {}) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'app/builders/panda/core/form_builder.rb', line 51 def datetime_field(method, = {}) # Extract custom label if provided custom_label = .delete(:label) content_tag :div, class: container_styles do label(method, custom_label) + () + super(method, .reverse_merge(class: input_styles)) + (method) end end |
#email_field(method, options = {}) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'app/builders/panda/core/form_builder.rb', line 42 def email_field(method, = {}) # Extract custom label if provided custom_label = .delete(:label) content_tag :div, class: container_styles do label(method, custom_label) + () + super(method, .reverse_merge(class: input_styles)) + (method) end end |
#file_field(method, options = {}) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'app/builders/panda/core/form_builder.rb', line 107 def file_field(method, = {}) # Extract custom label if provided custom_label = .delete(:label) # Check if cropper is requested with_cropper = .delete(:with_cropper) # Check if simple mode is requested (no fancy upload UI) simple_mode = .delete(:simple) if with_cropper # Image upload with cropper aspect_ratio = .delete(:aspect_ratio) # e.g., 1.91 for OG images (1200x630) min_width = .delete(:min_width) || 0 min_height = .delete(:min_height) || 0 accept_types = .delete(:accept) || "image/*" field_id = "#{object_name}_#{method}" content_tag :div, class: container_styles do label(method, custom_label) + () + # Cropper stylesheet @template.content_tag(:link, nil, rel: "stylesheet", href: "https://cdn.jsdelivr.net/npm/[email protected]/dist/cropper.min.css") + # File input content_tag(:div, class: "mt-2") do super(method, .reverse_merge( id: field_id, accept: accept_types, class: "file:rounded file:border-0 file:text-sm file:bg-white file:text-gray-500 hover:file:bg-gray-50 bg-white px-2.5 hover:bg-gray-50 #{input_styles}", data: { controller: "image-cropper", image_cropper_target: "input", action: "change->image-cropper#handleFileSelect", image_cropper_aspect_ratio_value: aspect_ratio, image_cropper_min_width_value: min_width, image_cropper_min_height_value: min_height } )) end + # Cropper container (hidden by default) content_tag(:div, class: "hidden mt-4 bg-gray-100 dark:bg-gray-800 p-4 rounded-lg", data: {image_cropper_target: "cropperContainer"}) do # Preview image @template.image_tag("", alt: "Crop preview", data: {image_cropper_target: "preview"}, class: "max-w-full") + # Cropper controls content_tag(:div, class: "mt-4 flex gap-2 flex-wrap") do @template.("Crop & Save", type: "button", class: "inline-flex items-center gap-x-1.5 rounded-md bg-indigo-600 px-3 py-2 text-sm font-semibold text-white shadow-xs hover:bg-indigo-500", data: {action: "click->image-cropper#crop"}) + @template.("Cancel", type: "button", class: "inline-flex items-center gap-x-1.5 rounded-md bg-white px-3 py-2 text-sm font-semibold text-gray-900 shadow-xs inset-ring inset-ring-gray-300 hover:bg-gray-50", data: {action: "click->image-cropper#cancel"}) + @template.(type: "button", class: "inline-flex items-center gap-x-1.5 rounded-md bg-white px-3 py-2 text-sm font-semibold text-gray-900 shadow-xs inset-ring inset-ring-gray-300 hover:bg-gray-50", data: {action: "click->image-cropper#reset"}) do @template.content_tag(:i, "", class: "fa-solid fa-rotate-left") + @template.content_tag(:span, "Reset") end + @template.(type: "button", class: "inline-flex items-center gap-x-1.5 rounded-md bg-white px-3 py-2 text-sm font-semibold text-gray-900 shadow-xs inset-ring inset-ring-gray-300 hover:bg-gray-50", data: {action: "click->image-cropper#rotate", degrees: "90"}) do @template.content_tag(:i, "", class: "fa-solid fa-rotate-right") + @template.content_tag(:span, "Rotate") end + @template.(type: "button", class: "inline-flex items-center gap-x-1.5 rounded-md bg-white px-3 py-2 text-sm font-semibold text-gray-900 shadow-xs inset-ring inset-ring-gray-300 hover:bg-gray-50", data: {action: "click->image-cropper#flip", direction: "horizontal"}) do @template.content_tag(:i, "", class: "fa-solid fa-arrows-left-right") + @template.content_tag(:span, "Flip H") end + @template.(type: "button", class: "inline-flex items-center gap-x-1.5 rounded-md bg-white px-3 py-2 text-sm font-semibold text-gray-900 shadow-xs inset-ring inset-ring-gray-300 hover:bg-gray-50", data: {action: "click->image-cropper#zoom", ratio: "0.1"}) do @template.content_tag(:i, "", class: "fa-solid fa-magnifying-glass-plus") + @template.content_tag(:span, "Zoom In") end + @template.(type: "button", class: "inline-flex items-center gap-x-1.5 rounded-md bg-white px-3 py-2 text-sm font-semibold text-gray-900 shadow-xs inset-ring inset-ring-gray-300 hover:bg-gray-50", data: {action: "click->image-cropper#zoom", ratio: "-0.1"}) do @template.content_tag(:i, "", class: "fa-solid fa-magnifying-glass-minus") + @template.content_tag(:span, "Zoom Out") end end end end elsif simple_mode # Simple file input with basic styling content_tag :div, class: container_styles do label(method, custom_label) + () + super(method, .reverse_merge(class: "file:rounded file:border-0 file:text-sm file:bg-white file:text-gray-500 hover:file:bg-gray-50 bg-white px-2.5 hover:bg-gray-50 #{input_styles}")) end else # Fancy drag-and-drop UI accept_types = .delete(:accept) || "image/*" max_size = .delete(:max_size) || "10MB" file_types_display = .delete(:file_types_display) || "PNG, JPG, GIF" field_id = "#{object_name}_#{method}" content_tag :div, class: "#{container_styles} col-span-full", data: {controller: "file-upload"} do label(method, custom_label) + () + content_tag(:div, class: "mt-2 flex justify-center rounded-lg border border-dashed border-gray-900/25 px-6 py-10 dark:border-white/25 transition-colors", data: {file_upload_target: "dropzone"}) do content_tag(:div, class: "text-center") do # Icon @template.content_tag(:svg, viewBox: "0 0 24 24", fill: "currentColor", "data-slot": "icon", "aria-hidden": true, class: "mx-auto size-12 text-gray-300 dark:text-gray-600") do @template.content_tag(:path, nil, d: "M1.5 6a2.25 2.25 0 0 1 2.25-2.25h16.5A2.25 2.25 0 0 1 22.5 6v12a2.25 2.25 0 0 1-2.25 2.25H3.75A2.25 2.25 0 0 1 1.5 18V6ZM3 16.06V18c0 .414.336.75.75.75h16.5A.75.75 0 0 0 21 18v-1.94l-2.69-2.689a1.5 1.5 0 0 0-2.12 0l-.88.879.97.97a.75.75 0 1 1-1.06 1.06l-5.16-5.159a1.5 1.5 0 0 0-2.12 0L3 16.061Zm10.125-7.81a1.125 1.125 0 1 1 2.25 0 1.125 1.125 0 0 1-2.25 0Z", "clip-rule": "evenodd", "fill-rule": "evenodd") end + # Upload area content_tag(:div, class: "mt-4 flex items-baseline justify-center text-sm leading-6 text-gray-600 dark:text-gray-400") do content_tag(:label, for: field_id, class: "relative cursor-pointer rounded-md bg-transparent font-semibold text-indigo-600 focus-within:outline-2 focus-within:outline-offset-2 focus-within:outline-indigo-600 hover:text-indigo-500 dark:text-indigo-400 dark:focus-within:outline-indigo-500 dark:hover:text-indigo-300") do content_tag(:span, "Upload a file") + super(method, .reverse_merge( id: field_id, accept: accept_types, class: "sr-only", data: { file_upload_target: "input", action: "change->file-upload#handleFileSelect" } )) end + content_tag(:span, "or drag and drop", class: "pl-1") end + # File type info content_tag(:p, "#{file_types_display} up to #{max_size}", class: "text-xs/5 text-gray-600 dark:text-gray-400") end end + # File info display (hidden by default) content_tag(:div, "", class: "hidden mt-3", data: {file_upload_target: "fileInfo"}) + # Preview display (hidden by default) content_tag(:div, "", class: "hidden mt-3", data: {file_upload_target: "preview"}) end end end |
#label(attribute, text = nil, options = {}) ⇒ Object
9 10 11 |
# File 'app/builders/panda/core/form_builder.rb', line 9 def label(attribute, text = nil, = {}) super(attribute, text, .reverse_merge(class: label_styles)) end |
#meta_text(options) ⇒ Object
328 329 330 331 332 |
# File 'app/builders/panda/core/form_builder.rb', line 328 def () return unless [:meta] @template.content_tag(:p, [:meta], class: "block text-black/60 text-sm mb-2") end |
#password_field(attribute, options = {}) ⇒ Object
69 70 71 72 73 74 75 76 |
# File 'app/builders/panda/core/form_builder.rb', line 69 def password_field(attribute, = {}) # Extract custom label if provided custom_label = .delete(:label) content_tag :div, class: container_styles do label(attribute, custom_label) + () + super(attribute, .reverse_merge(class: input_styles)) + (attribute) end end |
#radio_button_group(method, choices, options = {}) ⇒ Object
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'app/builders/panda/core/form_builder.rb', line 303 def (method, choices, = {}) # Extract custom label if provided custom_label = .delete(:label) current_value = object.send(method) content_tag :div, class: container_styles do label(method, custom_label) + () + content_tag(:div, class: "mt-2 space-y-2") do choices.map do |choice| choice_value = choice.is_a?(Array) ? choice.last : choice choice_label = choice.is_a?(Array) ? choice.first : choice.to_s.humanize choice_id = "#{object_name}_#{method}_#{choice_value}" is_checked = (current_value.to_s == choice_value.to_s) content_tag(:label, class: "flex items-center gap-x-3 rounded-lg border border-gray-300 px-3 py-3 text-sm/6 font-medium cursor-pointer hover:bg-gray-50 dark:border-white/10 dark:hover:bg-white/5") do (method, choice_value, {id: choice_id, checked: is_checked, class: "size-4 border-gray-300 text-indigo-600 focus:ring-indigo-600 dark:border-white/10 dark:bg-white/5"}) + content_tag(:span, choice_label, class: "text-gray-900 dark:text-white") end end.join.html_safe end end end |
#section_heading(text, options = {}) ⇒ Object
334 335 336 337 338 |
# File 'app/builders/panda/core/form_builder.rb', line 334 def section_heading(text, = {}) @template.content_tag(:div, class: "-mx-4 sm:-mx-6 px-4 sm:px-6 py-4 bg-gray-200 dark:bg-gray-700 mb-6") do @template.content_tag(:h3, text, class: "text-base font-semibold text-gray-900 dark:text-white") end end |
#select(method, choices = nil, options = {}, html_options = {}) ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'app/builders/panda/core/form_builder.rb', line 78 def select(method, choices = nil, = {}, = {}) # Extract custom label if provided custom_label = .delete(:label) content_tag :div, class: container_styles do label(method, custom_label) + () + super(method, choices, , .reverse_merge(class: select_styles)) + select_svg + (method) end end |
#submit(value = nil, options = {}) ⇒ Object
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'app/builders/panda/core/form_builder.rb', line 261 def submit(value = nil, = {}) value ||= submit_default_value # Use the primary mid color for save/create actions action = object.persisted? ? :save : :create = case action when :save, :create "text-white bg-mid hover:bg-mid/80" when :save_inactive "text-white bg-gray-400" when :secondary "text-gray-700 border-2 border-gray-500 bg-transparent hover:bg-gray-100 transition-all" else "text-gray-700 border-2 border-gray-500 bg-transparent hover:bg-gray-100 transition-all" end # Combine with common button classes classes = "inline-flex items-center rounded-md font-medium shadow-sm focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 px-3 py-2 #{button_classes}" [:class] = [:class] ? "#{options[:class]} #{classes}" : classes super end |
#text_area(method, options = {}) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'app/builders/panda/core/form_builder.rb', line 60 def text_area(method, = {}) # Extract custom label if provided custom_label = .delete(:label) content_tag :div, class: container_styles do label(method, custom_label) + () + super(method, .reverse_merge(class: input_styles)) + (method) end end |
#text_field(attribute, options = {}) ⇒ Object
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 |
# File 'app/builders/panda/core/form_builder.rb', line 13 def text_field(attribute, = {}) # Extract custom label if provided custom_label = .delete(:label) # Add disabled/readonly styling field_classes = if [:readonly] || [:disabled] readonly_input_styles else input_styles end if .dig(:data, :prefix) content_tag :div, class: container_styles do label(attribute, custom_label) + () + content_tag(:div, class: "flex flex-grow") do content_tag(:span, class: "inline-flex items-center px-3 text-base border border-r-none rounded-s-md whitespace-nowrap break-keep") do .dig(:data, :prefix) end + super(attribute, .reverse_merge(class: "#{field_classes} input-prefix rounded-l-none border-l-none")) end + (attribute) end else content_tag :div, class: container_styles do label(attribute, custom_label) + () + super(attribute, .reverse_merge(class: field_classes)) + (attribute) end end end |
#time_zone_select(method, priority_zones = nil, options = {}, html_options = {}) ⇒ Object
96 97 98 99 100 101 102 103 104 105 |
# File 'app/builders/panda/core/form_builder.rb', line 96 def time_zone_select(method, priority_zones = nil, = {}, = {}) wrap_field(method, ) do super( method, priority_zones, , .reverse_merge(class: select_styles) ) end end |