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
- #datetime_split_field(method, options = {}) ⇒ Object
- #email_field(method, options = {}) ⇒ Object
-
#field_id(method, *suffixes, namespace: @options[:namespace], index: @options[:index]) ⇒ Object
Override field_id to match FormBuilder's signature, since the included FormTagHelper defines a field_id with a different arity that shadows it.
- #file_field(method, options = {}) ⇒ Object
- #label(attribute, text = nil, options = {}) ⇒ Object
- #meta_text(options) ⇒ Object
- #number_field(method, 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
- #telephone_field(method, options = {}) ⇒ Object
- #text_area(method, options = {}) ⇒ Object
- #text_field(attribute, options = {}) ⇒ Object
- #time_zone_select(method, priority_zones = nil, options = {}, html_options = {}) ⇒ Object
- #url_field(method, options = {}) ⇒ Object
Instance Method Details
#button(value = nil, options = {}, &block) ⇒ Object
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'app/builders/panda/core/form_builder.rb', line 293 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
349 350 351 352 353 354 355 356 |
# File 'app/builders/panda/core/form_builder.rb', line 349 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 text-primary-600 focus:ring-primary-500 ml-2"), checked_value, unchecked_value) end end |
#collection_select(method, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 |
# File 'app/builders/panda/core/form_builder.rb', line 146 def collection_select(method, collection, value_method, text_method, = {}, = {}) # Extract custom label if provided custom_label = .delete(:label) = .reverse_merge(class: input_styles) [:data] = ([:data] || {}).merge("custom-select-target": "select") content_tag :div, class: container_styles, data: {controller: "custom-select"} do label(method, custom_label) + () + super(method, collection, value_method, text_method, , ) + (method) end end |
#date_field(method, options = {}) ⇒ Object
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 |
# File 'app/builders/panda/core/form_builder.rb', line 358 def date_field(method, = {}) custom_label = .delete(:label) date_min = .delete(:min) date_max = .delete(:max) # Format current value for display raw_value = object.respond_to?(method) ? object.send(method) : nil iso_value = if raw_value.respond_to?(:strftime) raw_value.strftime("%Y-%m-%d") else raw_value end display_value = if raw_value.respond_to?(:strftime) raw_value.strftime("%-d %b %Y") elsif raw_value.is_a?(String) && raw_value.present? raw_value else "" end controller_data = {controller: "datepicker"} controller_data[:datepicker_date_min_value] = date_min.to_s if date_min controller_data[:datepicker_date_max_value] = date_max.to_s if date_max content_tag :div, class: "#{container_styles} relative", data: controller_data do label(method, custom_label) + () + hidden_field(method, value: iso_value, data: {datepicker_target: "hidden"}) + @template.text_field_tag( nil, display_value, class: "#{input_styles} cursor-pointer", readonly: true, placeholder: [:placeholder] || "Select date...", data: {datepicker_target: "display", action: "click->datepicker#toggle focus->datepicker#toggle"} ) + content_tag(:div, "", class: "hidden absolute z-50 mt-1 left-0", data: {datepicker_target: "calendar"}) + (method) end end |
#datetime_field(method, options = {}) ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'app/builders/panda/core/form_builder.rb', line 73 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 |
#datetime_split_field(method, options = {}) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'app/builders/panda/core/form_builder.rb', line 82 def datetime_split_field(method, = {}) custom_label = .delete(:label) value = object.respond_to?(method) ? object.send(method) : nil content_tag :div, class: container_styles, data: {controller: "datetime"} do label(method, custom_label) + () + hidden_field(method, data: {datetime_target: "combinedField"}, value: value&.iso8601) + content_tag(:div, class: "flex gap-3") do @template.tag.input( type: "date", class: input_styles, data: {datetime_target: "dateField", action: "change->datetime#update"} ) + @template.tag.input( type: "time", step: "60", class: input_styles, data: {datetime_target: "timeField", action: "change->datetime#update"} ) end + (method) end end |
#email_field(method, options = {}) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'app/builders/panda/core/form_builder.rb', line 48 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 |
#field_id(method, *suffixes, namespace: @options[:namespace], index: @options[:index]) ⇒ Object
Override field_id to match FormBuilder's signature, since the included FormTagHelper defines a field_id with a different arity that shadows it.
15 16 17 |
# File 'app/builders/panda/core/form_builder.rb', line 15 def field_id(method, *suffixes, namespace: @options[:namespace], index: @options[:index]) @template.field_id(@object_name, method, *suffixes, namespace: namespace, index: index) end |
#file_field(method, options = {}) ⇒ Object
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 228 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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'app/builders/panda/core/form_builder.rb', line 172 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) + () + # Note: cropperjs 2.x uses Web Components with built-in styles — no external CSS needed # 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-primary-500 px-3 py-2 text-sm font-semibold text-white shadow-xs hover:bg-primary-600", 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-primary-600 focus-within:outline-2 focus-within:outline-offset-2 focus-within:outline-primary-600 hover:text-primary-500 dark:text-primary-400 dark:focus-within:outline-primary-500 dark:hover:text-primary-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
424 425 426 427 428 |
# File 'app/builders/panda/core/form_builder.rb', line 424 def () return unless [:meta] @template.content_tag(:p, [:meta], class: "block text-black/60 text-sm mb-2") end |
#number_field(method, options = {}) ⇒ Object
125 126 127 128 129 130 131 132 |
# File 'app/builders/panda/core/form_builder.rb', line 125 def number_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 |
#password_field(attribute, options = {}) ⇒ Object
116 117 118 119 120 121 122 123 |
# File 'app/builders/panda/core/form_builder.rb', line 116 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
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
# File 'app/builders/panda/core/form_builder.rb', line 399 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-primary-600 focus:ring-primary-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
430 431 432 433 434 |
# File 'app/builders/panda/core/form_builder.rb', line 430 def section_heading(text, = {}) @template.content_tag(:div, class: "mt-6 pt-4 border-t border-gray-200 mb-4") do @template.content_tag(:h3, text, class: "text-sm font-semibold text-gray-700") end end |
#select(method, choices = nil, options = {}, html_options = {}) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 |
# File 'app/builders/panda/core/form_builder.rb', line 134 def select(method, choices = nil, = {}, = {}) # Extract custom label if provided custom_label = .delete(:label) = .reverse_merge(class: select_styles) [:data] = ([:data] || {}).merge("custom-select-target": "select") content_tag :div, class: container_styles, data: {controller: "custom-select"} do label(method, custom_label) + () + super(method, choices, , ) + select_svg + (method) end end |
#submit(value = nil, options = {}) ⇒ Object
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'app/builders/panda/core/form_builder.rb', line 325 def submit(value = nil, = {}) value ||= submit_default_value # Use the primary color for save/create actions action = object&.persisted? ? :save : :create = case action when :save, :create "text-white bg-primary-500 hover:bg-primary-600" 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-2 focus-visible:outline-offset-2 focus-visible:outline-primary-600 px-3 py-2 #{}" [:class] = [:class] ? "#{[:class]} #{classes}" : classes super end |
#telephone_field(method, options = {}) ⇒ Object
57 58 59 60 61 62 63 |
# File 'app/builders/panda/core/form_builder.rb', line 57 def telephone_field(method, = {}) 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_area(method, options = {}) ⇒ Object
107 108 109 110 111 112 113 114 |
# File 'app/builders/panda/core/form_builder.rb', line 107 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
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 |
# File 'app/builders/panda/core/form_builder.rb', line 19 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
158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'app/builders/panda/core/form_builder.rb', line 158 def time_zone_select(method, priority_zones = nil, = {}, = {}) = .reverse_merge(class: select_styles) [:data] = ([:data] || {}).merge("custom-select-target": "select") content_tag :div, class: container_styles, data: {controller: "custom-select"} do label(method) + () + super( method, priority_zones, , ) + (method) end end |
#url_field(method, options = {}) ⇒ Object
65 66 67 68 69 70 71 |
# File 'app/builders/panda/core/form_builder.rb', line 65 def url_field(method, = {}) 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 |