Skip to content

Commit

Permalink
Merge pull request #14 from pantographe/check-box-component
Browse files Browse the repository at this point in the history
Add Form::CheckBoxComponent
  • Loading branch information
Spone authored Jun 30, 2021
2 parents 6f5ccc2 + 2ae4ada commit 588e716
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
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">)
)
end
end

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

0 comments on commit 588e716

Please sign in to comment.