Sha256: 890a13b73c0362881187870bbabbfb99331b1c3cdff1c8a96d386fa1bef568f4

Contents?: true

Size: 1.58 KB

Versions: 3

Compression:

Stored size: 1.58 KB

Contents

module GOVUKDesignSystemFormBuilder
  module Elements
    class Select < GOVUKDesignSystemFormBuilder::Base
      def initialize(builder, object_name, attribute_name, collection, value_method:, text_method:, options: {}, html_options: {}, hint_text:, label:, &block)
        super(builder, object_name, attribute_name)

        @collection    = collection
        @value_method  = value_method
        @text_method   = text_method
        @options       = options
        @html_options  = html_options
        @label         = label
        @hint_text     = hint_text
        @block_content = @builder.capture { block.call } if block_given?
      end

      def html
        Containers::FormGroup.new(@builder, @object_name, @attribute_name).html do
          @builder.safe_join(
            [
              label_element.html,
              hint_element.html,
              error_element.html,
              @block_content,
              @builder.collection_select(
                @attribute_name,
                @collection,
                @value_method,
                @text_method,
                @options,
                build_html_options(hint_element, error_element)
              )
            ]
          )
        end
      end

    private

      def build_html_options(hint_element, error_element)
        @html_options.deep_merge(
          id: field_id(link_errors: true),
          class: select_classes,
          aria: { describedby: described_by(hint_element.hint_id, error_element.error_id) }
        )
      end

      def select_classes
        %w(govuk-select)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
govuk_design_system_formbuilder-0.9.2 lib/govuk_design_system_formbuilder/elements/select.rb
govuk_design_system_formbuilder-0.9.1 lib/govuk_design_system_formbuilder/elements/select.rb
govuk_design_system_formbuilder-0.9.0 lib/govuk_design_system_formbuilder/elements/select.rb