-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #203 from DFE-Digital/version-2.0.0-pp-accordion
Give accordion feature parity with nunjucks macros
- Loading branch information
Showing
6 changed files
with
194 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 1 addition & 11 deletions
12
app/components/govuk_component/accordion_component.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,5 @@ | ||
<%= tag.div(id: @id, class: classes, data: { module: 'govuk-accordion' }, **html_attributes) do %> | ||
<% sections.each do |section| %> | ||
<%= tag.div(id: section.id(suffix: 'section'), class: section.classes, **section.html_attributes) do %> | ||
<div class="govuk-accordion__section-header"> | ||
<h2 class="govuk-accordion__section-heading"> | ||
<%= tag.span(section.title, id: section.id, class: "govuk-accordion__section-button", aria: { expanded: section.expanded?, controls: section.id(suffix: 'content') }) %> | ||
</h2> | ||
<% if section.summary.present? %> | ||
<%= tag.div(section.summary, id: section.id(suffix: 'summary'), class: %w(govuk-accordion__section-summary govuk-body)) %> | ||
<% end %> | ||
</div> | ||
<%= section %> | ||
<% end %> | ||
<%= section %> | ||
<% end %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
app/components/govuk_component/accordion_component/section_component.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<%= tag.div(id: id(suffix: 'section'), class: classes, **html_attributes) do %> | ||
<div class="govuk-accordion__section-header"> | ||
<%= content_tag(heading_level, class: "govuk-accordion__section-heading") do %> | ||
<%= tag.span(heading_content, id: id, class: "govuk-accordion__section-button", aria: { expanded: expanded?, controls: id(suffix: 'content') }) %> | ||
<% end %> | ||
<% if summary_content.present? %> | ||
<%= tag.div(summary_content, id: id(suffix: 'summary'), class: %w(govuk-accordion__section-summary govuk-body)) %> | ||
<% end %> | ||
</div> | ||
<%= tag.div(content, id: id(suffix: 'content'), class: %w(govuk-accordion__section-content), aria: { labelledby: id }) %> | ||
<% end %> |
41 changes: 41 additions & 0 deletions
41
app/components/govuk_component/accordion_component/section_component.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
class GovukComponent::AccordionComponent::SectionComponent < GovukComponent::Base | ||
attr_reader :heading_text, :summary_text, :expanded, :heading_level | ||
|
||
renders_one :heading_html | ||
renders_one :summary_html | ||
|
||
alias_method :expanded?, :expanded | ||
|
||
def initialize(heading_text:, summary_text:, expanded:, heading_level:, classes: [], html_attributes: {}) | ||
super(classes: classes, html_attributes: html_attributes) | ||
|
||
@heading_text = heading_text | ||
@summary_text = summary_text | ||
@expanded = expanded | ||
@heading_level = heading_level | ||
end | ||
|
||
def id(suffix: nil) | ||
# generate a random number if we don't have heading_text to avoid attempting | ||
# to parameterize a potentially-huge chunk of HTML | ||
@prefix ||= heading_text&.parameterize || SecureRandom.hex(4) | ||
|
||
[@prefix, suffix].compact.join('-') | ||
end | ||
|
||
def heading_content | ||
heading_html || heading_text || fail(ArgumentError, "no heading_text or heading_html") | ||
end | ||
|
||
def summary_content | ||
summary_html || summary_text | ||
end | ||
|
||
private | ||
|
||
def default_classes | ||
%w(govuk-accordion__section).tap do |classes| | ||
classes.append("govuk-accordion__section--expanded") if expanded? | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters