From f6c8247de35ea4fa709ed4321a0662b0665cefb6 Mon Sep 17 00:00:00 2001 From: Joel Low Date: Thu, 23 Jul 2015 18:03:48 +0800 Subject: [PATCH] Fix non-JavaScript field label generation. Ensure that when generating the date time component, the nominal field label is generated for the displayed text field. The hidden field which is displayed using JavaScript should be given the hidden ID so that non-JavaScript browsers have the label properly set for the displayed field. Furthermore, the JavaScript needs to reassign the label to the hidden field before displaying the enhanced date-time picker. --- CHANGELOG.md | 3 +++ .../simple_form-bootstrap/date_time_input.js | 1 + .../bootstrap/inputs/date_time_input.rb | 25 ++++++++++--------- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5594e6..dc7c8e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ ### enhancements ### bug fixes + * The proper ID for date/time pickers should be generated for non-JavaScript aware browsers first; + Browsers with JavaScript would reassign the label to the hidden date/time picker. This fixes + tests written using Capybara, without JavaScript support. * Specifying the button size (e.g. `btn-lg`) should still generate the appropriate button class (`btn-default` or `btn-primary`). [@lowjoel](https://github.com/lowjoel) diff --git a/app/assets/javascripts/simple_form-bootstrap/date_time_input.js b/app/assets/javascripts/simple_form-bootstrap/date_time_input.js index 685af3e..c0a1226 100644 --- a/app/assets/javascripts/simple_form-bootstrap/date_time_input.js +++ b/app/assets/javascripts/simple_form-bootstrap/date_time_input.js @@ -32,6 +32,7 @@ this.attr('type', 'text'); this.parent().css('display', ''); this.parent().next('input[type=text]').attr('type', 'hidden'); + this.parent().prev('label').attr('for', this.attr('id')) return this; }, diff --git a/lib/simple_form/bootstrap/inputs/date_time_input.rb b/lib/simple_form/bootstrap/inputs/date_time_input.rb index 9a51894..7bc8033 100644 --- a/lib/simple_form/bootstrap/inputs/date_time_input.rb +++ b/lib/simple_form/bootstrap/inputs/date_time_input.rb @@ -9,24 +9,25 @@ def input(_ = nil) # Integrate with Bootstrap's styling text_field_options[:class] << 'form-control' - hidden_field_options = text_field_options.dup - hidden_field_options[:class] = text_field_options[:class].dup # so they won't work with same array object + hidden_field_options = text_field_options.deep_dup + hidden_field_options[:class] << 'bootstrap-datepicker' + hidden_field_options[:class] << 'bootstrap-timepicker' if input_type == :bootstrap_date_time hidden_field_options[:id] = "#{attribute_name}_hidden" - hidden_field_options[:value] ||= value(object).try(:to_s) - text_field_options[:class] << 'bootstrap-datepicker' - text_field_options[:class] << 'bootstrap-timepicker' if input_type == :bootstrap_date_time - text_field_options[:value] ||= format_date(value(object), format) - text_field_options[:data] = { 'date-format' => strftime_to_momentjs_format(format) } + hidden_field_options[:value] ||= format_date(value(object), format) + hidden_field_options[:data] = { 'date-format' => strftime_to_momentjs_format(format) } - text_field_group_classes = text_field_options[:class].dup - text_field_group_classes.delete('form-control') + hidden_field_group_classes = hidden_field_options[:class].dup + hidden_field_group_classes.delete('form-control') + + + text_field_options[:value] ||= value(object).try(:to_s) return_string = <<-END_INPUT -