Commit 927afbeb authored by eugielimpin's avatar eugielimpin

Pass value to generic_label when creating multi select checkboxes

When { multiple: true } option is passed to check_box it suffixes the
input's id attribute value with checked_value. For example:

f.check_box(:pets, { multiple: true}, 'dog')
=> <input type="checkbox"
       id="user_pets_dog"
       name="user[pets][]"
       value="dog" />

Previous implementation does not pass in a value parameter to
generic_label so the resulting label tag has the incorrect for attribute
value. This makes the resulting checkboxes un-checkable.

Now, when checkbox_options is set to { multiple: true } we pass in
checked_value as value param to generic_label to create labels with
correct for attribute values.
parent a635fd2d
...@@ -16,13 +16,15 @@ module Gitlab ...@@ -16,13 +16,15 @@ module Gitlab
:div, :div,
class: 'gl-form-checkbox custom-control custom-checkbox' class: 'gl-form-checkbox custom-control custom-checkbox'
) do ) do
value = checkbox_options[:multiple] ? checked_value : nil
@template.check_box( @template.check_box(
@object_name, @object_name,
method, method,
format_options(checkbox_options, ['custom-control-input']), format_options(checkbox_options, ['custom-control-input']),
checked_value, checked_value,
unchecked_value unchecked_value
) + generic_label(method, label, label_options, help_text: help_text) ) + generic_label(method, label, label_options, help_text: help_text, value: value)
end end
end end
......
...@@ -78,6 +78,29 @@ RSpec.describe Gitlab::FormBuilders::GitlabUiFormBuilder do ...@@ -78,6 +78,29 @@ RSpec.describe Gitlab::FormBuilders::GitlabUiFormBuilder do
expect(fake_template).to have_received(:label).with(:user, :view_diffs_file_by_file, { class: %w(custom-control-label label-foo-bar), object: user, value: nil }) expect(fake_template).to have_received(:label).with(:user, :view_diffs_file_by_file, { class: %w(custom-control-label label-foo-bar), object: user, value: nil })
end end
end end
context 'with checkbox_options: { multiple: true }' do
let(:optional_args) do
{
checkbox_options: { multiple: true },
checked_value: 'one',
unchecked_value: false
}
end
it 'renders labels with correct for attributes' do
expected_html = <<~EOS
<div class="gl-form-checkbox custom-control custom-checkbox">
<input class="custom-control-input" type="checkbox" value="one" name="user[view_diffs_file_by_file][]" id="user_view_diffs_file_by_file_one" />
<label class="custom-control-label" for="user_view_diffs_file_by_file_one">
Show one file at a time on merge request&#39;s Changes tab
</label>
</div>
EOS
expect(checkbox_html).to eq(html_strip_whitespace(expected_html))
end
end
end end
describe '#gitlab_ui_radio_component' do describe '#gitlab_ui_radio_component' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment