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

Inherits:
CCS::Components::GovUK::Field show all
Defined in:
lib/ccs/components/govuk/field/input.rb,
lib/ccs/components/govuk/field/input/select.rb,
lib/ccs/components/govuk/field/input/textarea.rb,
lib/ccs/components/govuk/field/input/text_input.rb,
lib/ccs/components/govuk/field/input/file_upload.rb,
lib/ccs/components/govuk/field/input/password_input.rb,
lib/ccs/components/govuk/field/input/text_input/fix.rb,
lib/ccs/components/govuk/field/input/character_count.rb,
lib/ccs/components/govuk/field/input/character_count/count_message.rb,
lib/ccs/components/govuk/field/input/password_input/show_hide_button.rb

Overview

GOV.UK Field Input

This class is used to create a form for an individual field, e.g. text input or select. It will wrap the input in the form group and:

  • display the label

  • display the hint (if there is one)

  • find and display the error message (if there is one)

Direct Known Subclasses

FileUpload, Select, TextInput, Textarea

Defined Under Namespace

Classes: CharacterCount, FileUpload, PasswordInput, Select, TextInput, Textarea

Constant Summary

Constants inherited from Base

Base::DEFAULT_ATTRIBUTES

Instance Method Summary collapse

Constructor Details

#initialize(attribute:, label:, **options) ⇒ Input

Returns a new instance of Input.

Parameters:

  • 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



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ccs/components/govuk/field/input.rb', line 33

def initialize(attribute:, label:, **options)
  super(attribute: attribute, **options)

  set_described_by(@options, @attribute, @error_message, options[:hint])

  @options[:attributes][:class] << " #{self.class::DEFAULT_ATTRIBUTES[:class]}--error" if @error_message

  field_id = @options.dig(:attributes, :id)
  (label[:attributes] ||= {})[:for] = field_id if field_id

  @label = Label.new(attribute: attribute, form: @options[:form], context: @context, **label)
end

Instance Method Details

#render { ... } ⇒ ActiveSupport::SafeBuffer

Generates the HTML to wrap arround a GDS form component

Yields:

  • the field HTML

Returns:

  • (ActiveSupport::SafeBuffer)


52
53
54
55
56
57
58
59
60
61
# File 'lib/ccs/components/govuk/field/input.rb', line 52

def render
  super do |display_error_message|
    concat(label.render)
    concat(hint.render) if hint
    concat(display_error_message)
    concat(before_input) if before_input
    concat(yield)
    concat(after_input) if after_input
  end
end