Class: CCS::Components::GovUK::Field::Input::TextInput

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

Overview

GOV.UK Input

This is used for generating the input component from the GDS - Components - Text Input

Defined Under Namespace

Classes: Fix

Constant Summary collapse

DEFAULT_ATTRIBUTES =

The default attributes for the text input

{ class: 'govuk-input' }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(attribute:, field_type: :text, value: nil, prefix: nil, suffix: nil, input_wrapper: {}, **options) ⇒ TextInput

Returns a new instance of TextInput.

Parameters:

  • field_type (Symbol, String) (defaults to: :text)

    (:text) the input field type

  • value (String) (defaults to: nil)

    the value if the input

  • prefix (Hash) (defaults to: nil)

    optional prefix for the input field, see Fix#initialize for more details.

  • suffix (Hash) (defaults to: nil)

    optional suffix for the input field, see Fix#initialize for more details.

  • input_wrapper (Hash) (defaults to: {})

    HTML options for the input wrapper

  • 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



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ccs/components/govuk/field/input/text_input.rb', line 45

def initialize(attribute:, field_type: :text, value: nil, prefix: nil, suffix: nil, input_wrapper: {}, **options)
  super(attribute: attribute, **options)

  @field_type = :"#{field_type}_field"
  @value = @options[:model] ? @options[:model].send(attribute) : value
  @prefix = Fix.new(fix: 'pre', context: @context, **prefix) if prefix
  @suffix = Fix.new(fix: 'suf', context: @context, **suffix) if suffix
  @input_wrapper_html_options = {
    class: "govuk-input__wrapper #{input_wrapper[:classes]}".rstrip
  }.merge(input_wrapper[:attributes] || {})
end

Instance Method Details

#renderActiveSupport::SafeBuffer

Generates the HTML for the GOV.UK Text Input component

Returns:

  • (ActiveSupport::SafeBuffer)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ccs/components/govuk/field/input/text_input.rb', line 64

def render
  form_group.render do |display_error_message|
    concat(label.render)
    concat(hint.render) if hint
    concat(display_error_message)
    concat(text_input_wrapper do
      if options[:form]
        options[:form].send(field_type, attribute, **options[:attributes])
      else
        context.send("#{field_type}_tag", attribute, value, **options[:attributes])
      end
    end)
  end
end