From e70043129702f8b2a19d902958aaa75467a32343 Mon Sep 17 00:00:00 2001
From: Adam Goldstone <13471320+agoldstone93@users.noreply.github.com>
Date: Wed, 20 Nov 2024 09:17:05 +0000
Subject: [PATCH 1/2] Add custom heading level to summary cards
---
.../summary_list_component/card_component.html.erb | 2 +-
.../summary_list_component/card_component.rb | 9 +++++++--
.../govuk_component/summary_list_card_component_spec.rb | 9 +++++++++
3 files changed, 17 insertions(+), 3 deletions(-)
diff --git a/app/components/govuk_component/summary_list_component/card_component.html.erb b/app/components/govuk_component/summary_list_component/card_component.html.erb
index 9e0a0701..9cc6cefe 100644
--- a/app/components/govuk_component/summary_list_component/card_component.html.erb
+++ b/app/components/govuk_component/summary_list_component/card_component.html.erb
@@ -1,6 +1,6 @@
<%= tag.div(**html_attributes) do %>
- <%= tag.h2(title, class: "#{brand}-summary-card__title") %>
+ <%= content_tag(heading_tag, title, class: "#{brand}-summary-card__title") %>
<% if actions.any? %>
diff --git a/app/components/govuk_component/summary_list_component/card_component.rb b/app/components/govuk_component/summary_list_component/card_component.rb
index 9c405370..c4a2756b 100644
--- a/app/components/govuk_component/summary_list_component/card_component.rb
+++ b/app/components/govuk_component/summary_list_component/card_component.rb
@@ -1,11 +1,12 @@
class GovukComponent::SummaryListComponent::CardComponent < GovukComponent::Base
- attr_reader :title
+ attr_reader :title, :heading_level
renders_many :actions
renders_one :summary_list, "GovukComponent::SummaryListComponent"
- def initialize(title:, actions: [], classes: [], html_attributes: {})
+ def initialize(title:, heading_level: 2, actions: [], classes: [], html_attributes: {})
@title = title
+ @heading_level = heading_level
actions.each { |a| with_action { a } } if actions.any?
super(classes:, html_attributes:)
@@ -17,6 +18,10 @@ def default_attributes
{ class: "#{brand}-summary-card" }
end
+ def heading_tag
+ "h#{heading_level}"
+ end
+
def action_text(action)
safe_join([action, tag.span(" (" + title + ")", class: "#{brand}-visually-hidden")])
end
diff --git a/spec/components/govuk_component/summary_list_card_component_spec.rb b/spec/components/govuk_component/summary_list_card_component_spec.rb
index 2a5df38d..6b907cd6 100644
--- a/spec/components/govuk_component/summary_list_card_component_spec.rb
+++ b/spec/components/govuk_component/summary_list_card_component_spec.rb
@@ -79,6 +79,15 @@
expect(rendered_content).to have_tag("h2", text: title, with: { class: "govuk-summary-card__title" })
end
+ context "with a custom heading level" do
+ let(:custom_heading_level) { 3 }
+ before { render_inline(described_class.new(title:, heading_level: custom_heading_level)) }
+
+ specify "the card has the right heading level" do
+ expect(rendered_content).to have_tag(%(h#{custom_heading_level}), with: { class: 'govuk-summary-card__title' })
+ end
+ end
+
specify "card contains a summary list" do
expect(rendered_content).to have_tag("dl", with: { class: "govuk-summary-list" })
end
From b0f71d07899634317113829c170905918ceb48e3 Mon Sep 17 00:00:00 2001
From: Adam Goldstone <13471320+agoldstone93@users.noreply.github.com>
Date: Mon, 2 Dec 2024 09:18:31 +0000
Subject: [PATCH 2/2] Raise error if heading level is not 1..6
---
.../card_component.html.erb | 2 +-
.../summary_list_component/card_component.rb | 8 +++++---
.../summary_list_card_component_spec.rb | 20 +++++++++++++++----
3 files changed, 22 insertions(+), 8 deletions(-)
diff --git a/app/components/govuk_component/summary_list_component/card_component.html.erb b/app/components/govuk_component/summary_list_component/card_component.html.erb
index 9cc6cefe..1d37541e 100644
--- a/app/components/govuk_component/summary_list_component/card_component.html.erb
+++ b/app/components/govuk_component/summary_list_component/card_component.html.erb
@@ -1,6 +1,6 @@
<%= tag.div(**html_attributes) do %>
- <%= content_tag(heading_tag, title, class: "#{brand}-summary-card__title") %>
+ <%= content_tag(heading_level, title, class: "#{brand}-summary-card__title") %>
<% if actions.any? %>
diff --git a/app/components/govuk_component/summary_list_component/card_component.rb b/app/components/govuk_component/summary_list_component/card_component.rb
index c4a2756b..0defc79d 100644
--- a/app/components/govuk_component/summary_list_component/card_component.rb
+++ b/app/components/govuk_component/summary_list_component/card_component.rb
@@ -6,7 +6,7 @@ class GovukComponent::SummaryListComponent::CardComponent < GovukComponent::Base
def initialize(title:, heading_level: 2, actions: [], classes: [], html_attributes: {})
@title = title
- @heading_level = heading_level
+ @heading_level = heading_tag(heading_level)
actions.each { |a| with_action { a } } if actions.any?
super(classes:, html_attributes:)
@@ -18,8 +18,10 @@ def default_attributes
{ class: "#{brand}-summary-card" }
end
- def heading_tag
- "h#{heading_level}"
+ def heading_tag(level)
+ fail(ArgumentError, "heading_level must be 1-6") unless level.in?(1..6)
+
+ "h#{level}"
end
def action_text(action)
diff --git a/spec/components/govuk_component/summary_list_card_component_spec.rb b/spec/components/govuk_component/summary_list_card_component_spec.rb
index 6b907cd6..366cdbc4 100644
--- a/spec/components/govuk_component/summary_list_card_component_spec.rb
+++ b/spec/components/govuk_component/summary_list_card_component_spec.rb
@@ -80,11 +80,23 @@
end
context "with a custom heading level" do
- let(:custom_heading_level) { 3 }
- before { render_inline(described_class.new(title:, heading_level: custom_heading_level)) }
+ context "when the heading level is valid" do
+ let(:custom_heading_level) { 3 }
+ before { render_inline(described_class.new(title:, heading_level: custom_heading_level)) }
- specify "the card has the right heading level" do
- expect(rendered_content).to have_tag(%(h#{custom_heading_level}), with: { class: 'govuk-summary-card__title' })
+ specify "the card has the right heading level" do
+ expect(rendered_content).to have_tag(%(h#{custom_heading_level}), with: { class: 'govuk-summary-card__title' })
+ end
+ end
+
+ context "when the heading level is invalid" do
+ let(:custom_heading_level) { 8 }
+
+ specify "has the overriden level" do
+ expected_message = "heading_level must be 1-6"
+
+ expect { described_class.new(title:, heading_level: custom_heading_level) }.to raise_error(ArgumentError, expected_message)
+ end
end
end