Feature request: wrap inputs

31 views
Skip to first unread message

Jørgen

unread,
Dec 12, 2010, 7:15:03 AM12/12/10
to SimpleForm
First of all: thanks for a great library!

For easily applying the "sliding door" CSS technique on simple_form
text fields, text areas and buttons, I would like a way to add wrapper
spans around <input> fields (not the combined label and input, just
the input). (maybe this should be generalized to allow wrappings
around labels too?)

I have tried to add it myself, but failed to find a good place in the
code to do it, so I send a feature request here in hope that somebody
else sees this as a nice addition.

Example config:

config.input_tag = :span (default: :nil)
config.input_class = :input_field

Example code:

module SimpleForm
module Inputs
class StringInput < Base
def input
input_html_options[:size] ||= [limit,
SimpleForm.default_input_size].compact.min
input_html_options[:maxlength] ||= limit if limit
input_html_options[:type] ||= input_type unless string?

"<span class='input_fieldr'>".html_safe +
@builder.text_field(attribute_name, input_html_options) + "</
span>".html_safe
end
end
end
end

Carlos Antonio da Silva

unread,
Dec 12, 2010, 7:34:05 AM12/12/10
to plataformate...@googlegroups.com
I believe you could create your own input type, inheriting from StringInput, and add the "span" wrapper in the input method, sth like:

module SimpleForm
  module Inputs
    class MyInput < StringInput

      def input
        template.content_tag(:span, super, :class => "input_field")
      end
    end
  end
end

Usage:

  f.input :foo, :as => :my_input

But remember that it will just work for your my_input specific type, and won't work for other types such as collection and number. Based on this you might figure out a way to handle that =)
--
At.
Carlos A. da Silva

Julio Protzek

unread,
Dec 12, 2010, 1:29:34 PM12/12/10
to plataformate...@googlegroups.com
If you are using jQuery:

$("input[type="text]").wrap("span")

Jørgen Austvik

unread,
Dec 13, 2010, 5:07:19 AM12/13/10
to plataformate...@googlegroups.com
On Sun, Dec 12, 2010 at 13:34, Carlos Antonio da Silva
<carlosanto...@gmail.com> wrote:
> I believe you could create your own input type, inheriting from StringInput,
> and add the "span" wrapper in the input method, sth like:
> module SimpleForm
>   module Inputs
>     class MyInput < StringInput
>       def input
>         template.content_tag(:span, super, :class => "input_field")
>       end
>     end
>   end
> end
> Usage:
>   f.input :foo, :as => :my_input
> But remember that it will just work for your my_input specific type, and
> won't work for other types such as collection and number. Based on this you
> might figure out a way to handle that =)

Thanks for the tip. I'll look into it.

It would involve changing adding a :at to every field in every form in
an application with 30-40 models, though, so I think I would still
prefer a configuration option :)

-J

Jørgen Austvik

unread,
Dec 13, 2010, 5:09:21 AM12/13/10
to plataformate...@googlegroups.com
On Sun, Dec 12, 2010 at 19:29, Julio Protzek <juliop...@gmail.com> wrote:
> If you are using jQuery:
> $("input[type="text]").wrap("span")

Thanks!

I am using jQuery, and this is a neat trick and workaround which lets
me add the style until the span's are in the html. The downside with
this trick is off course that the styling now is depending on
javascript being enabled.

-J

Reply all
Reply to author
Forward
0 new messages