Skip to content

Commit 362beb0

Browse files
committed
fix: fix focus order and placememt on addition and removal
1 parent 3f2fa44 commit 362beb0

File tree

3 files changed

+70
-15
lines changed

3 files changed

+70
-15
lines changed

app/views/common/partials/side-navigation.njk

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@
4545

4646

4747

48+
<li class="app-vertical-nav__item ">
49+
<a class="app-vertical-nav__link" href="/components/api-error/">API error</a></li>
50+
51+
52+
53+
54+
4855
<li class="app-vertical-nav__item ">
4956
<a class="app-vertical-nav__link" href="/components/badge/">Badge</a></li>
5057

@@ -192,6 +199,13 @@
192199

193200

194201

202+
<li class="app-vertical-nav__item ">
203+
<a class="app-vertical-nav__link" href="/components/outage-banner/">Outage banner</a></li>
204+
205+
206+
207+
208+
195209
<li class="app-vertical-nav__item ">
196210
<a class="app-vertical-nav__link" href="/components/page-header-actions/">Page header actions</a></li>
197211

src/moj/components/add-another/_add-another.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
margin-top: govuk-spacing(6);
1212
padding: 0;
1313

14+
&:focus {
15+
//@include govuk-focused-box;
16+
}
17+
1418
&:first-of-type {
1519
margin-top: 0;
1620
}

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

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { 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

Comments
 (0)