Class: RUI::Forms::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Includes:
ActiveSupport::Concern
Defined in:
lib/rui/forms/form_builder.rb

Constant Summary collapse

TEXT_FIELD_STYLE =

Base styles for different element types

"bg-zinc-50 ring ring-zinc-300 hover:ring-zinc-400 focus:ring-blue-500 rounded px-2 py-1".freeze
SELECT_FIELD_STYLE =
"bg-zinc-50 ring ring-zinc-300 hover:ring-zinc-400 focus:ring-blue-500 rounded px-2 py-2".freeze

Instance Method Summary collapse

Instance Method Details

#label(method, content_or_options = nil, options = {}, &block) ⇒ Object

Style labels and include error messages if present



12
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
# File 'lib/rui/forms/form_builder.rb', line 12

def label(method, content_or_options = nil, options = {}, &block)
opts = content_or_options || options

default_classes = opts[:required] ? "text-sm font-semibold after:content-['*'] after:ml-1 after:text-red-500" : "text-sm font-semibold"
error_classes = "text-sm text-red-500 font-semibold"

label_options = merge_classes(opts, default_classes)

label_content = if object.present? && object.errors[method].present?
  @template.(:div, class: "flex flex-col") do
    @template.concat(super(method, content_or_options, label_options, &block))
    @template.concat(@template.(:span, object.errors.full_messages_for(method).to_sentence, class: error_classes))
  end
else
  super(method, content_or_options, label_options, &block)
end

# Use metaprogramming to style fields that are like text fields

text_field_helpers.each do |field_method|
  class_eval "    def \#{field_method}(method, options = {})\n      super(method, merge_classes(options, TEXT_FIELD_STYLE))\n    end\n  RUBY_EVAL\nend\n  label_content\nend\n", __FILE__, __LINE__ + 1

#select(method, choices = nil, options = {}, html_options = {}, &block) ⇒ Object

Fields and helpers that are not covered in text-like fields



42
43
44
# File 'lib/rui/forms/form_builder.rb', line 42

def select(method, choices = nil, options = {}, html_options = {}, &block)
  super(method, choices, options, merge_classes(html_options, SELECT_FIELD_STYLE), &block)
end

#submit(value = nil, options = {}) ⇒ Object



46
47
48
49
50
# File 'lib/rui/forms/form_builder.rb', line 46

def submit(value = nil, options = {})
  default_classes = RUI::TailwindMerger.instance.merge(RUI::Buttons::Base::STYLE, RUI::Buttons::Primary::STYLE)

  @template.("div", super(value, merge_classes(options, default_classes)))
end