how to add a class to the input component in a wrapper in simple_form 2

前端 未结 5 1058
难免孤独
难免孤独 2021-02-04 04:57

I am trying to have class=\"text\" in my input fields when using a custom wrapper called :hinted in simple_form 2.0.0.rc

config.wrappers :hinted do          


        
5条回答
  •  [愿得一人]
    2021-02-04 05:55

    I had a similar problem, however it seems that this feature (the input_class one) was merged after the 3.0.0 version.

    So I tried to make a monkey patch for supporting at least the config.input_class = 'foo' code

    My intention is not to do a great monkey patch (in fact I like this article here for doing that - the monkey patch), well it is only an idea but it works, now I'm working with the SimpleForm v2.1.3 and Bootstrap 4 - alpha version (the last one is not important here but it is just for an information purpose)

    here is the code for the monkey patch:

    module SimpleForm
      mattr_accessor :input_class
      @@input_class = nil
    end
    module SimpleForm
      module Inputs
        class Base
          def html_options_for(namespace, css_classes)
            html_options = options[:"#{namespace}_html"]
            html_options = html_options ? html_options.dup : {}
            css_classes << html_options[:class] if html_options.key?(:class)
            css_classes << SimpleForm.input_class if namespace == :input && SimpleForm.input_class.present?
            html_options[:class] = css_classes unless css_classes.empty?
            html_options
          end
        end
      end
    end
    

    now you can do something like this:

    SimpleForm.setup do |config|
      # ...
      config.input_class = 'foo'
      #...
    end
    

提交回复
热议问题