Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add ViewComponent::Form::WeekdaySelectComponent #105

Merged
merged 5 commits into from
Feb 18, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
- Add `field_id` helper, backported from Rails 7.0 (#104)
- Add `weekday_select` helper (#105)

## [0.1.3] - 2022-01-11
### Fixed
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,11 @@ The following helpers are currently supported by `ViewComponent::Form`.

### `ActionView::Helpers::FormBuilder`

**Supported:** `button` `check_box` `collection_check_boxes` `collection_radio_buttons` `collection_select` `color_field` `date_field` `date_select` `datetime_field` `datetime_local_field` `datetime_select` `email_field` `fields` `fields_for` `file_field` `field_id` `grouped_collection_select` `hidden_field` `month_field` `number_field` `password_field` `phone_field` `radio_button` `range_field` `search_field` `select` `submit` `telephone_field` `text_area` `text_field` `time_field` `time_select` `time_zone_select` `to_model` `to_partial_path` `url_field` `week_field`
**Supported:** `button` `check_box` `collection_check_boxes` `collection_radio_buttons` `collection_select` `color_field` `date_field` `date_select` `datetime_field` `datetime_local_field` `datetime_select` `email_field` `fields` `fields_for` `file_field` `field_id` `grouped_collection_select` `hidden_field` `month_field` `number_field` `password_field` `phone_field` `radio_button` `range_field` `search_field` `select` `submit` `telephone_field` `text_area` `text_field` `time_field` `time_select` `time_zone_select` `to_model` `to_partial_path` `url_field` `week_field` `weekday_select`

**Partially supported:** `label` (blocks not supported) `rich_text_area` (untested)

**Unsupported for now:** `field_name` `weekday_select` (Rails 7 only)
**Unsupported for now:** `field_name`

### Specific to `ViewComponent::Form`

Expand Down
36 changes: 36 additions & 0 deletions app/components/view_component/form/weekday_select_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

if Rails::VERSION::MAJOR >= 7
module ViewComponent
module Form
class WeekdaySelectComponent < FieldComponent
attr_reader :html_options

def initialize(form, object_name, method_name, options = {}, html_options = {})
@html_options = html_options

super(form, object_name, method_name, options)

set_html_options!
end

def call
ActionView::Helpers::Tags::WeekdaySelect.new(
object_name,
method_name,
@view_context,
options,
html_options
).render
end

protected

def set_html_options!
@html_options[:class] = class_names(html_options[:class], html_class)
@html_options.delete(:class) if @html_options[:class].blank?
end
end
end
end
end
23 changes: 23 additions & 0 deletions spec/view_component/form/weekday_select_component_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

if Rails::VERSION::MAJOR >= 7
RSpec.describe ViewComponent::Form::WeekdaySelectComponent, type: :component do
let(:object) { OpenStruct.new }
let(:form) { form_with(object) }
let(:options) { {} }
let(:html_options) { {} }

let(:component) { render_inline(described_class.new(form, object_name, :weekday, options, html_options)) }
let(:component_html_attributes) { component.css("select").first.attributes }

context "with simple args" do
it "has a select for the weekdays" do
expect(component.to_html).to have_tag("select",
with: { id: "user_weekday", name: "user[weekday]" })
end
end

include_examples "component with custom html classes", :html_options
include_examples "component with custom data attributes", :html_options
end
end