simple_form icon indicating copy to clipboard operation
simple_form copied to clipboard

Wrapper API - error_class is ignored in div wrapper

Open abevoelker opened this issue 4 years ago • 4 comments

Environment

  • Ruby 2.7.2
  • Rails 6.1.1
  • Simple Form 5.1.0

Current behavior

I wrote the following wrapper to create a Tailwind text input:

config.wrappers :tailwind_text_input, tag: 'div', class: '', error_class: '', valid_class: '' do |b|
  b.use :html5
  b.use :placeholder
  b.optional :maxlength
  b.optional :minlength
  b.optional :pattern
  b.optional :min_max
  b.optional :readonly

  b.use :label, class: "block text-sm font-medium text-gray-700"
  b.wrapper tag: 'div', class: 'mt-1', error_class: 'relative rounded-md shadow-sm' do |c|
    c.use :input,
      class: 'appearance-none block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm',
      error_class: 'block w-full pr-10 border-red-300 text-red-900 placeholder-red-300 focus:outline-none focus:ring-red-500 focus:border-red-500 sm:text-sm rounded-md'
    c.use :full_error, wrap_with: { tag: 'p', class: 'mt-2 text-sm text-red-600' }
  end
end

However, when my input has an error, the b.wrapper tag: 'div' 's error_class is ignored - I only see the mt-1 class. If I add an error_class to the :label definition, I get error classes added to the <label> just fine.

I've had some other stumbling points with adding Tailwind support to Simple Form out of the box, but I'll put those comments under #1723.

Expected behavior

I would expect the error classes to be added to the div.

abevoelker avatar Feb 16 '21 15:02 abevoelker

I don't believe the wrapper there accepts the error_class and similar options, it seems to be only possible to add those to the label/input, and to the main wrappers builder around it, but not to inner wrappers like that.

carlosantoniodasilva avatar Feb 17 '21 11:02 carlosantoniodasilva

Ah I see. I got this working how I wanted with a custom input so I no longer need this feature. So I don't mind if this were to get closed.

abevoelker avatar Feb 17 '21 12:02 abevoelker

Changing one method in many.rb would fix this issue, I might make a MR for this since we need this as well.

  def html_classes(input, options)
    classes = []
    classes << @defaults[:class].dup
    classes << @defaults[:error_class].dup if input.has_errors?
    classes
  end

lafeber avatar Mar 03 '21 15:03 lafeber

Any update on this one?

yordis avatar May 23 '21 01:05 yordis