Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

### New features

* [#557](https://github.com/bootstrap-ruby/bootstrap_form/pull/557): Allow prepending and appending multiple items to an input by passing an array to `prepend` and `append` options - [@donv](https://github.com/donv).
* Your contribution here!

### Bugfixes
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,12 @@ You can pass `prepend` and/or `append` options to input fields:
<%= f.text_field :price, prepend: "$", append: ".00" %>
```

If you want to attach multiple items to the input, pass them as an array:

```erb
<%= f.text_field :price, prepend: ['Net', '$'], append: ['.00', 'per day'] %>
```

You can also prepend and append buttons. Note: The buttons must contain the
`btn` class to generate the correct markup.

Expand Down
15 changes: 6 additions & 9 deletions lib/bootstrap_form/helpers/bootstrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def prepend_and_append_input(name, options, &block)

input = capture(&block) || ActiveSupport::SafeBuffer.new

input = prepend_input(options) + input + append_input(options)
input = attach_input(options, :prepend) + input + attach_input(options, :append)
input += generate_error(name)
options.present? &&
input = content_tag(:div, input, class: ["input-group", options[:input_group_class]].compact)
Expand All @@ -106,14 +106,11 @@ def static_class

private

def append_input(options)
html = content_tag(:div, input_group_content(options[:append]), class: "input-group-append") if options[:append]
html || ActiveSupport::SafeBuffer.new
end

def prepend_input(options)
html = content_tag(:div, input_group_content(options[:prepend]), class: "input-group-prepend") if options[:prepend]
html || ActiveSupport::SafeBuffer.new
def attach_input(options, key)
tags = [*options[key]].map do |item|
content_tag(:div, input_group_content(item), class: "input-group-#{key}")
end
ActiveSupport::SafeBuffer.new(tags.join)
end

def setup_css_class(the_class, options={})
Expand Down
8 changes: 5 additions & 3 deletions test/bootstrap_form_group_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,18 @@ class BootstrapFormGroupTest < ActionView::TestCase
test "append and prepend button" do
prefix = '<div class="form-group"><label class="required" for="user_email">Email</label><div class="input-group">'
field = '<input class="form-control" id="user_email" name="user[email]" type="text" value="steve@example.com" />'
button_prepend = '<div class="input-group-prepend"><a class="btn btn-secondary" href="#">Click</a></div>'
button_append = '<div class="input-group-append"><a class="btn btn-secondary" href="#">Click</a></div>'
button_src = link_to("Click", "#", class: "btn btn-secondary")
button_prepend = "<div class=\"input-group-prepend\">#{button_src}</div>"
button_append = "<div class=\"input-group-append\">#{button_src}</div>"
suffix = "</div></div>"
after_button = prefix + field + button_append + suffix
before_button = prefix + button_prepend + field + suffix
both_button = prefix + button_prepend + field + button_append + suffix
button_src = link_to("Click", "#", class: "btn btn-secondary")
multiple_button = prefix + button_prepend + button_prepend + field + button_append + button_append + suffix
assert_equivalent_xml after_button, @builder.text_field(:email, append: button_src)
assert_equivalent_xml before_button, @builder.text_field(:email, prepend: button_src)
assert_equivalent_xml both_button, @builder.text_field(:email, append: button_src, prepend: button_src)
assert_equivalent_xml multiple_button, @builder.text_field(:email, append: [button_src] * 2, prepend: [button_src] * 2)
end

test "adding both prepend and append text" do
Expand Down