Sha256: f4062060b672a2ab73cac26563b5c3f308b264c953ac6d11e815b247b3027213

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 KB

Contents

require_relative '../base'

module CCS::Components
  module GovUK
    # = GOV.UK Error Message
    #
    # This is used to generate the error message component from the
    # {https://design-system.service.gov.uk/components/error-message GDS - Components - Error message}
    #
    # @!attribute [r] message
    #   @return [String] Text for the error message
    # @!attribute [r] attribute
    #   @return [String,Symbol] The attribute that has an error
    # @!attribute [r] visually_hidden_text
    #   @return [String] The visually hidden text before the error message

    class ErrorMessage < Base
      private

      attr_reader :message, :attribute, :visually_hidden_text

      public

      # @param message [String] the message to be displayed
      # @param attribute [String, Symbol] the attribute that has an error
      # @param options [Hash] options that will be used in customising the HTML
      #
      # @option options [String] :classes additional CSS classes for the error message HTML
      # @option options [String] :visually_hidden_text ('Error') visualy hidden text before the error message
      # @option options [Hash] :attributes ({}) any additional attributes that will be added as part of the HTML

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

        @options[:attributes][:id] ||= "#{attribute}-error"

        @message = message
        @attribute = attribute
        @visually_hidden_text = @options[:visually_hidden_text] || 'Error'
      end

      # Generates the HTML for the GOV.UK Error message component
      #
      # @return [ActiveSupport::SafeBuffer]

      def render
        tag.p(**options[:attributes]) do
          concat(tag.span("#{visually_hidden_text}: ", class: 'govuk-visually-hidden')) if visually_hidden_text.present?
          concat(message)
        end
      end

      # The default attributes for the error message

      DEFAULT_ATTRIBUTES = { class: 'govuk-error-message' }.freeze
    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/error_message.rb