11import { ConfigurableComponent } from 'govuk-frontend'
2+
3+ import { setFocus } from '../../common/index.mjs'
4+
25/**
36 * @augments {ConfigurableComponent<AddAnotherConfig> }
47 */
@@ -59,9 +62,7 @@ export class AddAnother extends ConfigurableComponent {
5962 return
6063 }
6164
62- const $items = this . getItems ( )
6365 const $item = this . getNewItem ( )
64- console . log ( $items )
6566 console . log ( $item )
6667
6768 // if (!$item || !($item instanceof HTMLElement)) {
@@ -72,18 +73,38 @@ export class AddAnother extends ConfigurableComponent {
7273
7374 this . $itemsContainer . appendChild ( $item )
7475
76+ const $items = this . getItems ( )
7577 this . updateAllItems ( )
76- // const $lastItem = $items[$items.length - 1]
77- // if (!this.hasRemoveButton($lastItem)) {
78- // this.createRemoveButton($lastItem)
78+ const $lastItem = $items [ $items . length - 1 ]
79+
80+ // focus first input
81+ // const $input = $lastItem.querySelector('input, textarea, select')
82+ // if ($input && $input instanceof HTMLInputElement) {
83+ // setTimeout(() => {
84+ // $input.focus()
85+ // }, 100)
7986 // }
8087
81- // $items[$items.length - 1].after($item)
88+ // focus legend
89+ // const $legend = $lastItem.querySelector('legend')
90+ // if ($legend && $legend instanceof HTMLElement) {
91+ // console.log('setting focus on legend')
92+ // setTimeout(() => {
93+ // setFocus($legend)
94+ // }, 100)
95+ // }
8296
83- const $input = $item . querySelector ( 'input, textarea, select' )
84- if ( $input && $input instanceof HTMLInputElement ) {
85- $input . focus ( )
97+ // focus fieldset
98+ if ( $lastItem && $lastItem instanceof HTMLElement ) {
99+ console . log ( 'setting focus on fieldset' )
100+ setTimeout ( ( ) => {
101+ setFocus ( $lastItem )
102+ } , 100 )
86103 }
104+
105+ // Focus on new item legend
106+ // const $legend = $item.querySelector('legend')
107+ // setFocus($lastItem)
87108 }
88109
89110 /**
@@ -133,7 +154,7 @@ export class AddAnother extends ConfigurableComponent {
133154 }
134155
135156 $items . forEach ( ( $item , index , items ) => {
136- this . updateIndexes ( $item , $items . length )
157+ this . updateIndexes ( $item , index )
137158 this . updateLegends ( $item , index , items . length )
138159 this . updateRemoveButtons ( $item , index )
139160 this . updateFieldLabels ( $item , index )
@@ -284,13 +305,29 @@ export class AddAnother extends ConfigurableComponent {
284305 return
285306 }
286307
287- $ button. closest ( 'fieldset' ) . remove ( )
308+ const $itemToRemove = $ button. closest ( 'fieldset' )
288309
289- const $items = this . getItems ( )
310+ if ( ! $itemToRemove || ! ( $itemToRemove instanceof HTMLFieldSetElement ) ) {
311+ return
312+ }
290313
291- this . updateAllItems ( )
314+ let $itemToFocus = $itemToRemove . previousElementSibling
292315
293- this . focusHeading ( )
316+ // Should we get the next element?
317+ if ( ! $itemToFocus || ! ( $itemToFocus instanceof HTMLFieldSetElement ) ) {
318+ $itemToFocus = $itemToRemove . nextElementSibling
319+ }
320+ // focus on root of component?
321+ // it needs an accessible name?
322+ if ( ! $itemToFocus || ! ( $itemToFocus instanceof HTMLFieldSetElement ) ) {
323+ $itemToFocus = this . $root
324+ }
325+
326+ $itemToRemove . remove ( )
327+ this . updateAllItems ( )
328+ if ( $itemToFocus instanceof HTMLElement ) {
329+ setFocus ( $itemToFocus )
330+ }
294331 }
295332
296333 focusHeading ( ) {
@@ -331,7 +368,7 @@ export class AddAnother extends ConfigurableComponent {
331368 * @satisfies {Schema<AddAnotherConfig> }
332369 */
333370 static schema = Object . freeze (
334- /** @type {const } */ ( {
371+ /** @type {const } */ ( {
335372 properties : {
336373 legendLabel : { type : 'string' } ,
337374 legendFormat : { type : 'string' }
0 commit comments