Skip to content

Commit

Permalink
Add custom heading level option to radio component
Browse files Browse the repository at this point in the history
Currently if the radio component is passed a heading, and the heading is
not a "Page heading", then that heading is always a h2.
This is not always appropriate; there are instances where more
flexibility is necessary in order to maintain a correct hierarchical
heading structure.
This introduces the shared helper's heading level function to the radio
component. This way we can ensure the default remains h2, while also
having the ability to pass a custom heading level where necessary.
  • Loading branch information
danacotoran committed Mar 2, 2021
1 parent 77496db commit ca7806f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<%
shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new(local_assigns)
id ||= nil
id_prefix ||= "radio-#{SecureRandom.hex(4)}"
items ||= []
Expand Down Expand Up @@ -54,7 +55,7 @@
<% end %>
<% else %>
<%= tag.legend class: legend_classes do %>
<%= tag.h2 heading, class: "govuk-fieldset__heading" %>
<%= content_tag(shared_helper.get_heading_level, heading, class: "govuk-fieldset__heading") %>
<% end %>
<% end %>
<% end %>
Expand Down
15 changes: 14 additions & 1 deletion app/views/govuk_publishing_components/components/docs/radio.yml
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ examples:
text: "Blue"
with_custom_heading_size:
description: |
This allows the size of the legend to be changed. Valid options are s, m, l, xl, defaulting to m if no option is passed.
This allows the size of the legend to be changed. Valid options are s, m, l, xl, defaulting to m if no option is passed.
If the is_page_heading option is true and heading_size is not set, the text size will be xl.
data:
Expand All @@ -198,6 +198,19 @@ examples:
text: "Green"
- value: "blue"
text: "Blue"
with_custom_heading_level:
description: This allows the heading level to be changed. Heading level will default to `h2` if nothing is passed.
data:
name: "radio-group-description"
heading: "What is your favourite colour?"
heading_level: 3
items:
- value: "red"
text: "Red"
- value: "green"
text: "Green"
- value: "blue"
text: "Blue"
with_hint_text_on_radios:
data:
name: "radio-group-hint-text"
Expand Down
27 changes: 27 additions & 0 deletions spec/components/radio_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,33 @@ def component_name
assert_select ".govuk-fieldset__legend.govuk-fieldset__legend--m", "What is your favourite skittle?"
end

it "renders radio-group with h2 heading level by default" do
render_component(
name: "favourite-skittle",
heading: "What is your favourite skittle?",
items: [
{ label: "Red", value: "red" },
{ label: "Green", value: "green" },
{ label: "Blue", value: "blue" },
],
)
assert_select "h2.govuk-fieldset__heading", text: "What is your favourite skittle?"
end

it "renders radio-group custom heading level if custom heading level is passed" do
render_component(
name: "favourite-skittle",
heading: "What is your favourite skittle?",
heading_level: 4,
items: [
{ label: "Red", value: "red" },
{ label: "Green", value: "green" },
{ label: "Blue", value: "blue" },
],
)
assert_select "h4.govuk-fieldset__heading", text: "What is your favourite skittle?"
end

it "renders radio-group with bold labels" do
render_component(
name: "radio-group-bold-labels",
Expand Down

0 comments on commit ca7806f

Please sign in to comment.