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

Bring the panel's functionality in line with the Nunjucks macros #215

Merged
merged 4 commits into from
Jul 24, 2021
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
40 changes: 25 additions & 15 deletions app/components/govuk_component/panel_component.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
class GovukComponent::PanelComponent < GovukComponent::Base
attr_reader :title, :body
attr_reader :id, :title_text, :text, :heading_level

def initialize(title: nil, body: nil, classes: [], html_attributes: {})
renders_one :title_html

def initialize(title_text: nil, text: nil, heading_level: 1, id: nil, classes: [], html_attributes: {})
super(classes: classes, html_attributes: html_attributes)

@title = title
@body = body
@heading_level = heading_level
@title_text = title_text
@text = text
@id = id
end

def call
tag.div(class: classes, **html_attributes) do
tag.div(id: id, class: classes, **html_attributes) do
safe_join([panel_title, panel_body].compact)
end
end
Expand All @@ -20,27 +24,33 @@ def default_classes
%w(govuk-panel govuk-panel--confirmation)
end

def display_title?
title.present?
def heading_tag
"h#{heading_level}"
end

def panel_content
content || text
end

def display_body?
body.present? || content.present?
def title
title_html || title_text
end

def panel_title
tag.h1(title, class: "govuk-panel__title") if display_title?
return if title.blank?

content_tag(heading_tag, title, class: "govuk-panel__title")
end

def panel_body
if display_body?
tag.div(class: "govuk-panel__body") do
content.presence || body
end
return if panel_content.blank?

tag.div(class: "govuk-panel__body") do
panel_content
end
end

def render?
display_title? || display_body?
title.present? || panel_content.present?
end
end
69 changes: 52 additions & 17 deletions spec/components/govuk_component/panel_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,85 @@

let(:component_css_class) { 'govuk-panel.govuk-panel--confirmation' }

let(:title) { 'Springfield' }
let(:body) { 'A noble spirit embiggens the smallest man' }
let(:kwargs) { { title: title, body: body } }
let(:title_text) { 'Springfield' }
let(:text) { 'A noble spirit embiggens the smallest man' }
let(:kwargs) { { title_text: title_text, text: text } }

it_behaves_like 'a component that accepts custom classes'
it_behaves_like 'a component that accepts custom HTML attributes'

specify 'contains a panel with the correct title and body text' do
specify 'contains a panel with the correct title and text' do
render_inline(described_class.new(**kwargs))

expect(rendered_component).to have_tag('div', with: { class: %w(govuk-panel govuk-panel--confirmation) }) do
with_tag('h1', with: { class: 'govuk-panel__title' }, text: title)
with_tag('div', with: { class: 'govuk-panel__body' }, text: body)
with_tag('h1', with: { class: 'govuk-panel__title' }, text: title_text)
with_tag('div', with: { class: 'govuk-panel__body' }, text: text)
end
end

context 'when title_html is provided instead of title_text' do
let(:custom_tag) { "h5" }
let(:custom_title_text) { "I'm a title" }
before do
render_inline(described_class.new(**kwargs.except(:title_text))) do |component|
component.title_html { helper.content_tag(custom_tag, custom_title_text) }
end
end

specify "the custom HTMl is rendered" do
expect(rendered_component).to have_tag("div", with: { class: component_css_class }) do
with_tag(custom_tag, text: custom_title_text)
end
end
end

context 'when a custom id is supplied' do
let(:custom_id) { "fancy-id" }

before { render_inline(described_class.new(**kwargs.merge(id: custom_id))) }

specify 'renders the panel with the custom id' do
expect(rendered_component).to have_tag('div', with: { id: custom_id, class: component_css_class })
end
end

context 'when no title is supplied' do
before { render_inline(described_class.new(**kwargs.except(:title))) }
before { render_inline(described_class.new(**kwargs.except(:title_text))) }

specify 'contains a panel with no title and the body' do
specify 'contains a panel with no title and the text' do
expect(rendered_component).to have_tag('div', with: { class: %w(govuk-panel govuk-panel--confirmation) }) do
without_tag('h1', with: { class: 'govuk-panel__title' }, text: title)
with_tag('div', with: { class: 'govuk-panel__body' }, text: body)
without_tag('h1', with: { class: 'govuk-panel__title' }, text: title_text)
with_tag('div', with: { class: 'govuk-panel__body' }, text: text)
end
end
end

context 'when no body is supplied' do
before { render_inline(described_class.new(**kwargs.except(:body))) }
context 'with a custom heading level' do
let(:custom_heading_level) { 3 }
before { render_inline(described_class.new(**kwargs.merge(heading_level: custom_heading_level))) }

specify 'contains a panel with the title and no text' do
expect(rendered_component).to have_tag(%(h#{custom_heading_level}), with: { class: 'govuk-panel__title' }, text: title_text)
end
end

context 'when no text is supplied' do
before { render_inline(described_class.new(**kwargs.except(:text))) }

specify 'contains a panel with the title and no body' do
specify 'contains a panel with the title and no text' do
expect(rendered_component).to have_tag('div', with: { class: %w(govuk-panel govuk-panel--confirmation) }) do
with_tag('h1', with: { class: 'govuk-panel__title' }, text: title)
without_tag('div', with: { class: 'govuk-panel__body' }, text: body)
with_tag('h1', with: { class: 'govuk-panel__title' }, text: title_text)
without_tag('div', with: { class: 'govuk-panel__body' }, text: text)
end
end
end

context 'when a block is supplied' do
before { render_inline(described_class.new(**kwargs.except(:title))) { helper.tag.div('Something in a block') } }
before { render_inline(described_class.new(**kwargs.except(:title_text))) { helper.tag.div('Something in a block') } }

specify 'contains a panel with no title and the block' do
expect(rendered_component).to have_tag('div', with: { class: %w(govuk-panel govuk-panel--confirmation) }) do
without_tag('h1', with: { class: 'govuk-panel__title' }, text: title)
without_tag('h1', with: { class: 'govuk-panel__title' }, text: title_text)
with_tag('div', with: { class: 'govuk-panel__body' }) do
with_tag('div', text: 'Something in a block')
end
Expand Down
2 changes: 1 addition & 1 deletion spec/helpers/govuk_components_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def initialize(helper_method:, klass:, args:, kwargs:, css_matcher:, block: nil)
helper_method: :govuk_panel,
klass: GovukComponent::PanelComponent,
args: [],
kwargs: { title: 'Panel title', body: 'Panel body' },
kwargs: { title_text: 'Panel title', text: 'Panel body' },
css_matcher: %(.govuk-panel)
},
{
Expand Down