Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent error summary from being refocused after it has been initially focused on page load #2491

Merged
merged 3 commits into from
Jan 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/govuk/components/error-summary/error-summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,28 @@ ErrorSummary.prototype.init = function () {
if (!$module) {
return
}
$module.focus()

this.setFocus()
$module.addEventListener('click', this.handleClick.bind(this))
}

/**
* Focus the error summary
*/
ErrorSummary.prototype.setFocus = function () {
var $module = this.$module

// Set tabindex to -1 to make the element programmatically focusable, but
// remove it on blur as the error summary doesn't need to be focused again.
$module.setAttribute('tabindex', '-1')

$module.addEventListener('blur', function () {
$module.removeAttribute('tabindex')
})

$module.focus()
}

/**
* Click event handler
*
Expand Down
9 changes: 9 additions & 0 deletions src/govuk/components/error-summary/error-summary.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ describe('Error Summary', () => {
expect(moduleName).toBe('govuk-error-summary')
})

it('removes the tabindex attribute on blur', async () => {
vanitabarrett marked this conversation as resolved.
Show resolved Hide resolved
await page.goto(baseUrl + '/components/error-summary/preview', { waitUntil: 'load' })

await page.$eval('.govuk-error-summary', el => el.blur())

const tabindex = await page.$eval('.govuk-error-summary', el => el.getAttribute('tabindex'))
expect(tabindex).toBeNull()
})

const inputTypes = [
// [description, input id, selector for label or legend]
['an input', 'input', 'label[for="input"]'],
Expand Down
2 changes: 1 addition & 1 deletion src/govuk/components/error-summary/template.njk
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="govuk-error-summary
{%- if params.classes %} {{ params.classes }}{% endif %}" aria-labelledby="error-summary-title" role="alert" tabindex="-1"
{%- if params.classes %} {{ params.classes }}{% endif %}" aria-labelledby="error-summary-title" role="alert"
{%- for attribute, value in params.attributes %} {{ attribute }}="{{ value }}"{% endfor %} data-module="govuk-error-summary">
<h2 class="govuk-error-summary__title" id="error-summary-title">
{{ params.titleHtml | safe if params.titleHtml else params.titleText }}
Expand Down
7 changes: 0 additions & 7 deletions src/govuk/components/error-summary/template.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ describe('Error-summary', () => {
expect(roleAttr).toEqual('alert')
})

it('has the correct tabindex attribute to be focussed', () => {
const $ = render('error-summary', examples.default)
const tabindexAttr = $('.govuk-error-summary').attr('tabindex')

expect(tabindexAttr).toEqual('-1')
})

it('renders title text', () => {
const $ = render('error-summary', examples.default)
const summaryTitle = $('.govuk-error-summary__title').text().trim()
Expand Down