Method: CCS::Components::GovUK::Field::Input::Select#initialize

Defined in:
lib/ccs/components/govuk/field/input/select.rb

#initialize(attribute:, items:, selected: nil) ⇒ Select

Returns a new instance of Select.

Parameters:

  • items (Array<Hash>)

    array of options for select. The options are:

    • :text the text of the option item. If this is blank the value is used

    • :value the value of the option item

    • :attributes any additional attributes that will added as part of the option HTML

  • selected (String) (defaults to: nil)

    the selected option

  • 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



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

def initialize(attribute:, items:, selected: nil, **)
  super(attribute: attribute, **)

  @items = items.map do |item|
    [
      item[:text] || item[:value],
      item[:value].nil? ? item[:text] : item[:value],
      item[:attributes] || {}
    ]
  end
  @selected = @options[:model] ? @options[:model].send(attribute) : selected
end