Skip to content

Commit c7e283f

Browse files
authored
Allow prepending and appending multiple items to an input (bootstrap-ruby#557)
1 parent aa53f79 commit c7e283f

File tree

4 files changed

+18
-12
lines changed

4 files changed

+18
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
### New features
88

9+
* [#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).
910
* Your contribution here!
1011

1112
### Bugfixes

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,12 @@ You can pass `prepend` and/or `append` options to input fields:
276276
<%= f.text_field :price, prepend: "$", append: ".00" %>
277277
```
278278

279+
If you want to attach multiple items to the input, pass them as an array:
280+
281+
```erb
282+
<%= f.text_field :price, prepend: ['Net', '$'], append: ['.00', 'per day'] %>
283+
```
284+
279285
You can also prepend and append buttons. Note: The buttons must contain the
280286
`btn` class to generate the correct markup.
281287

lib/bootstrap_form/helpers/bootstrap.rb

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def prepend_and_append_input(name, options, &block)
8282

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

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

107107
private
108108

109-
def append_input(options)
110-
html = content_tag(:div, input_group_content(options[:append]), class: "input-group-append") if options[:append]
111-
html || ActiveSupport::SafeBuffer.new
112-
end
113-
114-
def prepend_input(options)
115-
html = content_tag(:div, input_group_content(options[:prepend]), class: "input-group-prepend") if options[:prepend]
116-
html || ActiveSupport::SafeBuffer.new
109+
def attach_input(options, key)
110+
tags = [*options[key]].map do |item|
111+
content_tag(:div, input_group_content(item), class: "input-group-#{key}")
112+
end
113+
ActiveSupport::SafeBuffer.new(tags.join)
117114
end
118115

119116
def setup_css_class(the_class, options={})

test/bootstrap_form_group_test.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,18 @@ class BootstrapFormGroupTest < ActionView::TestCase
149149
test "append and prepend button" do
150150
prefix = '<div class="form-group"><label class="required" for="user_email">Email</label><div class="input-group">'
151151
field = '<input class="form-control" id="user_email" name="user[email]" type="text" value="[email protected]" />'
152-
button_prepend = '<div class="input-group-prepend"><a class="btn btn-secondary" href="#">Click</a></div>'
153-
button_append = '<div class="input-group-append"><a class="btn btn-secondary" href="#">Click</a></div>'
152+
button_src = link_to("Click", "#", class: "btn btn-secondary")
153+
button_prepend = "<div class=\"input-group-prepend\">#{button_src}</div>"
154+
button_append = "<div class=\"input-group-append\">#{button_src}</div>"
154155
suffix = "</div></div>"
155156
after_button = prefix + field + button_append + suffix
156157
before_button = prefix + button_prepend + field + suffix
157158
both_button = prefix + button_prepend + field + button_append + suffix
158-
button_src = link_to("Click", "#", class: "btn btn-secondary")
159+
multiple_button = prefix + button_prepend + button_prepend + field + button_append + button_append + suffix
159160
assert_equivalent_xml after_button, @builder.text_field(:email, append: button_src)
160161
assert_equivalent_xml before_button, @builder.text_field(:email, prepend: button_src)
161162
assert_equivalent_xml both_button, @builder.text_field(:email, append: button_src, prepend: button_src)
163+
assert_equivalent_xml multiple_button, @builder.text_field(:email, append: [button_src] * 2, prepend: [button_src] * 2)
162164
end
163165

164166
test "adding both prepend and append text" do

0 commit comments

Comments
 (0)