Skip to content

Commit 62e7e57

Browse files
committed
Allow wrapper false for check_box, radio, and switch
1 parent 2f19f88 commit 62e7e57

1 file changed

Lines changed: 45 additions & 11 deletions

File tree

lib/railsui/form_builder.rb

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,21 @@ def select(method, choices = nil, options = {}, html_options = {})
108108
super(method, choices, options, html_options)
109109
end
110110
end
111-
111+
112112
def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
113-
wrapper_options = options.delete(:wrapper) || {}
113+
wrapper_options = options.delete(:wrapper)
114114
label_text = options.delete(:label)
115115
label_class = options.delete(:label_class) || "form-label"
116116
is_required = options.delete(:required) || false
117117

118+
# If wrapper is explicitly false, return just the checkbox without wrapping
119+
if wrapper_options == false
120+
add_error_class!(options) if has_error?(method)
121+
return super(method, options, checked_value, unchecked_value)
122+
end
123+
124+
wrapper_options ||= {}
125+
118126
# Set default flex layout for the wrapper
119127
wrapper_class = "flex items-center justify-start gap-2"
120128
wrapper_options[:class] = [wrapper_class, wrapper_options[:class]].compact.join(" ")
@@ -136,9 +144,17 @@ def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
136144
end
137145

138146
def radio_button(method, tag_value, options = {})
139-
wrapper_options = options.delete(:wrapper) || {}
147+
wrapper_options = options.delete(:wrapper)
140148
label_text = options.delete(:label)
141149

150+
# If wrapper is explicitly false, return just the radio button without wrapping
151+
if wrapper_options == false
152+
add_error_class!(options) if has_error?(method)
153+
return super(method, tag_value, options)
154+
end
155+
156+
wrapper_options ||= {}
157+
142158
# Set default flex layout for the wrapper
143159
wrapper_class = "flex items-center justify-start gap-2"
144160
wrapper_options[:class] = [wrapper_class, wrapper_options[:class]].compact.join(" ")
@@ -192,16 +208,24 @@ def range_field(method, options = {})
192208

193209

194210
def switch_field(method, options = {})
211+
wrapper_options = options.delete(:wrapper)
195212
label_text = options.delete(:label) || method.to_s.humanize
196213

197-
field_wrapper(method, options.merge(label: false)) do
198-
add_default_class!(options, "form-input-switch")
199-
add_error_class!(options) if has_error?(method)
214+
add_default_class!(options, "form-input-switch")
215+
add_error_class!(options) if has_error?(method)
216+
217+
# Create switch input without hidden field
218+
switch_html = @template.check_box(@object_name, method, objectify_options(options.merge(include_hidden: false)), "1", "0")
219+
label_html = label(method, label_text, class: "")
220+
switch_content = safe_join([switch_html, label_html])
221+
222+
# If wrapper is explicitly false, return just the switch without wrapping
223+
if wrapper_options == false
224+
return switch_content
225+
end
200226

201-
# Create switch input without hidden field
202-
switch_html = @template.check_box(@object_name, method, objectify_options(options.merge(include_hidden: false)), "1", "0")
203-
label_html = label(method, label_text)
204-
safe_join([switch_html, label_html])
227+
field_wrapper(method, { wrapper: wrapper_options, label: false }) do
228+
switch_content
205229
end
206230
end
207231

@@ -240,7 +264,7 @@ def form_group(method = nil, options = {}, &block)
240264
end
241265

242266
def form_help(text, options = {})
243-
add_default_class!(options, "form-help text-xs")
267+
add_default_class!(options, "form-help")
244268
content_tag(:p, text, options)
245269
end
246270

@@ -310,5 +334,15 @@ def required_field?(method)
310334
validator.is_a?(ActiveModel::Validations::PresenceValidator)
311335
end
312336
end
337+
338+
private
339+
340+
def required_field?(method)
341+
return false unless @object.class.respond_to?(:validators_on)
342+
343+
@object.class.validators_on(method).any? do |validator|
344+
validator.is_a?(ActiveModel::Validations::PresenceValidator)
345+
end
346+
end
313347
end
314348
end

0 commit comments

Comments
 (0)