Skip to content

Commit

Permalink
Add custom heading level to summary cards (#573)
Browse files Browse the repository at this point in the history
This extends the summary card to allow for changing the heading level
from the default `h2`
  • Loading branch information
peteryates authored Dec 5, 2024
2 parents e2728ec + b0f71d0 commit af6c7d2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%= tag.div(**html_attributes) do %>
<div class="<%= brand %>-summary-card__title-wrapper">
<%= tag.h2(title, class: "#{brand}-summary-card__title") %>
<%= content_tag(heading_level, title, class: "#{brand}-summary-card__title") %>

<% if actions.any? %>
<ul class="<%= brand %>-summary-card__actions">
Expand Down
Original file line number Diff line number Diff line change
@@ -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_tag(heading_level)
actions.each { |a| with_action { a } } if actions.any?

super(classes:, html_attributes:)
Expand All @@ -17,6 +18,12 @@ def default_attributes
{ class: "#{brand}-summary-card" }
end

def heading_tag(level)
fail(ArgumentError, "heading_level must be 1-6") unless level.in?(1..6)

"h#{level}"
end

def action_text(action)
safe_join([action, tag.span(" (" + title + ")", class: "#{brand}-visually-hidden")])
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,27 @@
expect(rendered_content).to have_tag("h2", text: title, with: { class: "govuk-summary-card__title" })
end

context "with a custom heading level" do
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' })
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

specify "card contains a summary list" do
expect(rendered_content).to have_tag("dl", with: { class: "govuk-summary-list" })
end
Expand Down

0 comments on commit af6c7d2

Please sign in to comment.