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

Add component wrapper helper to textarea #4574

Merged
merged 3 commits into from
Jan 22, 2025
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
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

* **BREAKING** Add component wrapper helper to textarea ([PR #4574](https://github.com/alphagov/govuk_publishing_components/pull/4574))
* Remove margin top from print link ([PR #4577](https://github.com/alphagov/govuk_publishing_components/pull/4577))
* **BREAKING** Use component wrapper on print link component ([PR #4576](https://github.com/alphagov/govuk_publishing_components/pull/4576))
* Refactor single page notification component ([PR #4501](https://github.com/alphagov/govuk_publishing_components/pull/4501))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
.govuk-textarea {
margin-bottom: 0;

+ .govuk-hint {
~ .govuk-hint {
margin-top: govuk-spacing(1);
}

~ .govuk-error-message {
margin-top: govuk-spacing(1);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<%
add_gem_component_stylesheet("error-message")
add_gem_component_stylesheet("character-count")

id ||= "character-count-#{SecureRandom.hex(4)}"
maxlength ||= nil
maxwords ||= nil
threshold ||= nil
textarea ||= {}

textarea[:textarea_id] = id
%>
<% if maxlength || maxwords %>
<%= content_tag :div,
Expand All @@ -15,14 +20,10 @@
threshold: threshold
} do %>

<%= render "govuk_publishing_components/components/textarea", { id: id, character_count: true }.merge(textarea.symbolize_keys) %>
<%= render "govuk_publishing_components/components/textarea", { textarea_id: id, character_count: true, margin_bottom: 1 }.merge(textarea.symbolize_keys) %>

<div id="<%= id %>-info" class="govuk-hint govuk-character-count__message">
<%= t("components.character_count.body", number: maxlength || maxwords, type: maxwords ? t("components.character_count.type.words") : t("components.character_count.type.characters")) %>
</div>
<% end %>
<% end %>
<%
add_gem_component_stylesheet("error-message")
add_gem_component_stylesheet("character-count")
%>
25 changes: 11 additions & 14 deletions app/views/govuk_publishing_components/components/_textarea.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%
add_gem_component_stylesheet("textarea")

id ||= "textarea-#{SecureRandom.hex(4)}"
textarea_id ||= "textarea-#{SecureRandom.hex(4)}"
value ||= nil
rows ||= 5
describedby ||= nil
Expand All @@ -11,7 +11,6 @@
label ||= nil
hint ||= nil
local_assigns[:margin_bottom] ||= 6
local_assigns[:margin_bottom] = 6 if local_assigns[:margin_bottom] > 9
error_message ||= nil
error_items ||= []
character_count ||= nil
Expand All @@ -22,14 +21,13 @@
right_to_left ||= false
right_to_left_help = right_to_left_help.nil? ? right_to_left : right_to_left_help

shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new(local_assigns)
component_helper = GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(local_assigns)
component_helper.add_class("gem-c-textarea govuk-form-group")
component_helper.add_class("govuk-form-group--error") if has_error

css_classes = %w(govuk-textarea)
css_classes << "govuk-js-character-count" if character_count
css_classes << "govuk-textarea--error" if has_error
form_group_css_classes = %w(gem-c-textarea govuk-form-group)
form_group_css_classes << "govuk-form-group--error" if has_error
form_group_css_classes << shared_helper.get_margin_bottom
textarea_classes = %w(govuk-textarea)
textarea_classes << "govuk-js-character-count" if character_count
textarea_classes << "govuk-textarea--error" if has_error

aria_described_by ||= nil
if hint || has_error || describedby
Expand All @@ -40,11 +38,10 @@
aria_described_by = aria_described_by.join(" ")
end
%>

<%= content_tag :div, class: form_group_css_classes do %>
<%= tag.div(**component_helper.all_attributes) do %>
<% if label %>
<%= render "govuk_publishing_components/components/label", {
html_for: id,
html_for: textarea_id,
right_to_left: right_to_left_help
}.merge(label.symbolize_keys) %>
<% end %>
Expand All @@ -67,9 +64,9 @@
<% end %>

<%= tag.textarea name: name,
class: css_classes,
class: textarea_classes,
dir: right_to_left ? "rtl" : nil,
id: id,
id: textarea_id,
rows: rows,
maxlength: maxlength,
data: data,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ accessibility_criteria: |
- have correctly associated labels

Labels use the [label component](/component-guide/label).
uses_component_wrapper_helper: true
examples:
default:
data:
Expand All @@ -26,7 +27,7 @@ examples:
label:
text: "What is the nature of your medical emergency?"
name: "emergency-name"
id: "emergency-id"
textarea_id: "emergency-id"
with_margin_bottom:
description: The component accepts a number for margin bottom from `0` to `9` (`0px` to `60px`) using the [GOV.UK Frontend spacing scale](https://design-system.service.gov.uk/styles/spacing/#the-responsive-spacing-scale). It defaults to a margin bottom of 6 (30px).
data:
Expand Down Expand Up @@ -102,7 +103,7 @@ examples:
<%= component %>
<% end %>
data:
id: "contextual-guidance-id"
textarea_id: "contextual-guidance-id"
label:
text: "Title"
bold: true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file needs the uses_component_wrapper_helper: true line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, have added. Looking into the visual diff now 👍

Expand Down
40 changes: 24 additions & 16 deletions spec/components/character_count_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,46 @@ def component_name
assert_empty render_component({})
end

it "renders character count with textarea" do
it "renders character count with minimal data passed" do
render_component(
name: "character-count",
textarea: {
label: { text: "Can you provide more detail?" },
name: "more-details",
},
data: {
module: "character-count",
},
maxlength: "100",
)
assert_select ".govuk-character-count .govuk-textarea[name='more-details']"
end

assert_select ".govuk-character-count .govuk-textarea"

it "renders character count with more options" do
render_component(
textarea: {
label: { text: "Can you provide more detail?" },
name: "more-details",
},
maxlength: "100",
maxwords: "3",
threshold: "11",
)
assert_select ".gem-c-character-count[data-maxlength='100']"
assert_select ".gem-c-character-count[data-maxwords='3']"
assert_select ".gem-c-character-count[data-threshold='11']"
assert_select ".govuk-character-count .govuk-textarea[name='more-details']"
assert_select ".govuk-character-count .govuk-label", text: "Can you provide more detail?"
end

it "renders character count with data attributes" do
it "renders character count with the correct id for the textarea" do
render_component(
name: "character-count",
textarea: {
label: { text: "Can you provide more detail?" },
name: "more-details",
},
data: {
module: "character-count",
textarea_id: "textarea-element-id",
id: "textarea-component-id",
},
maxlength: "100",
id: "character-count-id",
)

assert_select ".govuk-character-count[data-module='govuk-character-count']"
assert_select ".govuk-character-count[data-maxlength='100']"
assert_select "#textarea-component-id.gem-c-textarea"
assert_select "textarea#character-count-id"
assert_select "#textarea-element-id", false
end
end
16 changes: 4 additions & 12 deletions spec/components/textarea_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ def component_name
render_component(
label: { text: "Can you provide more detail?" },
name: "more-details",
id: "this-id",
textarea_id: "this-id",
id: "component-id",
)

assert_select "#this-id.govuk-textarea"
assert_select "#component-id.gem-c-textarea"
assert_select "textarea#this-id"
end

it "renders textarea with a custom number of rows" do
Expand Down Expand Up @@ -102,16 +104,6 @@ def component_name
assert_select '.gem-c-textarea.govuk-\!-margin-bottom-4'
end

it "defaults to the initial bottom margin if an incorrect value is passed" do
render_component(
label: { text: "Can you provide more detail?" },
name: "with-fallback-margin-bottom",
margin_bottom: 12,
)

assert_select '.gem-c-textarea.govuk-\!-margin-bottom-6'
end

it "defaults to the initial bottom margin if no value is passed" do
render_component(
label: { text: "Can you provide more detail?" },
Expand Down
Loading