Class: CCS::Components::GovUK::Field::Input::Select
- Inherits:
-
CCS::Components::GovUK::Field::Input
- Object
- Base
- CCS::Components::GovUK::Field
- CCS::Components::GovUK::Field::Input
- CCS::Components::GovUK::Field::Input::Select
- 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
-
#initialize(attribute:, items:, selected: nil, **options) ⇒ Select
constructor
A new instance of Select.
-
#render ⇒ ActiveSupport::SafeBuffer
Generates the HTML for the GOV.UK Select component.
Constructor Details
#initialize(attribute:, items:, selected: nil, **options) ⇒ Select
Returns a new instance of Select.
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 |
Instance Method Details
#render ⇒ ActiveSupport::SafeBuffer
Generates the HTML for the GOV.UK Select component
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 [:form] [:form].select( attribute, items, {}, **[:attributes] ) else context.select_tag( attribute, context.( items, selected ), **[:attributes] ) end end end |