-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add 'append and prepend' wrapper #531
Conversation
@mfo sounds great, thanks! I'm just wondering if we should stick with |
Since those are all the same, I think instead we should make the internal div a named parameter and get rid of those different wrappers:
And then in the view:
Another option is to have a wrapper_input component. But I think that having 4 components exactly the same is a bit not-dry. :) (Alternatively we could wrap those in a lambda to dry up the config file) |
I like the José idea. Can we try it? |
@carlosantoniodasilva : right :-D, but jose submitted a great idea :-) Thx for your feedbacks guys |
I came up with the following quick solution. (the classname is not configurable, as I don't need really need it to be)
I'd love to learn how could I improve this :) |
We should just allow all inputs to accept We can't use :hint for this, because we need the text to be placed right next to the checkbox so that it's still within the label. Providing a general |
Actually, I found that there's an undocumented |
@dlee Ah, you just saved me (possibly) hours of work with that Thanks! ;) |
A :prepend/:append option seems useful. @carlosantoniodasilva, @rafaelfranca wdyt? |
@josevalim @dlee is it reasonable to add both ( |
@zeppelin it seems reasonable. I also think you can easily do that today by passing a block to simple form:
But it has the downside of forcing you to pass twice the field twice. PS: not sure if it is called @dlee documentation for that option would be welcome :) |
Wow, quick responses. @josevalim I saw your comment after I posted the Issue. Didn't mean to do a judo chop on you. @zeppelin I think the |
@dlee I think what @josevalim demonstrated takes care of the case where you want to provide the prepend/append HTML as-is. I was thinking about the To illustrate my point: It does not, however, stops us from adding some polymorphism to the mix: But I guess the latter is a question of API-consistency. |
I don't like the idea of add HTML tags in an option of the input method. As @josevalim said, we can use the block syntax to do such of thing. PS: |
I'm with @rafaelfranca on this one, I think there's no need for new extra options, but I do think we can make our little @josevalim's example would look something like: <%# ps: there's no variable passed to the block %>
<%= f.input :sample do %>
Prepend stuff
<%= f.input_field :sample %>
Append stuff
<% end %> We can make it a bit smarter by truly yielding a builder to the block: <%= f.input :sample do |b| %>
Prepend stuff
<%= b.input_field %>
Append stuff
<% end %> This would avoid you from having to provide the input name twice, and would nicely solve the prepend/append problem in my opinion. Thoughts? |
I like this idea. |
👍 |
@carlosantoniodasilva You mean that |
@zeppelin option was rejected in favor of blocks. I'll work on this issue |
@zeppelin I think that if the block change solves the problem of prepending / appending any text / html, then I'd go with it instead of adding new options to the api. |
@carlosantoniodasilva @rafaelfranca I have to agree on the API argument. |
I like @carlosantoniodasilva's idea as far as that making sense, but I thought the main purpose of this was to simplify the markup for using prepend and append. See #525. # From this (used proposed syntax)
= f.input :cost, wrapper => :prepend_and_append do |n|
= content_tag :span, "$", class: "add-on"
= n.number_field
= content_tag :span, ".00", class: "add-on"
# To this
= f.input :cost, prepend: "$", append: ".00" The wrapper class would only add the prepend class if the prepend option was used and vice versa. Or it would add both if both options were used. |
The problem with this approach is that we are coupling SimpleForm with twitter bootstrap. I definitely don't want to do this. Our intention, since the beginning, was to provide a easy way to configure SimpleForm with Third-party CSS libraries, not couple SimpleForm with they. If the library change we don't want to change SimpleForm. For example, when we release SimpleForm 2.0 we didn't have the prepend-append input type. I don't want to change SimpleForm if the Twitter bootstrap add a new component called Right now you can create easily a custom wrapper, input or maybe a helper in your application do to this. I really want to add suport to prepend-append inputs, but not adding new options to the inputs. |
Ok, I understand that reasoning, but I think it could be designed so that initializers could set some of the options. Maybe if some of the other CSS libraries add prepend/append functionality, then I can pester you into adding support for it, haha. Although, the use cases for prepend/append can get complicated (using checkboxes or buttons instead of the simple text example I have given), so it might be best to leave it out. However, I don't see how this proposed change handles the initial problem for this issue. There still isn't a |
I tried to block method to prepend an add-on, but it breaks the placeholder.
After:
Before, I get an automatic placeholder from I18n. After, the placeholder is gone. |
@yemartin that's weird, using |
Another use-case, as I've not seen this discussed (with replies) on the mailing-list or wiki/Nested-Models. I would like to append (or prepend) arbitrary HTML by calling a helper, rather than "append" implying a specific tag ( When used with I do this now as discussed above by @josevalim, @carlosantoniodasilva and @rafaelfranca: simple_nested_form_for @book do |book|
book.simple_fields_for :authors do |author|
author.input :name do
author.input_field :name
author.link_to_remove "Remove"
end I don't think I can do this with a wrapper, because Bottom line, I would like a less-verbose usage. For example: author.input :name, removable: true Or perhaps it belongs as a option at the collection level: book.simple_fields_for :authors, removable: true do |author|
author.input :name
end |
Here's a quick and easy solution that is DRY: wrapper_class = "input-prepend"
options = { tag: "div", class: "control-group", error_class: "error" }
block = lambda do |b|
b.use :html5
b.use :placeholder
b.use :label
b.wrapper :tag => 'div', :class => 'controls' do |input|
input.wrapper :tag => 'div', :class => wrapper_class do |prepend|
prepend.use :input
end
input.use :hint, :wrap_with => { :tag => 'span', :class => 'help-block' }
input.use :error, :wrap_with => { :tag => 'span', :class => 'help-inline' }
end
end
config.wrappers :prepend, options, &block
wrapper_class = "input-append"
config.wrappers :append, options, &block
wrapper_class = "input-prepend input-append"
config.wrappers :"prepend append", options, &block
config.wrappers :"append prepend", options, &block Use like this: <%= f.input :foo, wrapper: "append prepend" do %>
<% end %> Or <%= f.input :foo, wrapper: "prepend append" do %>
<% end %> This makes it look like you can specify multiple wrappers. I don't know if you think that's good or bad. |
Late to the party here, but after reading this whole page, I'm still not clear about whether or not this is actually easily doable to have a prepend-append kind of style around an input. I've found solution involving modifications to Rails initializers, none of them are really satisfactory. We need to present domain name reservations on our website and it'd be extremely helpful. Thanks. |
Easiest way to do it now is this FYI <%= f.input :field, :wrapper_html => {class: "input-prepend input-append"} do %>
<span class="add-on">$</span>
<%= f.input_field :field %>
<span class="add-on">%</span>
<% end %>
|
@bcackerman Thank you. Much appreciated. |
Thank you! This information should really be added to the homepage. |
This is not need to bootstrap 3 so I'm closing. Thank you for the PR |
Hi ;
at sharypic we needed the 'append and prepend' style of the bootstrap (see : http://twitter.github.com/bootstrap/base-css.html#forms or a small screen shot http://cl.ly/1B451f1v3x311k193u1i)
So i updated your generator, hope it helps.
Best, Martin.
PS: if something is messing let me know ;-)