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

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

Overview

GOV.UK Select

This is used for generating the select component from the GDS - Components - Select

Constant Summary collapse

DEFAULT_ATTRIBUTES =

The default attributes for the select

{ class: 'govuk-select' }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(attribute:, items:, selected: nil, **options) ⇒ 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

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



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, **options)
  super(attribute: attribute, **options)

  @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

Instance Method Details

#renderActiveSupport::SafeBuffer

Generates the HTML for the GOV.UK Select component

Returns:

  • (ActiveSupport::SafeBuffer)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ccs/components/govuk/field/input/select.rb', line 52

def render
  super do
    if options[:form]
      options[:form].select(
        attribute,
        items,
        {},
        **options[:attributes]
      )
    else
      context.select_tag(
        attribute,
        context.options_for_select(
          items,
          selected
        ),
        **options[:attributes]
      )
    end
  end
end