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

Inherits:
CCS::Components::GovUK::Field::Inputs show all
Defined in:
lib/ccs/components/govuk/field/inputs/radios.rb

Overview

GOV.UK Radios

This is used for generating the radios component from the GDS - Components - Radios

Constant Summary collapse

DEFAULT_ATTRIBUTES =

The default attributes for the radios

{ class: 'govuk-radios', data: { module: 'govuk-radios' } }.freeze

Instance Method Summary collapse

Methods inherited from CCS::Components::GovUK::Field::Inputs

#render

Methods inherited from CCS::Components::GovUK::Field

#render

Constructor Details

#initialize(attribute:, radio_items:, **options) ⇒ Radios

Returns a new instance of Radios.

Parameters:

  • radio_items (Array<Hash>)

    an array of options for the radios. See Radio#initialize for details of the items in the array.

  • fieldset (Hash)

    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



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

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

  @options[:value] = (@options[:model] || @options[:form].object).send(attribute) if @options[:model] || @options[:form]

  radio_items.each { |radio_item| radio_item[:checked] = @options[:value] == radio_item[:value] } if @options[:value]

  radio_item_class = @options[:form] ? Item::Radio::Form : Inputs::Item::Radio::Tag

  @input_items = radio_items.map { |radio_item| radio_item[:divider] ? Item::Divider.new(divider: radio_item[:divider], type: 'radios') : radio_item_class.new(attribute: attribute, form: @options[:form], context: @context, **radio_item) }
end