Class: RUI::Forms::FormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- RUI::Forms::FormBuilder
- 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
-
#label(method, content_or_options = nil, options = {}, &block) ⇒ Object
Style labels and include error messages if present.
-
#select(method, choices = nil, options = {}, html_options = {}, &block) ⇒ Object
Fields and helpers that are not covered in text-like fields.
- #submit(value = nil, options = {}) ⇒ Object
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, = nil, = {}, &block) opts = || 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" = merge_classes(opts, default_classes) label_content = if object.present? && object.errors[method].present? @template.content_tag(:div, class: "flex flex-col") do @template.concat(super(method, , , &block)) @template.concat(@template.content_tag(:span, object.errors.(method).to_sentence, class: error_classes)) end else super(method, , , &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, = {}, = {}, &block) super(method, choices, , merge_classes(, 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, = {}) default_classes = RUI::TailwindMerger.instance.merge(RUI::Buttons::Base::STYLE, RUI::Buttons::Primary::STYLE) @template.content_tag("div", super(value, merge_classes(, default_classes))) end |