@@ -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 ( / % i n d e x % / , `${ 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