Skip to content

Commit 3cd1315

Browse files
authored
Allow configuring textarea input options (#2665)
This allows configuring the rendered `<textarea>` for `Field::Text` fields, e.g.: ATTRIBUTE_TYPES = { body: Field::Text.with_options(input_options: { rows: 20 }), }.freeze Will render: <textarea rows="20" name="post[body]" id="post_body">...</textarea>
1 parent f384038 commit 3cd1315

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

app/views/fields/text/_form.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ This partial renders a textarea element for a text attribute.
1818
<%= f.label field.attribute %>
1919
</div>
2020
<div class="field-unit__field">
21-
<%= f.text_area field.attribute %>
21+
<%= f.text_area field.attribute, field.options.fetch(:input_options, {}) %>
2222
</div>

docs/customizing_dashboards.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ Default is `false`.
267267
`:truncate` - Set the number of characters to display in the index view.
268268
Defaults to `50`.
269269

270+
`:input_options` - Options to customize the text area in form view.
271+
Example: `.with_options(input_options: { rows: 20 })`
272+
270273
**Field::Url**
271274

272275
`:searchable` - Specify if the attribute should be considered when searching.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
require "rails_helper"
2+
require "administrate/field/text"
3+
4+
RSpec.describe "fields/text/_form", type: :view do
5+
it "allows configuring input options" do
6+
textarea = instance_double(
7+
"Administrate::Field::Text",
8+
attribute: :name,
9+
data: nil,
10+
options: {input_options: {rows: 50}}
11+
)
12+
13+
render_partial(textarea)
14+
15+
expect(rendered).to have_css(%(textarea[rows=50]))
16+
end
17+
18+
it "doesn't require input options" do
19+
textarea = instance_double(
20+
"Administrate::Field::Text",
21+
attribute: :name,
22+
data: nil,
23+
options: {}
24+
)
25+
26+
render_partial(textarea)
27+
28+
expect(rendered).to have_css(%(textarea))
29+
end
30+
31+
def render_partial(field)
32+
product = build(:product)
33+
34+
render(
35+
partial: "fields/text/form",
36+
locals: {field: field, f: form_builder(product)}
37+
)
38+
end
39+
40+
def form_builder(object)
41+
ActionView::Helpers::FormBuilder.new(
42+
object.model_name.singular,
43+
object,
44+
build_template,
45+
{}
46+
)
47+
end
48+
49+
def build_template
50+
Object.new.tap do |template|
51+
template.extend ActionView::Helpers::FormHelper
52+
template.extend ActionView::Helpers::FormOptionsHelper
53+
template.extend ActionView::Helpers::FormTagHelper
54+
end
55+
end
56+
end

0 commit comments

Comments
 (0)