Skip to content

Commit

Permalink
Use component wrapper in hint component
Browse files Browse the repository at this point in the history
This adds the component wrapper to the hint component in order to
provide additional options to a hint.

The primary motivation of this was that we want to dynamically change
the value of a hint in GOV.UK Chat and thus want to set a .js-class to
act as a selector [1] (private repo) and instead are using a custom id
to work around it.

Looking at other components it seemed that using the component wrapper
was a more conventional approach than just utilising the shared helper
to add only classes.

[1]: alphagov/govuk-chat#660 (comment)
  • Loading branch information
kevindew committed Sep 10, 2024
1 parent c175ea4 commit cf26c7b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

## Unreleased

* Use component wrapper in hint component ([PR #4214](https://github.com/alphagov/govuk_publishing_components/pull/4214))
* Hide printed URLs when printing tables ([PR #4209](https://github.com/alphagov/govuk_publishing_components/pull/4209))

## 43.1.0
Expand Down
13 changes: 6 additions & 7 deletions app/views/govuk_publishing_components/components/_hint.html.erb
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
<%
add_gem_component_stylesheet("hint")

id ||= "hint-#{SecureRandom.hex(4)}"
local_assigns[:id] ||= "hint-#{SecureRandom.hex(4)}"
is_radio_label_hint ||= false
right_to_left ||= false
shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new(local_assigns)

css_classes = %w( gem-c-hint govuk-hint )
css_classes << "govuk-radios__hint" if is_radio_label_hint

custom_margin_bottom_class = shared_helper.get_margin_bottom if [*0..9].include?(local_assigns[:margin_bottom])
css_classes << custom_margin_bottom_class if local_assigns[:margin_bottom]
component_helper = GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(local_assigns)
component_helper.add_class("gem-c-hint govuk-hint")
component_helper.add_class("govuk-radios__hint") if is_radio_label_hint
component_helper.add_class(shared_helper.get_margin_bottom) if [*0..9].include?(local_assigns[:margin_bottom])
%>
<%= tag.div id: id, class: css_classes, dir: right_to_left ? "rtl" : nil do %>
<%= tag.div(**component_helper.all_attributes, dir: right_to_left ? "rtl" : nil) do %>
<%= text %>
<% end %>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ name: Form hint text
description: Use hints for any form fields.
govuk_frontend_components:
- hint
uses_component_wrapper_helper: true
accessibility_criteria: |
All text must have a contrast ratio higher than 4.5:1 against the background colour to meet [WCAG AA](https://www.w3.org/TR/WCAG20/#visual-audio-contrast-contrast)
Expand Down
18 changes: 18 additions & 0 deletions spec/components/hint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ def component_name
assert_select ".govuk-hint", text: "For example, ‘QQ 12 34 56 C’."
end

it "sets a default id" do
render_component(text: "For example, ‘QQ 12 34 56 C’.")

assert_select ".govuk-hint[id^=hint-]"
end

it "accepts a custom id" do
render_component(text: "For example, ‘QQ 12 34 56 C’.", id: "special-hint")

assert_select ".govuk-hint[id=special-hint]"
end

it "applies a specified bottom margin" do
render_component(text: "For example, ‘QQ 12 34 56 C’.", margin_bottom: 7)

Expand All @@ -29,6 +41,12 @@ def component_name
assert_select '.govuk-hint.govuk-\!-margin-bottom-3', false
end

it "accepts js classes" do
render_component(text: "For example, ‘QQ 12 34 56 C’.", classes: "js-class-one js-class-two")

assert_select ".govuk-hint.js-class-one.js-class-two"
end

it "renders the component with the correct `dir` attribute" do
render_component(
text: "العربيَّة",
Expand Down

0 comments on commit cf26c7b

Please sign in to comment.