Sha256: dc12d32dc5ea1e930dc819bce887f555bcd3d4c4feda85c6293eea849f380b13

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 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

1 entries across 1 versions & 1 rubygems

Version Path
govuk_design_system_formbuilder-0.7.10 lib/govuk_design_system_formbuilder/elements/select.rb