Files
nairobi/lib/consul_form_builder.rb
Javi Martín fc9a87a8ab Use native HTML5 date fields in the admin section
We've had to add a couple of hacks in order to make jQuery UI datepicker
work with Turbolinks, and one of our tests is failing because the
datepicker changes its height when changing from a month with 5 weeks to
a month with 6 weeks.

We could add a workaround so the test still passes (jQuery UI doesn't
provide a configuration option to always displays 6 weeks in the
datepicker), but I think it's easier to just use the HTML5 native date
input field, which also allows us to simplify the code a bit and IMHO it
improves the user experience, particularly when using mobile phones.

Since date fields are not supported in Safari and Internet Explorer,
we're still using the jQuery UI datepicker on those browsers (and on any
other browser not supporting date fields).

Due to these changes, we're moving the tests checking datepicker's
behaviour to the dashboard. I've choosing not to change the public pages
because I'm not 100% sure everybody would like this change (some people
prefer the datepicker because we can configure the way it looks).
2020-08-28 12:55:58 +02:00

83 lines
2.3 KiB
Ruby

class ConsulFormBuilder < FoundationRailsHelper::FormBuilder
include ActionView::Helpers::SanitizeHelper
def enum_select(attribute, options = {}, html_options = {})
choices = object.class.send(attribute.to_s.pluralize).keys.map do |name|
[object.class.human_attribute_name("#{attribute}.#{name}"), name]
end
select attribute, choices, options, html_options
end
%i[text_field text_area date_field number_field password_field email_field].each do |field|
define_method field do |attribute, options = {}|
label_with_hint(attribute, options.merge(label_options: label_options_for(options))) +
super(attribute, options.merge(
label: false, hint: nil,
aria: { describedby: help_text_id(attribute, options) }
))
end
end
def check_box(attribute, options = {})
if options[:label] != false
label = tag.span sanitize(label_text(attribute, options[:label])), class: "checkbox"
super(attribute, options.merge(label: label, label_options: label_options_for(options)))
else
super
end
end
def radio_button(attribute, tag_value, options = {})
default_label = object.class.human_attribute_name("#{attribute}_#{tag_value}")
super(attribute, tag_value, { label: default_label }.merge(options))
end
private
def custom_label(attribute, text, options)
if text == false
super
else
super(attribute, sanitize(label_text(attribute, text)), options)
end
end
def label_with_hint(attribute, options)
custom_label(attribute, options[:label], options[:label_options]) +
help_text(attribute, options)
end
def label_text(attribute, text)
if text.nil? || text == true
default_label_text(object, attribute)
else
text
end
end
def label_options_for(options)
label_options = options[:label_options] || {}
if options[:id]
{ for: options[:id] }.merge(label_options)
else
label_options
end
end
def help_text(attribute, options)
if options[:hint]
tag.span options[:hint], class: "help-text", id: help_text_id(attribute, options)
end
end
def help_text_id(attribute, options)
if options[:hint]
"#{custom_label(attribute, nil, nil).match(/for="([^"]+)"/)[1]}-help-text"
end
end
end