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

Add Form::CheckBoxComponent #14

Merged
merged 6 commits into from
Jun 30, 2021
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
27 changes: 27 additions & 0 deletions app/components/view_component/form/check_box_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

module ViewComponent
module Form
class CheckBoxComponent < FieldComponent
attr_reader :checked_value, :unchecked_value

def initialize(form, object_name, method_name, checked_value, unchecked_value, options = {}) # rubocop:disable Metrics/ParameterLists
@checked_value = checked_value
@unchecked_value = unchecked_value

super(form, object_name, method_name, options)
end

def call
ActionView::Helpers::Tags::CheckBox.new(
object_name,
method_name,
form,
checked_value,
unchecked_value,
options
).render
end
end
end
end
22 changes: 22 additions & 0 deletions spec/view_component/form/check_box_component_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

RSpec.describe ViewComponent::Form::CheckBoxComponent, type: :component do
let(:object) { OpenStruct.new }
let(:form) { form_with(object) }
let(:options) { {} }

let(:component) { render_inline(described_class.new(form, object_name, :admin, "1", "0", options)) }
let(:component_html_attributes) { component.css("input").last.attributes }

context "with simple args" do
it do
expect(component.to_html).to eq(
%(<input name="user[admin]" type="hidden" value="0">) +
%(<input type="checkbox" value="1" name="user[admin]" id="user_admin">)
Comment on lines +14 to +15
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@nicolas-brousse idea for a better syntax? (without newlines and whitespace between the inputs)

Copy link
Member

Choose a reason for hiding this comment

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

In our current project, I use to have this kind of tests.

      expect(component.to_html.tr("\n", "")).to eq <<~HTML.tr("\n", "").strip
        <label class="form-checkbox" for="user_is_enabled">
        <input name="user[is_enabled]" type="hidden" value="0">
        <input class="form-checkbox__input" type="checkbox" value="1" name="user[is_enabled]" id="user_is_enabled">
        <span class="form-checkbox__label">Actif</span>
        </label>
      HTML

It's not the cleanest. Maybe we may create a custom matcher like

      expect(component).to have_html <<~HTML
        <label class="form-checkbox" for="user_is_enabled">
        <input name="user[is_enabled]" type="hidden" value="0">
        <input class="form-checkbox__input" type="checkbox" value="1" name="user[is_enabled]" id="user_is_enabled">
        <span class="form-checkbox__label">Actif</span>
        </label>
      HTML

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, let's create a custom matcher, can you open a PR for it?

Copy link
Member

Choose a reason for hiding this comment

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

Sure.

)
end
end

include_examples "component with custom html classes"
include_examples "component with custom data attributes"
end