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

Set label's for: to id: option for checkbox and radio buttons. #472

Closed

Conversation

lcreid
Copy link
Contributor

@lcreid lcreid commented Jul 2, 2018

If the programmer passes an id option to most helpers, our code sets the for attribute of the label to the value of the id option. However, this was not happening for custom check boxes and radio buttons. I can think of no reason why we shouldn't do the same for custom check boxes and radio buttons, so this PR does so.

Fixes #471.

@lcreid lcreid requested a review from mattbrictson July 2, 2018 18:59
Copy link
Contributor

@mattbrictson mattbrictson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! ✨

I think a CHANGELOG entry is warranted for this one. Also some suggestions inline below.

checkbox_html
.concat(label(label_name,
label_description,
{ class: label_class }.merge(options[:id].present? ? { for: options[:id] } : {})))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it would be better to split out the label options into a separate variable instead of trying to do everything in one line? This is a bit hard to read.

# TODO: Notice we don't seem to pass the ID into the custom control.
radio_html.concat(label(name, options[:label], value: value, class: label_class))
radio_html
.concat(label(name, options[:label], { value: value, class: label_class }.merge(options[:id].present? ? { for: options[:id] } : {})))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here, e.g.

label_options = { value: value, class: label_class }.merge(options[:id].present? ? { for: options[:id] } : {})
radio_html.concat(label(name, options[:label], label_options))

or (even more explicit):

label_options = { value: value, class: label_class }
label_options[:for] = options[:id] if options[:id].present?
radio_html.concat(label(name, options[:label], label_options))

@@ -463,6 +463,17 @@ class BootstrapCheckboxTest < ActionView::TestCase
assert_equivalent_xml expected, @builder.check_box(:terms, {label: 'I agree to the terms', custom: true})
end

test "check_box is wrapped correctly with id option and custom option set" do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great tests 💯

Copy link
Contributor

@mattbrictson mattbrictson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@@ -49,6 +49,8 @@ In addition to these necessary markup changes, the bootstrap_form API itself has
* [#449](https://github.com/bootstrap-ruby/bootstrap_form/pull/449): Passing `.form-row` overrides default `.form-group.row` in horizontal layouts.
* Added an option to the `submit` (and `primary`, by transitivity) form tag helper, `render_as_button`, which when truthy makes the submit button render as a button instead of an input. This allows you to easily provide further styling to your form submission buttons, without requiring you to reinvent the wheel and use the `button` helper (and having to manually insert the typical Bootstrap classes). - [@jsaraiva](https://github.com/jsaraiva).
* Add `:error_message` option to `check_box` and `radio_button`, so they can output validation error messages if needed. [@lcreid](https://github.com/lcreid).
- [#472](https://github.com/bootstrap-ruby/bootstrap_form/pull/472) Use `id` option's value as `for` attribute of label for custom checkboxes and radio buttons.
* Your contribution here!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The entry should go in the Unreleased section, not the 4.0.0.alpha1 section. I'll change it before I merge.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doh! Thanks for moving it!

@mattbrictson
Copy link
Contributor

Merged into master @ 3032ba0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants