diff --git a/CHANGELOG.md b/CHANGELOG.md index ef0fc0db6..d4dc24aa7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ Features: - Use humanized attribute name in label errors (#146, @atipugin) - Allow to skip label rendering (#145, @atipugin) - Added a `required` CSS class for labels with required attributes (#150, @krsyoung) + - Added option to customize labels' class. ## 2.2.0 (2014-09-16) diff --git a/README.md b/README.md index b991d5f70..d375029a3 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,13 @@ class, which keeps your labels accessible to those using screen readers. ```erb <%= f.text_area :comment, hide_label: :true, placeholder: "Leave a comment..." %> ``` + +To add custom classes to the field's label: + +```erb +<%= f.text_field :email, label_class: "custom-class" %> +``` + #### Required Fields A label that is associated with a required field is automatically annotated with diff --git a/lib/bootstrap_form/form_builder.rb b/lib/bootstrap_form/form_builder.rb index d1d52b589..b0a297713 100644 --- a/lib/bootstrap_form/form_builder.rb +++ b/lib/bootstrap_form/form_builder.rb @@ -100,21 +100,24 @@ def time_zone_select_with_bootstrap(method, priority_zones = nil, options = {}, def check_box_with_bootstrap(name, options = {}, checked_value = "1", unchecked_value = "0", &block) options = options.symbolize_keys! + check_box_options = options.except(:label, :label_class, :help, :inline) - html = check_box_without_bootstrap(name, options.except(:label, :help, :inline), checked_value, unchecked_value) + html = check_box_without_bootstrap(name, check_box_options, checked_value, unchecked_value) label_content = block_given? ? capture(&block) : options[:label] html.concat(" ").concat(label_content || (object && object.class.human_attribute_name(name)) || name.to_s.humanize) label_name = name label_name = "#{name}_#{checked_value}" if options[:multiple] - disabled_class = options[:disabled] ? " disabled" : "" + disabled_class = " disabled" if options[:disabled] + label_class = options[:label_class] if options[:inline] - label(label_name, html, class: "checkbox-inline#{disabled_class}") + label_class = " #{label_class}" if label_class + label(label_name, html, class: "checkbox-inline#{disabled_class}#{label_class}") else content_tag(:div, class: "checkbox#{disabled_class}") do - label(label_name, html) + label(label_name, html, class: label_class) end end end @@ -123,16 +126,19 @@ def check_box_with_bootstrap(name, options = {}, checked_value = "1", unchecked_ def radio_button_with_bootstrap(name, value, *args) options = args.extract_options!.symbolize_keys! - args << options.except(:label, :help, :inline) + args << options.except(:label, :label_class, :help, :inline) html = radio_button_without_bootstrap(name, value, *args) + " " + options[:label] - disabled_class = options[:disabled] ? " disabled" : "" + + disabled_class = " disabled" if options[:disabled] + label_class = options[:label_class] if options[:inline] - label(name, html, class: "radio-inline#{disabled_class}", value: value) + label_class = " #{label_class}" if label_class + label(name, html, class: "radio-inline#{disabled_class}#{label_class}", value: value) else content_tag(:div, class: "radio#{disabled_class}") do - label(name, html, value: value) + label(name, html, value: value, class: label_class) end end end @@ -276,8 +282,6 @@ def form_group_builder(method, options, html_options = nil) options = convert_form_tag_options(method, options) if acts_like_form_tag - label = options.delete(:label) - label_class = hide_class if options.delete(:hide_label) wrapper_class = options.delete(:wrapper_class) wrapper_options = options.delete(:wrapper) help = options.delete(:help) @@ -300,8 +304,11 @@ def form_group_builder(method, options, html_options = nil) end unless options.delete(:skip_label) + label_class = hide_class if options.delete(:hide_label) + label_class ||= options.delete(:label_class) + form_group_options.reverse_merge!(label: { - text: label, + text: options.delete(:label), class: label_class }) end diff --git a/test/bootstrap_checkbox_test.rb b/test/bootstrap_checkbox_test.rb index 7e7063638..c2631f5c2 100644 --- a/test/bootstrap_checkbox_test.rb +++ b/test/bootstrap_checkbox_test.rb @@ -27,6 +27,11 @@ def setup assert_equal expected, @builder.check_box(:terms) { "I agree to the terms" } end + test "check_box accepts a custom label class" do + expected = %{
} + assert_equal expected, @builder.check_box(:terms, label_class: 'btn') + end + test "check_box responds to checked_value and unchecked_value arguments" do expected = %{} assert_equal expected, @builder.check_box(:terms, {label: 'I agree to the terms'}, 'yes', 'no') @@ -42,6 +47,11 @@ def setup assert_equal expected, @builder.check_box(:terms, label: 'I agree to the terms', inline: true, disabled: true) end + test "inline checkboxes with custom label class" do + expected = %{} + assert_equal expected, @builder.check_box(:terms, inline: true, label_class: 'btn') + end + test 'collection_check_boxes renders the form_group correctly' do collection = [Address.new(id: 1, street: 'Foobar')] expected = %{