Sha256: 0b21c0c26daa0df6585334a8b8e3f103a59a4b440635e3e9ffcd27730d67e296

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

require_relative '../field'
require_relative '../label'

module CCS::Components
  module GovUK
    class Field < Base
      # = GOV.UK Field Input
      #
      # This class is used to create a form for an individual field, e.g. text input or select.
      # It will wrap the input in the form group and:
      # - display the label
      # - display the hint (if there is one)
      # - find and display the error message (if there is one)
      #
      # @!attribute [r] label
      #   @return [Label] The initialised label

      class Input < Field
        private

        attr_reader :label

        public

        # @param (see CCS::Components::GovUK::Field#initialize)
        # @param label [Hash] attributes for the label, see {CCS::Components::GovUK::Label#initialize Label#initialize} for more details.
        #
        # @option (see CCS::Components::GovUK::Field#initialize)

        def initialize(attribute:, label:, **options)
          super(attribute: attribute, **options)

          set_described_by(@options, @attribute, @error_message, options[:hint])

          @options[:attributes][:class] << " #{self.class::DEFAULT_ATTRIBUTES[:class]}--error" if @error_message

          field_id = @options.dig(:attributes, :id)
          (label[:attributes] ||= {})[:for] = field_id if field_id

          @label = Label.new(attribute: attribute, form: @options[:form], context: @context, **label)
        end

        # Generates the HTML to wrap arround a GDS form component
        #
        # @yield (see CCS::Components::GovUK::Field#render)
        #
        # @return [ActiveSupport::SafeBuffer]

        def render
          super() do |display_error_message|
            concat(label.render)
            concat(hint.render) if hint
            concat(display_error_message)
            concat(yield)
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ccs-frontend_helpers-0.1.0.rc.2 lib/ccs/components/govuk/field/input.rb