Merge pull request #1657 from TALlama/skip-valid-class-check-when-valid-class-is-nil

Skip valid_class check if no class defined
This commit is contained in:
Felipe Renan 2020-02-06 20:32:03 +02:00 committed by GitHub
commit ac5759cb80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 5 deletions

View File

@ -28,11 +28,16 @@ module SimpleForm
css += SimpleForm.additional_classes_for(:wrapper) do
input.additional_classes + [input.input_class]
end
css << (options[:wrapper_error_class] || @defaults[:error_class]) if input.has_errors?
css << (options[:wrapper_hint_class] || @defaults[:hint_class]) if input.has_hint?
css << (options[:wrapper_valid_class] || @defaults[:valid_class]) if input.valid?
css << html_class(:error_class, options) { input.has_errors? }
css << html_class(:hint_class, options) { input.has_hint? }
css << html_class(:valid_class, options) { input.valid? }
css.compact
end
def html_class(key, options)
css = (options[:"wrapper_#{key}"] || @defaults[key])
css if css && yield
end
end
end
end

View File

@ -59,6 +59,15 @@ class WrapperTest < ActionView::TestCase
assert_no_select 'input.is-invalid'
end
test 'wrapper does not determine if valid class is needed when it is set to nil' do
def @user.name; raise BOOM; end
with_form_for @user, :name, as: :file, wrapper: custom_wrapper_with_input_valid_class(valid_class: nil)
assert_select 'div'
assert_select 'input'
assert_no_select 'div.field_with_errors'
assert_no_select 'input.is-invalid'
end
test 'wrapper adds hint class for attribute with a hint' do
with_form_for @user, :name, hint: 'hint'
assert_select 'div.field_with_hint'

View File

@ -213,8 +213,8 @@ module MiscHelpers
end
end
def custom_wrapper_with_input_valid_class
SimpleForm.build tag: :div, class: "custom_wrapper", valid_class: :field_without_errors do |b|
def custom_wrapper_with_input_valid_class(valid_class: :field_without_errors)
SimpleForm.build tag: :div, class: "custom_wrapper", valid_class: valid_class do |b|
b.use :label
b.use :input, class: 'inline-class', valid_class: 'is-valid'
end