Skip to content

Commit

Permalink
Merge pull request #4238 from alphagov/custom-name-on-password-input
Browse files Browse the repository at this point in the history
Allow custom name attribute on password input
  • Loading branch information
yndajas authored Sep 19, 2024
2 parents e6854fb + 5f429f8 commit 9bc50f5
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
## Unreleased

* Remove unused cookies from cookie category ([PR #4234](https://github.com/alphagov/govuk_publishing_components/pull/4234))
* Allow custom name attribute on password input ([PR #4238](https://github.com/alphagov/govuk_publishing_components/pull/4238))

## 43.2.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
password_shown_announcement ||= t("components.password_input.password_shown_announcement")
password_hidden_announcement ||= t("components.password_input.password_hidden_announcement")

name ||= "password"

shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new(local_assigns)
component_helper = GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(local_assigns)
component_helper.add_data_attribute({
Expand All @@ -34,17 +36,16 @@

label_for = uid + '-password-input'

input_name = 'password'
input_id = uid + '-password-input'

aria_controls = uid + '-password-input'

input_classes = %w(govuk-input govuk-password-input__input govuk-js-password-input-input)

if error_text
name << '-input-with-error-message'
label_for << '-with-error-message'
input_id << '-with-error-message'
input_name << '-input-with-error-message'
input_classes << 'govuk-input--error'
aria_controls << '-with-error-message'
paragraph_id = uid + '-password-input-with-error-message-error'
Expand All @@ -64,7 +65,7 @@

<div class="govuk-input__wrapper govuk-password-input__wrapper">
<%= tag.input(
name: input_name,
name:,
type: "password",
class: input_classes,
id: input_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ uses_component_wrapper_helper: true
examples:
default:
data:
with_name:
description: If no name is provided, it defaults to "password".
data:
name: user[password]
with_error:
description: If there is a validation error, passing error text will style the password input component with error styles, and semantically state that there was a validation error.
data:
Expand Down
5 changes: 5 additions & 0 deletions spec/components/password_input_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ def component_name
expect(password_input_component.attr("data-i18n.password-hidden-announcement")).to eql("Your password is hidden")
end

it "renders a password input with custom name" do
render_component({ name: "user[password]" })
assert_select "div.govuk-password-input input[name='user\[password\]']"
end

it "renders a password input with extra classes and a paragraph when error_text is passed" do
render_component({ error_text: "Password must contain at least 8 characters" })
assert_select "div.govuk-form-group--error"
Expand Down

0 comments on commit 9bc50f5

Please sign in to comment.