Add item label class option for collection inputs.

This allows checkbox and radio collections to be styled with the checkbox-inline and radio-inline styles from boostrap 3 when paired with setting the item wrapper tag to false.
This commit is contained in:
Megan Bowra-Dean 2014-05-07 18:35:07 +12:00
parent 609e157817
commit ba6bc460ac
3 changed files with 35 additions and 0 deletions

View File

@ -19,6 +19,7 @@ module SimpleForm
label_options = {} label_options = {}
add_default_name_and_id_for_value(value, label_options) add_default_name_and_id_for_value(value, label_options)
label_options['for'] = label_options.delete('id') label_options['for'] = label_options.delete('id')
label_options['class'] = @options.fetch(:item_label_class, '')
rendered_item = content_tag(:label, rendered_item, label_options) rendered_item = content_tag(:label, rendered_item, label_options)
end end

View File

@ -245,6 +245,23 @@ class CollectionCheckBoxesInputTest < ActionView::TestCase
end end
end end
test 'input check boxes with nested style renders item labels with specified class' do
swap SimpleForm, boolean_style: :nested do
with_input_for @user, :active, :check_boxes, item_label_class: "test"
assert_select 'span.checkbox > label.test > input'
end
end
test 'input check boxes with nested style and falsey input wrapper renders item labels with specified class' do
swap SimpleForm, boolean_style: :nested, item_wrapper_tag: false do
with_input_for @user, :active, :check_boxes, item_label_class: "checkbox-inline"
assert_select 'label.checkbox-inline > input'
assert_no_select 'span.checkbox'
end
end
test 'input check boxes wrapper class are not included when set to falsey' do test 'input check boxes wrapper class are not included when set to falsey' do
swap SimpleForm, include_default_input_wrapper_class: false, boolean_style: :nested do swap SimpleForm, include_default_input_wrapper_class: false, boolean_style: :nested do
with_input_for @user, :gender, :check_boxes, collection: [:male, :female] with_input_for @user, :gender, :check_boxes, collection: [:male, :female]

View File

@ -368,6 +368,23 @@ class CollectionRadioButtonsInputTest < ActionView::TestCase
end end
end end
test 'input radio with nested style renders item labels with specified class' do
swap SimpleForm, boolean_style: :nested do
with_input_for @user, :active, :radio_buttons, item_label_class: "test"
assert_select 'span.radio > label.test > input'
end
end
test 'input radio with nested style and falsey input wrapper renders item labels with specified class' do
swap SimpleForm, boolean_style: :nested, item_wrapper_tag: false do
with_input_for @user, :active, :radio_buttons, item_label_class: "radio-inline"
assert_select 'label.radio-inline > input'
assert_no_select 'span.radio'
end
end
test 'input radio wrapper class are not included when set to falsey' do test 'input radio wrapper class are not included when set to falsey' do
swap SimpleForm, include_default_input_wrapper_class: false, boolean_style: :nested do swap SimpleForm, include_default_input_wrapper_class: false, boolean_style: :nested do
with_input_for @user, :gender, :radio_buttons, collection: [:male, :female] with_input_for @user, :gender, :radio_buttons, collection: [:male, :female]