Class: CCS::Components::GovUK::ErrorSummary

Inherits:
Base
  • Object
show all
Defined in:
lib/ccs/components/govuk/error_summary.rb,
lib/ccs/components/govuk/error_summary/item.rb

Overview

GOV.UK Error Summary

This is used to generate the error summary component from the GDS - Components - Error summary

Defined Under Namespace

Classes: Item

Constant Summary collapse

DEFAULT_ATTRIBUTES =

The default attributes for the error summary

{ class: 'govuk-error-summary', data: { module: 'govuk-error-summary' } }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(title:, error_summary_items: [], description: nil, **options) ⇒ ErrorSummary

Returns a new instance of ErrorSummary.

Parameters:

  • title (String)

    text to use for the heading of the error summary block

  • error_summary_items (Array<Hash>) (defaults to: [])

    the list of errors to include in the summary. See Item#initialize

  • description (String) (defaults to: nil)

    optional text to use for the description of the errors

  • options (Hash)

    options that will be used in customising the HTML

Options Hash (**options):

  • :classes (String)

    additional CSS classes for the error summary HTML

  • :attributes (Hash) — default: {}

    any additional attributes that will be added as part of the HTML



34
35
36
37
38
39
40
# File 'lib/ccs/components/govuk/error_summary.rb', line 34

def initialize(title:, error_summary_items: [], description: nil, **options)
  super(**options)

  @title = title
  @error_summary_items = error_summary_items.map { |error_summary_item| Item.new(context: @context, **error_summary_item) }
  @description = description
end

Instance Method Details

#renderActiveSupport::SafeBuffer

Generates the HTML for the GOV.UK Error summary component

Returns:

  • (ActiveSupport::SafeBuffer)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ccs/components/govuk/error_summary.rb', line 48

def render
  tag.div(**options[:attributes]) do
    tag.div(role: 'alert') do
      concat(tag.h2(title, class: 'govuk-error-summary__title'))
      concat(tag.div(class: 'govuk-error-summary__body') do
        concat(tag.p(description)) if description
        if error_summary_items.any?
          concat(tag.ul(class: 'govuk-list govuk-error-summary__list') do
            error_summary_items.each { |error_summary_item| concat(error_summary_item.render) }
          end)
        end
      end)
    end
  end
end