Skip to content

Commit 61276db

Browse files
committed
fix(add-another): small tweaks and tidying
1 parent fc04ab0 commit 61276db

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

docs/components/add-another/examples/default/index.njk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ redirect_from: examples/add-another
166166
}
167167
]
168168
}) }}
169-
170169
{% endcall %}
171170
{% endcall %}
172171

docs/components/add-another/examples/errors/index.njk

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ layout: 'layouts/example.njk'
112112
}
113113
}) }}
114114

115+
{{ govukButton({
116+
classes: "govuk-button--secondary moj-add-another__remove-button",
117+
text: "Remove"
118+
}) }}
119+
115120
{% endcall %}
116121
{% call mojAddAnotherItem({ legend: { text: "Person 3 of 3" } }) -%}
117122
{{ govukInput({
@@ -146,6 +151,10 @@ layout: 'layouts/example.njk'
146151
}
147152
}) }}
148153

154+
{{ govukButton({
155+
classes: "govuk-button--secondary moj-add-another__remove-button",
156+
text: "Remove"
157+
}) }}
149158
{% endcall %}
150159
{% endcall %}
151160

src/moj/components/add-another/add-another.mjs

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ export class AddAnother extends ConfigurableComponent {
2222
)
2323
this.$itemsContainer = this.$root.querySelector('.moj-add-another__items')
2424

25-
console.log(this.$itemTemplate)
26-
console.log(this.$itemsContainer)
27-
2825
if (!(this.$itemTemplate instanceof HTMLTemplateElement)) {
2926
return
3027
}
@@ -141,16 +138,18 @@ export class AddAnother extends ConfigurableComponent {
141138
return $item
142139
}
143140

144-
updateAllItems() {
141+
updateAllItems(action = '') {
145142
const $items = this.getItems()
146-
const $firstItem = $items[0]
147143

148144
$items.forEach(($item, index, items) => {
149-
this.updateIndexes($item, index, items.length)
145+
this.updateIndexes($item, index)
150146
this.updateLegends($item, index, items.length)
151147
this.updateRemoveButtons($item, index, items.length)
152148
this.updateFieldLabels($item, index)
153149
this.updateGroupedFieldLegends($item, index)
150+
if (action === 'remove') {
151+
this.updateErrorMessages($item, index)
152+
}
154153
})
155154
}
156155

@@ -182,19 +181,45 @@ export class AddAnother extends ConfigurableComponent {
182181
})
183182
}
184183

184+
updateErrorMessages($item, index) {
185+
$item.querySelectorAll('[data-name]').forEach(($input) => {
186+
if (!this.isValidInputElement($input)) {
187+
return
188+
}
189+
190+
const $errorMessage = $input.parentElement?.querySelector(
191+
'.govuk-error-message'
192+
)
193+
if (!$errorMessage || !($errorMessage instanceof HTMLElement)) {
194+
return
195+
}
196+
197+
const id = $input.getAttribute('data-id') || ''
198+
const originalErrorMessageId = $errorMessage.id
199+
200+
const newErrorMessageId = `${id.replace(/%index%/, `${index}`)}-error`
201+
$errorMessage.id = newErrorMessageId
202+
203+
const describedBy = $input.getAttribute('aria-describedby') || ''
204+
const newDescribedBy = describedBy
205+
.split(' ')
206+
.map((desc) =>
207+
desc === originalErrorMessageId ? newErrorMessageId : desc
208+
)
209+
.join(' ')
210+
211+
$input.setAttribute('aria-describedby', newDescribedBy)
212+
})
213+
}
214+
185215
updateFieldLabels($item, index) {
186216
$item.querySelectorAll('[data-label]').forEach(($input) => {
187217
if (!this.isValidInputElement($input)) {
188218
return
189219
}
190220

191-
// const labelText = $input.getAttribute('data-label') || ''
192-
193221
const $label = $input.closest('.govuk-form-group').querySelector('label')
194222
if ($label && $label instanceof HTMLLabelElement) {
195-
// const $labelhiddenText = $label.querySelector(
196-
// 'span.govuk-visually-hidden:not(.moj-add-another__label-suffix)'
197-
// )
198223
let $labelSuffix = $label.querySelector(
199224
'.moj-add-another__label-suffix'
200225
)
@@ -385,7 +410,7 @@ export class AddAnother extends ConfigurableComponent {
385410
}
386411

387412
$itemToRemove.remove()
388-
this.updateAllItems()
413+
this.updateAllItems('remove')
389414
if ($itemToFocus instanceof HTMLElement) {
390415
setFocus($itemToFocus)
391416
}

0 commit comments

Comments
 (0)