Skip to content

Commit

Permalink
fix(add-another): find labels of affixed text inputs
Browse files Browse the repository at this point in the history
When a text input has affixes, it is wrapped in a `.govuk-input__wrapper` div, meaning its label is
no longer adjacent. This caused the Add anotehr component to fail, because it couldn't find the
input's label and update its for/id link.

I've added a new strategy for finding the label: if it's not the parent or adjacent, then JavaScript
identifies it by searching for a label whose `for` attribute is the ID of the original input.

This should not introduce any regressions because the previous strategies are still in place and
prioritised: the new strategy will only be used if nothing was found, which would have previously
resulted in the application crashing.

fixes #185
  • Loading branch information
gregtyler committed Jun 25, 2021
1 parent bb3e0c1 commit 31b0370
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
7 changes: 0 additions & 7 deletions docs/examples/add-another/script.js

This file was deleted.

6 changes: 6 additions & 0 deletions src/moj/components/add-another/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Add another

- [Guidance](https://moj-design-system.herokuapp.com/components/add-another)
- [Preview](https://moj-frontend.herokuapp.com/components/add-another)

## Arguments
6 changes: 5 additions & 1 deletion src/moj/components/add-another/add-another.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ MOJFrontend.AddAnother.prototype.getNewItem = function() {

MOJFrontend.AddAnother.prototype.updateAttributes = function(index, item) {
item.find('[data-name]').each(function(i, el) {
var originalId = el.id

el.name = $(el).attr('data-name').replace(/%index%/, index);
el.id = $(el).attr('data-id').replace(/%index%/, index);
($(el).siblings('label')[0] || $(el).parents('label')[0]).htmlFor = el.id;

var label = $(el).siblings('label')[0] || $(el).parents('label')[0] || item.find('[for="' + originalId + '"]')[0];
label.htmlFor = el.id;
});
};

Expand Down

0 comments on commit 31b0370

Please sign in to comment.