Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom heading level to summary cards #573

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading