Skip to content

Commit

Permalink
Merge pull request #1407 from herminiotorres/hct-merge-wrapper-options
Browse files Browse the repository at this point in the history
Merge attributes with wrapper_options
  • Loading branch information
rafaelfranca committed Jul 20, 2016
2 parents daaf41a + e2faeef commit b440a6c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
## Unreleased

* Add the `aria-invalid` attribute on inputs with errors.
### enhancements
* Add the `aria-invalid` attribute on inputs with errors.

### bug fix
* Fix `merge_wrapper_options` to correctly merge options with duplicated keys. [@herminiotorres](https://github.com/herminiotorres)
Closes [#1278](https://github.com/plataformatec/simple_form/issues/1278).

## 3.2.1

* Updated gem dependency to support Rails 5.0.x.
### enhancements
* Updated gem dependency to support Rails 5.0.x.

## 3.2.0

Expand Down
11 changes: 8 additions & 3 deletions lib/simple_form/inputs/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,14 @@ def translate_from_namespace(namespace, default = '')

def merge_wrapper_options(options, wrapper_options)
if wrapper_options
options.merge(wrapper_options) do |_, oldval, newval|
if Array === oldval
oldval + Array(newval)
wrapper_options.merge(options) do |key, oldval, newval|
case key.to_s
when "class"
Array(oldval) + Array(newval)
when "data", "aria"
oldval.merge(newval)
else
newval
end
end
else
Expand Down
32 changes: 32 additions & 0 deletions test/form_builder/wrapper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,38 @@ class WrapperTest < ActionView::TestCase
assert_select "section.custom_wrapper div.another_wrapper input.string"
end

test "input attributes class will merge with wrapper_options' classes" do
swap_wrapper :default, custom_wrapper_with_input_class do
with_concat_form_for @user do |f|
concat f.input :name, input_html: { class: 'another-class' }
end
end

assert_select "div.custom_wrapper input.string.inline-class.another-class"
end

test "input with data attributes will merge with wrapper_options' data" do
swap_wrapper :default, custom_wrapper_with_input_data_modal do
with_concat_form_for @user do |f|
concat f.input :name, input_html: { data: { modal: 'another-data', target: 'merge-data' } }
end
end

assert_select "input[data-wrapper='data-wrapper'][data-modal='another-data'][data-target='merge-data']"
end

test "input with aria attributes will merge with wrapper_options' aria" do
skip unless ActionPack::VERSION::MAJOR == '4' && ActionPack::VERSION::MINOR >= '2'

swap_wrapper :default, custom_wrapper_with_input_aria_modal do
with_concat_form_for @user do |f|
concat f.input :name, input_html: { aria: { modal: 'another-aria', target: 'merge-aria' } }
end
end

assert_select "input[aria-wrapper='aria-wrapper'][aria-modal='another-aria'][aria-target='merge-aria']"
end

test 'input accepts attributes in the DSL' do
swap_wrapper :default, custom_wrapper_with_input_class do
with_concat_form_for @user do |f|
Expand Down
14 changes: 14 additions & 0 deletions test/support/misc_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ def custom_wrapper_with_input_class
end
end

def custom_wrapper_with_input_data_modal
SimpleForm.build tag: :div, class: "custom_wrapper" do |b|
b.use :label
b.use :input, data: { modal: 'data-modal', wrapper: 'data-wrapper' }
end
end

def custom_wrapper_with_input_aria_modal
SimpleForm.build tag: :div, class: "custom_wrapper" do |b|
b.use :label
b.use :input, aria: { modal: 'aria-modal', wrapper: 'aria-wrapper' }
end
end

def custom_wrapper_with_label_class
SimpleForm.build tag: :div, class: "custom_wrapper" do |b|
b.use :label, class: 'inline-class'
Expand Down

0 comments on commit b440a6c

Please sign in to comment.