Commit de1cc9a4 authored by peterhegman's avatar peterhegman

Apply reviewer feedback

- Add `checked_value` and `unchecked_value` arguments
- Update links in docs to official Rails docs
parent 616bd6f5
......@@ -53,5 +53,7 @@ info: To determine the technical writer assigned to the Stage/Group associated w
| `method` | Attribute on the object passed to `gitlab_ui_form_for`. | `Symbol` | `true` |
| `label` | Checkbox label. | `String` | `true` |
| `help_text` | Help text displayed below the checkbox. | `String` | `false` (`nil`) |
| `checkbox_options` | Options that are passed to [Rails `check_box` method](https://apidock.com/rails/ActionView/Helpers/FormHelper/check_box). | `Hash` | `false` (`{}`) |
| `label_options` | Options that are passed to [Rails `label` method](https://apidock.com/rails/ActionView/Helpers/FormHelper/label). | `Hash` | `false` (`{}`) |
| `checkbox_options` | Options that are passed to [Rails `check_box` method](https://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-check_box). | `Hash` | `false` (`{}`) |
| `checked_value` | Value when checkbox is checked. | `String` | `false` (`'1'`) |
| `unchecked_value` | Value when checkbox is unchecked. | `String` | `false` (`'0'`) |
| `label_options` | Options that are passed to [Rails `label` method](https://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-label). | `Hash` | `false` (`{}`) |
......@@ -3,13 +3,25 @@
module Gitlab
module FormBuilders
class GitlabUiFormBuilder < ActionView::Helpers::FormBuilder
def gitlab_ui_checkbox_component(method, label, help_text: nil, checkbox_options: {}, label_options: {})
def gitlab_ui_checkbox_component(
method,
label,
help_text: nil,
checkbox_options: {},
checked_value: '1',
unchecked_value: '0',
label_options: {}
)
@template.content_tag(
:div,
class: 'gl-form-checkbox custom-control custom-checkbox'
) do
@template.check_box(
@object_name, method, format_options(checkbox_options, ['custom-control-input'])
@object_name,
method,
format_options(checkbox_options, ['custom-control-input']),
checked_value,
unchecked_value
) +
@template.label(
@object_name, method, format_options(label_options, ['custom-control-label'])
......
......@@ -41,15 +41,17 @@ RSpec.describe Gitlab::FormBuilders::GitlabUiFormBuilder do
{
help_text: 'Instead of all the files changed, show only one file at a time.',
checkbox_options: { class: 'checkbox-foo-bar' },
label_options: { class: 'label-foo-bar' }
label_options: { class: 'label-foo-bar' },
checked_value: '3',
unchecked_value: '1'
}
end
it 'renders help text' do
expected_html = <<~EOS
<div class="gl-form-checkbox custom-control custom-checkbox">
<input name="user[view_diffs_file_by_file]" type="hidden" value="0" />
<input class="custom-control-input checkbox-foo-bar" type="checkbox" value="1" name="user[view_diffs_file_by_file]" id="user_view_diffs_file_by_file" />
<input name="user[view_diffs_file_by_file]" type="hidden" value="1" />
<input class="custom-control-input checkbox-foo-bar" type="checkbox" value="3" name="user[view_diffs_file_by_file]" id="user_view_diffs_file_by_file" />
<label class="custom-control-label label-foo-bar" for="user_view_diffs_file_by_file">
<span>Show one file at a time on merge request&#39;s Changes tab</span>
<p class="help-text">Instead of all the files changed, show only one file at a time.</p>
......@@ -59,6 +61,22 @@ RSpec.describe Gitlab::FormBuilders::GitlabUiFormBuilder do
expect(checkbox_html).to eq(html_strip_whitespace(expected_html))
end
it 'passes arguments to `check_box` method' do
allow(fake_template).to receive(:check_box).and_return('')
checkbox_html
expect(fake_template).to have_received(:check_box).with(:user, :view_diffs_file_by_file, { class: %w(custom-control-input checkbox-foo-bar), object: user }, '3', '1')
end
it 'passes arguments to `label` method' do
allow(fake_template).to receive(:label).and_return('')
checkbox_html
expect(fake_template).to have_received(:label).with(:user, :view_diffs_file_by_file, { class: %w(custom-control-label label-foo-bar), object: user })
end
end
end
......
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