Class: Panda::Core::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Includes:
ActionView::Helpers::FormTagHelper, ActionView::Helpers::TagHelper
Defined in:
app/builders/panda/core/form_builder.rb

Instance Method Summary collapse

Instance Method Details

#button(value = nil, options = {}, &block) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/builders/panda/core/form_builder.rb', line 85

def button(value = nil, options = {}, &block)
  value ||= submit_default_value
  options = options.dup

  # Handle formmethod specially
  if options[:formmethod] == "delete"
    options[:name] = "_method"
    options[: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?

  options[:class] = [
    *base_classes,
    options[:class]
  ].compact.join(" ")

  if block_given?
    @template.button_tag(options, &block)
  else
    @template.button_tag(value, options)
  end
end

#check_box(method, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object



141
142
143
144
145
# File 'app/builders/panda/core/form_builder.rb', line 141

def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
   :div, class: container_styles do
    label(method) + meta_text(options) + super(method, options.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



62
63
64
65
66
# File 'app/builders/panda/core/form_builder.rb', line 62

def collection_select(method, collection, value_method, text_method, options = {}, html_options = {})
   :div, class: container_styles do
    label(method) + meta_text(options) + super(method, collection, value_method, text_method, options, html_options.reverse_merge(class: input_styles)) + error_message(method)
  end
end

#date_field(method, options = {}) ⇒ Object



147
148
149
150
151
# File 'app/builders/panda/core/form_builder.rb', line 147

def date_field(method, options = {})
   :div, class: container_styles do
    label(method) + meta_text(options) + super(method, options.reverse_merge(class: input_styles))
  end
end

#datetime_field(method, options = {}) ⇒ Object



38
39
40
41
42
# File 'app/builders/panda/core/form_builder.rb', line 38

def datetime_field(method, options = {})
   :div, class: container_styles do
    label(method) + meta_text(options) + super(method, options.reverse_merge(class: input_styles)) + error_message(method)
  end
end

#email_field(method, options = {}) ⇒ Object



32
33
34
35
36
# File 'app/builders/panda/core/form_builder.rb', line 32

def email_field(method, options = {})
   :div, class: container_styles do
    label(method) + meta_text(options) + super(method, options.reverse_merge(class: input_styles)) + error_message(method)
  end
end

#file_field(method, options = {}) ⇒ Object



79
80
81
82
83
# File 'app/builders/panda/core/form_builder.rb', line 79

def file_field(method, options = {})
   :div, class: container_styles do
    label(method) + meta_text(options) + super(method, options.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".concat(input_styles)))
  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, options = {})
  super(attribute, text, options.reverse_merge(class: label_styles))
end

#meta_text(options) ⇒ Object



153
154
155
156
157
# File 'app/builders/panda/core/form_builder.rb', line 153

def meta_text(options)
  return unless options[:meta]

  @template.(:p, options[:meta], class: "block text-black/60 text-sm mb-2")
end

#password_field(attribute, options = {}) ⇒ Object



50
51
52
53
54
# File 'app/builders/panda/core/form_builder.rb', line 50

def password_field(attribute, options = {})
   :div, class: container_styles do
    label(attribute) + meta_text(options) + super(attribute, options.reverse_merge(class: input_styles)) + error_message(attribute)
  end
end

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



56
57
58
59
60
# File 'app/builders/panda/core/form_builder.rb', line 56

def select(method, choices = nil, options = {}, html_options = {})
   :div, class: container_styles do
    label(method) + meta_text(options) + super(method, choices, options, html_options.reverse_merge(class: select_styles)) + select_svg + error_message(method)
  end
end

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



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/builders/panda/core/form_builder.rb', line 117

def submit(value = nil, options = {})
  value ||= submit_default_value

  # Use the same style logic as ButtonComponent
  action = object.persisted? ? :save : :create
  button_classes = case action
  when :save, :create
    "text-white bg-green-600 hover:bg-green-700"
  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}"

  options[:class] = options[:class] ? "#{options[:class]} #{classes}" : classes

  super
end

#text_area(method, options = {}) ⇒ Object



44
45
46
47
48
# File 'app/builders/panda/core/form_builder.rb', line 44

def text_area(method, options = {})
   :div, class: container_styles do
    label(method) + meta_text(options) + super(method, options.reverse_merge(class: input_styles)) + error_message(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
# File 'app/builders/panda/core/form_builder.rb', line 13

def text_field(attribute, options = {})
  if options.dig(:data, :prefix)
     :div, class: container_styles do
      label(attribute) + meta_text(options) +
        (:div, class: "flex flex-grow") do
          (:span,
            class: "inline-flex items-center px-3 text-base border border-r-none rounded-s-md whitespace-nowrap break-keep") do
            options.dig(:data, :prefix)
          end +
            super(attribute, options.reverse_merge(class: "#{input_styles_prefix} input-prefix rounded-l-none border-l-none"))
        end + error_message(attribute)
    end
  else
     :div, class: container_styles do
      label(attribute) + meta_text(options) + super(attribute, options.reverse_merge(class: input_styles)) + error_message(attribute)
    end
  end
end

#time_zone_select(method, priority_zones = nil, options = {}, html_options = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'app/builders/panda/core/form_builder.rb', line 68

def time_zone_select(method, priority_zones = nil, options = {}, html_options = {})
  wrap_field(method, options) do
    super(
      method,
      priority_zones,
      options,
      html_options.reverse_merge(class: select_styles)
    )
  end
end