Class: CCS::Components::GovUK::Field::Inputs

Inherits:
CCS::Components::GovUK::Field show all
Defined in:
lib/ccs/components/govuk/field/inputs.rb,
lib/ccs/components/govuk/field/inputs/item.rb,
lib/ccs/components/govuk/field/inputs/radios.rb,
lib/ccs/components/govuk/field/inputs/checkboxes.rb,
lib/ccs/components/govuk/field/inputs/date_input.rb,
lib/ccs/components/govuk/field/inputs/item/radio.rb,
lib/ccs/components/govuk/field/inputs/item/divider.rb,
lib/ccs/components/govuk/field/inputs/item/checkbox.rb,
lib/ccs/components/govuk/field/inputs/item/radio/tag.rb,
lib/ccs/components/govuk/field/inputs/date_input/item.rb,
lib/ccs/components/govuk/field/inputs/item/radio/form.rb,
lib/ccs/components/govuk/field/inputs/item/checkbox/tag.rb,
lib/ccs/components/govuk/field/inputs/item/checkbox/form.rb

Overview

GOV.UK Field Inputs

This class is used to create a form for a list of fields, e.g. radios or checkboxes. It will wrap the inputs in the form group and then the fieldset and:

  • display the hint (if there is one)

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

Direct Known Subclasses

Checkboxes, DateInput, Radios

Defined Under Namespace

Classes: Checkboxes, DateInput, Item, Radios

Constant Summary

Constants inherited from Base

Base::DEFAULT_ATTRIBUTES

Instance Method Summary collapse

Constructor Details

#initialize(attribute:, fieldset: nil, **options) ⇒ Inputs

Returns a new instance of Inputs.

Parameters:

  • fieldset (Hash) (defaults to: nil)

    attributes for the fieldset, see Fieldset#initialize for more details.

  • 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



32
33
34
35
36
37
38
39
40
# File 'lib/ccs/components/govuk/field/inputs.rb', line 32

def initialize(attribute:, fieldset: nil, **options)
  super(attribute: attribute, **options)

  return unless fieldset

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

  @fieldset = Fieldset.new(context: @context, **fieldset)
end

Instance Method Details

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

Generates the HTML to wrap arround a GDS form component

Yields:

  • the field HTML

Returns:

  • (ActiveSupport::SafeBuffer)


48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ccs/components/govuk/field/inputs.rb', line 48

def render(&block)
  super() do |display_error_message|
    if fieldset
      fieldset.render do
        field_inner_html(display_error_message, &block)
      end
    else
      field_inner_html(display_error_message, &block)
    end
  end
end