Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle inline radio buttons and check boxes for Bootstrap 4. #410

Merged
merged 2 commits into from
Jan 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 8 additions & 17 deletions lib/bootstrap_form/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,22 +136,16 @@ def check_box_with_bootstrap(name, options = {}, checked_value = "1", unchecked_
checkbox_html.concat(label(label_name, label_description, class: ["custom-control-label", label_class].compact.join(" ")))
end
else
if options[:inline]
label_class = " #{label_class}" if label_class
checkbox_html
.concat(label(label_name,
label_description,
{ class: "form-check-inline#{label_class}" }.merge(options[:id].present? ? { for: options[:id] } : {})))
else
content_tag(:div, class: "form-check") do
wrapper_class = "form-check"
wrapper_class += " form-check-inline" if options[:inline]
content_tag(:div, class: wrapper_class) do
checkbox_html
.concat(label(label_name,
label_description,
{ class: ["form-check-label", label_class].compact.join(" ") }.merge(options[:id].present? ? { for: options[:id] } : {})))
end
end
end
end

bootstrap_method_alias :check_box

Expand All @@ -176,15 +170,12 @@ def radio_button_with_bootstrap(name, value, *args)
radio_html.concat(label(name, options[:label], value: value, class: ["custom-control-label", label_class].compact.join(" ")))
end
else
label_class = " #{label_class}" if label_class
if options[:inline]
radio_html
.concat(label(name, options[:label], { class: "form-check-label#{label_class}", value: value }.merge(options[:id].present? ? { for: options[:id] } : {})))
else
content_tag(:div, class: "form-check#{disabled_class}") do
wrapper_class = "form-check"
wrapper_class += " form-check-inline" if options[:inline]
label_class = ["form-check-label", label_class].compact.join(" ")
content_tag(:div, class: "#{wrapper_class}#{disabled_class}") do
radio_html
.concat(label(name, options[:label], { class: "form-check-label#{label_class}", value: value }.merge(options[:id].present? ? { for: options[:id] } : {})))
end
.concat(label(name, options[:label], { value: value, class: label_class }.merge(options[:id].present? ? { for: options[:id] } : {})))
end
end
end
Expand Down
20 changes: 15 additions & 5 deletions test/bootstrap_checkbox_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,33 +98,39 @@ class BootstrapCheckboxTest < ActionView::TestCase

test "inline checkboxes" do
expected = <<-HTML.strip_heredoc
<div class="form-check form-check-inline">
<input name="user[terms]" type="hidden" value="0" />
<input class="form-check-input" id="user_terms" name="user[terms]" type="checkbox" value="1" />
<label class="form-check-inline" for="user_terms">
<label class="form-check-label" for="user_terms">
I agree to the terms
</label>
</div>
HTML
assert_equivalent_xml expected, @builder.check_box(:terms, label: 'I agree to the terms', inline: true)
end

test "disabled inline check_box" do
expected = <<-HTML.strip_heredoc
<div class="form-check form-check-inline">
<input name="user[terms]" type="hidden" value="0" disabled="disabled" />
<input class="form-check-input" id="user_terms" name="user[terms]" type="checkbox" value="1" disabled="disabled" />
<label class="form-check-inline" for="user_terms">
<label class="form-check-label" for="user_terms">
I agree to the terms
</label>
</div>
HTML
assert_equivalent_xml 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 = <<-HTML.strip_heredoc
<div class="form-check form-check-inline">
<input name="user[terms]" type="hidden" value="0" />
<input class="form-check-input" id="user_terms" name="user[terms]" type="checkbox" value="1" />
<label class="form-check-inline btn" for="user_terms">
<label class="form-check-label btn" for="user_terms">
Terms
</label>
</div>
HTML
assert_equivalent_xml expected, @builder.check_box(:terms, inline: true, label_class: 'btn')
end
Expand Down Expand Up @@ -176,15 +182,19 @@ class BootstrapCheckboxTest < ActionView::TestCase
<input id="user_misc" multiple="multiple" name="user[misc][]" type="hidden" value="" />
<div class="form-group">
<label for="user_misc">Misc</label>
<div class="form-check form-check-inline">
<input class="form-check-input" id="user_misc_1" name="user[misc][]" type="checkbox" value="1" />
<label class="form-check-inline" for="user_misc_1">
<label class="form-check-label" for="user_misc_1">
Foo
</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" id="user_misc_2" name="user[misc][]" type="checkbox" value="2" />
<label class="form-check-inline" for="user_misc_2">
<label class="form-check-label" for="user_misc_2">
Bar
</label>
</div>
</div>
HTML

assert_equivalent_xml expected, @builder.collection_check_boxes(:misc, collection, :id, :street, inline: true)
Expand Down
10 changes: 10 additions & 0 deletions test/bootstrap_radio_button_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,30 +55,36 @@ class BootstrapRadioButtonTest < ActionView::TestCase

test "radio_button inline label is set correctly" do
expected = <<-HTML.strip_heredoc
<div class="form-check form-check-inline">
<input class="form-check-input" id="user_misc_1" name="user[misc]" type="radio" value="1" />
<label class="form-check-label" for="user_misc_1">
This is a radio button
</label>
</div>
HTML
assert_equivalent_xml expected, @builder.radio_button(:misc, '1', label: 'This is a radio button', inline: true)
end

test "radio_button disabled inline label is set correctly" do
expected = <<-HTML.strip_heredoc
<div class="form-check form-check-inline disabled">
<input class="form-check-input" disabled="disabled" id="user_misc_1" name="user[misc]" type="radio" value="1" />
<label class="form-check-label" for="user_misc_1">
This is a radio button
</label>
</div>
HTML
assert_equivalent_xml expected, @builder.radio_button(:misc, '1', label: 'This is a radio button', inline: true, disabled: true)
end

test "radio_button inline label class is set correctly" do
expected = <<-HTML.strip_heredoc
<div class="form-check form-check-inline">
<input class="form-check-input" id="user_misc_1" name="user[misc]" type="radio" value="1" />
<label class="form-check-label btn" for="user_misc_1">
This is a radio button
</label>
</div>
HTML
assert_equivalent_xml expected, @builder.radio_button(:misc, '1', label: 'This is a radio button', inline: true, label_class: 'btn')
end
Expand Down Expand Up @@ -125,11 +131,15 @@ class BootstrapRadioButtonTest < ActionView::TestCase
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label for="user_misc">Misc</label>
<div class="form-check form-check-inline">
<input class="form-check-input" id="user_misc_1" name="user[misc]" type="radio" value="1" />
<label class="form-check-label" for="user_misc_1"> Foo</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" id="user_misc_2" name="user[misc]" type="radio" value="2" />
<label class="form-check-label" for="user_misc_2"> Bar</label>
</div>
</div>
HTML

assert_equivalent_xml expected, @builder.collection_radio_buttons(:misc, collection, :id, :street, inline: true)
Expand Down