From 2f046dc435c249bdb9d25f2514d8176d028d65d9 Mon Sep 17 00:00:00 2001 From: ChrisBAshton Date: Fri, 30 Aug 2019 15:43:05 +0100 Subject: [PATCH] Allow inset-text to take a block Can now pass an arbitrary block to the inset-text component to have content rendered inset. If both 'text' and a block are provided, 'text' takes precedence (mutually exclusive). This is a compromise solution, rather than copying the inset-prompt component from content-publisher (for use in collections-publisher), for the reason that under current GOV.UK architecture the component CSS would be added to the stylesheets for our end users (not just the users using the publishing app). See: https://github.com/alphagov/govuk_publishing_components/pull/1077 Once merged, we will use this latest inset-text component and manually recreate what the inset-prompt component looks like by passing components to the inset-text component as a block. Trello: https://trello.com/c/TcBpLRuC/48-add-inset-prompt-component-to-govukpublishingcomponents --- .../components/_inset_text.html.erb | 7 +++++-- .../components/docs/inset_text.yml | 8 ++++++++ spec/components/inset_text_spec.rb | 19 +++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/app/views/govuk_publishing_components/components/_inset_text.html.erb b/app/views/govuk_publishing_components/components/_inset_text.html.erb index e47a816e94..2032b78e13 100644 --- a/app/views/govuk_publishing_components/components/_inset_text.html.erb +++ b/app/views/govuk_publishing_components/components/_inset_text.html.erb @@ -1,7 +1,10 @@ <% id ||= "inset-text-#{SecureRandom.hex(4)}" %> - <%= tag.div id: id, class: "gem-c-inset-text govuk-inset-text" do %> - <%= text %> + <% if defined? text %> + <%= text %> + <% elsif block_given? %> + <%= yield %> + <% end %> <% end %> diff --git a/app/views/govuk_publishing_components/components/docs/inset_text.yml b/app/views/govuk_publishing_components/components/docs/inset_text.yml index 49455fd8ff..0b3d5e22b7 100644 --- a/app/views/govuk_publishing_components/components/docs/inset_text.yml +++ b/app/views/govuk_publishing_components/components/docs/inset_text.yml @@ -8,3 +8,11 @@ examples: default: data: text: "It can take up to 8 weeks to register a lasting power of attorney if there are no mistakes in the application." + + with_block: + data: + block: | +
+

To publish this step by step you need to

+ Check for broken links +
diff --git a/spec/components/inset_text_spec.rb b/spec/components/inset_text_spec.rb index 389fbd244f..464ad3f5f1 100644 --- a/spec/components/inset_text_spec.rb +++ b/spec/components/inset_text_spec.rb @@ -10,4 +10,23 @@ def component_name assert_select(".govuk-inset-text", text: "It can take up to 8 weeks to register a lasting power of attorney if there are no mistakes in the application.") end + + it "renders a block" do + block = " +

To publish this step by step you need to

+ Check for broken links + ".html_safe + render_component({}) { block } + + assert_select(".govuk-inset-text h2#heading", text: "To publish this step by step you need to") + assert_select(".govuk-inset-text a[href='/foo']", text: "Check for broken links") + end + + it "renders only the text if both text and block are provided" do + block = "

Foo

".html_safe + render_component(text: 'Bar') { block } + + assert_select(".govuk-inset-text", text: "Bar") + assert_select(".govuk-inset-text p#foo", false, "Block should not have rendered") + end end