Skip to content

Commit

Permalink
Fix non-JavaScript field label generation.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lowjoel committed Jul 23, 2015
1 parent e08ded1 commit f6c8247
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand Down
25 changes: 13 additions & 12 deletions lib/simple_form/bootstrap/inputs/date_time_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
<div class="input-group #{text_field_group_classes.join(' ')}" style="display: none">
#{@builder.hidden_field(attribute_name, text_field_options.to_hash)}
<div class="input-group #{hidden_field_group_classes.join(' ')}" style="display: none">
#{@builder.hidden_field(attribute_name, hidden_field_options.to_hash)}
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
</div>
#{@builder.text_field(attribute_name, hidden_field_options.to_hash)}
#{@builder.text_field(attribute_name, text_field_options.to_hash)}
END_INPUT
return_string.html_safe
end
Expand Down

0 comments on commit f6c8247

Please sign in to comment.