diff --git a/lib/simple_form/form_builder.rb b/lib/simple_form/form_builder.rb index a2b2ba94..42202579 100644 --- a/lib/simple_form/form_builder.rb +++ b/lib/simple_form/form_builder.rb @@ -131,14 +131,15 @@ module SimpleForm # name="user[name]" type="text" value="Carlos" /> # def input_field(attribute_name, options = {}) + components = (wrapper.components.map(&:namespace) & ATTRIBUTE_COMPONENTS) + options = options.dup - options[:input_html] = options.except(:as, :boolean_style, :collection, :label_method, :value_method, *ATTRIBUTE_COMPONENTS) + options[:input_html] = options.except(:as, :boolean_style, :collection, :label_method, :value_method, *components) options = @defaults.deep_dup.deep_merge(options) if @defaults input = find_input(attribute_name, options) wrapper = find_wrapper(input.input_type, options) - components = (wrapper.components.map(&:namespace) & ATTRIBUTE_COMPONENTS) + [:input] - components = components.map { |component| SimpleForm::Wrappers::Leaf.new(component) } + components = components.concat([:input]).map { |component| SimpleForm::Wrappers::Leaf.new(component) } SimpleForm::Wrappers::Root.new(components, wrapper.options.merge(wrapper: false)).render input end diff --git a/test/form_builder/input_field_test.rb b/test/form_builder/input_field_test.rb index aefc88e8..687261c2 100644 --- a/test/form_builder/input_field_test.rb +++ b/test/form_builder/input_field_test.rb @@ -146,4 +146,44 @@ class InputFieldTest < ActionView::TestCase assert_no_select 'input.boolean[boolean_style]' end + + test 'build input_field without pattern component use the pattern string' do + swap_wrapper :default, self.custom_wrapper_with_html5_components do + with_concat_form_for(@user) do |f| + f.input_field :name, pattern: '\w+' + end + + assert_select 'input[pattern="\w+"]' + end + end + + test 'build input_field without placeholder component use the placeholder string' do + swap_wrapper :default, self.custom_wrapper_with_html5_components do + with_concat_form_for(@user) do |f| + f.input_field :name, placeholder: 'Placeholder' + end + + assert_select 'input[placeholder="Placeholder"]' + end + end + + test 'build input_field without maxlength component use the maxlength string' do + swap_wrapper :default, self.custom_wrapper_with_html5_components do + with_concat_form_for(@user) do |f| + f.input_field :name, maxlength: 5 + end + + assert_select 'input[maxlength="5"]' + end + end + + test 'build input_field without readonly component use the readonly string' do + swap_wrapper :default, self.custom_wrapper_with_html5_components do + with_concat_form_for(@user) do |f| + f.input_field :name, readonly: true + end + + assert_select 'input[readonly="readonly"]' + end + end end diff --git a/test/support/misc_helpers.rb b/test/support/misc_helpers.rb index 0f465907..1cf67f28 100644 --- a/test/support/misc_helpers.rb +++ b/test/support/misc_helpers.rb @@ -178,6 +178,12 @@ module MiscHelpers end end + def custom_wrapper_with_html5_components + SimpleForm.build tag: :span, class: 'custom_wrapper' do |b| + b.use :label_text + end + end + def custom_form_for(object, *args, &block) simple_form_for(object, *args, { builder: CustomFormBuilder }, &block) end