Class: CCS::Components::GovUK::Field::Input::Textarea

Inherits:
CCS::Components::GovUK::Field::Input show all
Defined in:
lib/ccs/components/govuk/field/input/textarea.rb

Overview

GOV.UK Textarea

This helper is used for generating the textarea component from the GDS - Components - Textarea

Constant Summary collapse

DEFAULT_ATTRIBUTES =

The default attributes for the textarea

{ class: 'govuk-textarea' }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(attribute:, content: nil, rows: 5, **options) ⇒ Textarea

Returns a new instance of Textarea.

Parameters:

  • content (String) (defaults to: nil)

    the content of the textarea

  • rows (Integer) (defaults to: 5)

    the number of rows for the text area

  • label (Hash)

    attributes for the label, see Label#initialize for more details.

  • before_input (String)

    text or HTML to go before the input

  • after_input (String)

    text or HTML to go after the input

  • attribute (String, Symbol)

    the attribute of the field

  • hint (Hash)

    attributes for the hint, see Hint#initialize for more details. If no hint is given then no hint will be rendered

  • form_group (Hash)

    attributes for the form group, see FormGroup#initialize for more details.

  • options (Hash)

    options that will be used for the parts of the field

Options Hash (**options):

  • :error_message (String) — default: nil

    the error message to be displayed

  • :model (ActiveModel) — default: nil

    optional model that can be used to find an error message

  • :form (ActionView::Helpers::FormBuilder) — default: nil

    optional form builder used to create the field and find the error message

  • :classes (String)

    additional CSS classes for the field HTML

  • :attributes (Hash) — default: {}

    any additional attributes that will added as part of the HTML



29
30
31
32
33
34
35
# File 'lib/ccs/components/govuk/field/input/textarea.rb', line 29

def initialize(attribute:, content: nil, rows: 5, **options)
  super(attribute: attribute, **options)

  @options[:attributes][:rows] ||= rows

  @content = @options[:model] ? @options[:model].send(attribute) : content
end

Instance Method Details

#renderActiveSupport::SafeBuffer

Generates the HTML for the GOV.UK Textarea component

Returns:

  • (ActiveSupport::SafeBuffer)


41
42
43
44
45
46
47
48
49
# File 'lib/ccs/components/govuk/field/input/textarea.rb', line 41

def render
  super do
    if options[:form]
      options[:form].text_area(attribute, **options[:attributes])
    else
      context.text_area_tag(attribute, content, **options[:attributes])
    end
  end
end