Skip to content

Commit

Permalink
Allow configuring textarea input options (#2665)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
balvig authored Oct 15, 2024
1 parent f384038 commit 3cd1315
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/views/fields/text/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ This partial renders a textarea element for a text attribute.
<%= f.label field.attribute %>
</div>
<div class="field-unit__field">
<%= f.text_area field.attribute %>
<%= f.text_area field.attribute, field.options.fetch(:input_options, {}) %>
</div>
3 changes: 3 additions & 0 deletions docs/customizing_dashboards.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ Default is `false`.
`:truncate` - Set the number of characters to display in the index view.
Defaults to `50`.

`:input_options` - Options to customize the text area in form view.
Example: `.with_options(input_options: { rows: 20 })`

**Field::Url**

`:searchable` - Specify if the attribute should be considered when searching.
Expand Down
56 changes: 56 additions & 0 deletions spec/administrate/views/fields/text/_form_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require "rails_helper"
require "administrate/field/text"

RSpec.describe "fields/text/_form", type: :view do
it "allows configuring input options" do
textarea = instance_double(
"Administrate::Field::Text",
attribute: :name,
data: nil,
options: {input_options: {rows: 50}}
)

render_partial(textarea)

expect(rendered).to have_css(%(textarea[rows=50]))
end

it "doesn't require input options" do
textarea = instance_double(
"Administrate::Field::Text",
attribute: :name,
data: nil,
options: {}
)

render_partial(textarea)

expect(rendered).to have_css(%(textarea))
end

def render_partial(field)
product = build(:product)

render(
partial: "fields/text/form",
locals: {field: field, f: form_builder(product)}
)
end

def form_builder(object)
ActionView::Helpers::FormBuilder.new(
object.model_name.singular,
object,
build_template,
{}
)
end

def build_template
Object.new.tap do |template|
template.extend ActionView::Helpers::FormHelper
template.extend ActionView::Helpers::FormOptionsHelper
template.extend ActionView::Helpers::FormTagHelper
end
end
end

0 comments on commit 3cd1315

Please sign in to comment.