Skip to content

Commit

Permalink
Allow the action suffix to be set manually
Browse files Browse the repository at this point in the history
As the summary list can either be have its card automatically rendered
around it with `govuk_summary_list(card: { title: "A card" })` or be
manually rendered inside a card with `govuk_summary_card(...) { govuk_summary_list(...) }`
it makes sense to support both approaches when setting the contextual
visually hidden text that follows the action text.

This change adds support for the latter by allowing action suffixes to
be passed in. If both the card and action_suffix are provided the
action_suffix will take precedence, giving designers a little more
flexibility with the assistive text should they need it. There might be
some situations where the card title doesn't make sense, or it should
be lowercased when used in the link, etc.
  • Loading branch information
peteryates committed Dec 10, 2023
1 parent 026229b commit f4e93fb
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
9 changes: 5 additions & 4 deletions app/components/govuk_component/summary_list_component.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
module GovukComponent
class SummaryListComponent < GovukComponent::Base
attr_reader :borders, :actions, :card
attr_reader :borders, :actions, :card, :action_suffix

renders_many :rows, ->(classes: [], html_attributes: {}, &block) do
GovukComponent::SummaryListComponent::RowComponent.new(
show_actions_column: @show_actions_column,
card_title: card&.title,
action_suffix: action_suffix || card&.title,
classes: classes,
html_attributes: html_attributes,
&block
)
end

def initialize(rows: nil, actions: true, borders: config.default_summary_list_borders, card: nil, classes: [], html_attributes: {})
def initialize(rows: nil, actions: true, borders: config.default_summary_list_borders, card: {}, action_suffix: nil, classes: [], html_attributes: {})
@borders = borders
@show_actions_column = actions
@card = GovukComponent::SummaryListComponent::CardComponent.new(**card) unless card.blank?
@card = GovukComponent::SummaryListComponent::CardComponent.new(**card) if card.present?
@action_suffix = action_suffix

super(classes: classes, html_attributes: html_attributes)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class GovukComponent::SummaryListComponent::ActionComponent < GovukComponent::Base
attr_reader :href, :text, :visually_hidden_text, :card_title, :attributes, :classes
attr_reader :href, :text, :visually_hidden_text, :action_suffix, :attributes, :classes

def initialize(href: nil, text: 'Change', visually_hidden_text: false, card_title: nil, classes: [], html_attributes: {})
def initialize(href: nil, text: 'Change', visually_hidden_text: false, action_suffix: nil, classes: [], html_attributes: {})
@visually_hidden_text = visually_hidden_text

if config.require_summary_list_action_visually_hidden_text && visually_hidden_text == false
Expand All @@ -12,7 +12,7 @@ def initialize(href: nil, text: 'Change', visually_hidden_text: false, card_titl

@href = href
@text = text
@card_title = card_title
@action_suffix = action_suffix
end

def render?
Expand All @@ -38,7 +38,7 @@ def action_text
end

def visually_hidden_content
return "#{visually_hidden_text} (#{card_title})" if card_title
return "#{visually_hidden_text} (#{action_suffix})" if action_suffix

visually_hidden_text
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class GovukComponent::SummaryListComponent::RowComponent < GovukComponent::Base
attr_reader :href, :visually_hidden_text, :show_actions_column, :card_title
attr_reader :href, :visually_hidden_text, :show_actions_column, :action_suffix

renders_one :key, GovukComponent::SummaryListComponent::KeyComponent
renders_one :value, GovukComponent::SummaryListComponent::ValueComponent
Expand All @@ -8,16 +8,16 @@ class GovukComponent::SummaryListComponent::RowComponent < GovukComponent::Base
href: href,
text: text,
visually_hidden_text: visually_hidden_text,
card_title: card_title,
action_suffix: action_suffix,
classes: classes,
html_attributes: html_attributes,
&block
)
end

def initialize(show_actions_column: nil, card_title: nil, classes: [], html_attributes: {})
def initialize(show_actions_column: nil, action_suffix: nil, classes: [], html_attributes: {})
@show_actions_column = show_actions_column
@card_title = card_title
@action_suffix = action_suffix

super(classes: classes, html_attributes: html_attributes)
end
Expand Down
39 changes: 37 additions & 2 deletions spec/components/govuk_component/summary_list_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,43 @@
end
end

specify "the summary list is wrapped in a card" do
expect(rendered_content).to have_tag("div", with: { class: "govuk-summary-card" })
specify "when the card parameters are passed into the summary list via card:" do
expect(rendered_content).to have_tag("div", with: { class: "govuk-summary-card" }) do
with_tag("dl", with: { class: "govuk-summary-list" })
end
end

specify "the summary card's title is included in the visually hidden action suffix" do
expect(rendered_content).to have_tag("dd", with: { class: "govuk-summary-list__actions" }) do
with_text("Change this row (Hi)")
with_tag("span", with: { class: "govuk-visually-hidden" }, text: %r{this row \(Hi\)})
end
end

context "when the action_suffix is present" do
subject! do
render_inline(described_class.new(card: { title: "Hi" }, action_suffix: "Hello", **kwargs)) do |component|
component.with_row { |row| helper.safe_join([row.with_key(text: "Key"), row.with_value(text: "Value"), row.with_action(href: "/a", visually_hidden_text: "this row")]) }
end
end

specify "the action_suffix overrides the card's title in action links" do
expect(rendered_content).to have_tag("dd", with: { class: "govuk-summary-list__actions" }) do
with_tag("span", with: { class: "govuk-visually-hidden" }, text: %r{this row \(Hello\)})
end
end
end
end

context "when the summary list is manually placed within a card and the title is set with action_suffix:" do
subject! do
render_inline(described_class.new(action_suffix: "Hi", **kwargs)) do |component|
component.with_row { |row| helper.safe_join([row.with_key(text: "Key"), row.with_value(text: "Value"), row.with_action(href: "/a", visually_hidden_text: "this row")]) }
end
end

specify "the summary list isn't wrapped in a card" do
expect(rendered_content).not_to have_tag("div", with: { class: "govuk-summary-card" })
end

specify "the summary card's title is included in the visually hidden action suffix" do
Expand Down

0 comments on commit f4e93fb

Please sign in to comment.