Skip to content

Commit

Permalink
Merge pull request #671 from alphagov/focus-error-summary-on-window-load
Browse files Browse the repository at this point in the history
Focus error summary on window load
  • Loading branch information
NickColley authored May 2, 2018
2 parents e5e2a5d + 72f0d3e commit 53d2ccc
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 5 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,19 @@ New features:
- Checkboxes and Radios conditional reveal
(PR [#616](https://github.com/alphagov/govuk-frontend/pull/616))

- Vendor-in SassMQ functionality, write tests and remove external dependency
- Vendor-in SassMQ functionality, write tests and remove external dependency
(PR [#657](https://github.com/alphagov/govuk-frontend/pull/657))

- Focus Error Summary on window load
(PR [#671](https://github.com/alphagov/govuk-frontend/pull/671))

Internal:

- Update publishing docs (PR [#651](https://github.com/alphagov/govuk-frontend/pull/651))
- Wrap `app.css` in conditional comments in review app layout (PR [#653](https://github.com/alphagov/govuk-frontend/pull/653))
- Fix missing code highlight and remove duplicate layout
(PR [#663](https://github.com/alphagov/govuk-frontend/pull/663))
- Exclude test related files from `dist/` and `packages/` copy task
- Exclude test related files from `dist/` and `packages/` copy task
(PR [#662](https://github.com/alphagov/govuk-frontend/pull/662))
- Add test to check if Sass in packages compiles correctly after the `build:packages` task
(PR [#669](https://github.com/alphagov/govuk-frontend/pull/669))
Expand Down
7 changes: 6 additions & 1 deletion src/all/all.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { nodeListForEach } from '../globals/common'

import Button from '../button/button'
import Details from '../details/details'
import Checkboxes from '../checkboxes/checkboxes'
import Details from '../details/details'
import ErrorSummary from '../error-summary/error-summary'
import Radios from '../radios/radios'

export function initAll () {
Expand All @@ -14,6 +15,10 @@ export function initAll () {
new Checkboxes($checkbox).init()
})

// Find first Error Summary module to enhance.
var $errorSummary = document.querySelector('[data-module="error-summary"]')
new ErrorSummary($errorSummary).init()

var $radios = document.querySelectorAll('[data-module="radios"]')
nodeListForEach($radios, function ($radio) {
new Radios($radio).init()
Expand Down
2 changes: 1 addition & 1 deletion src/error-summary/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Find out when to use the Error summary component in your service in the [GOV.UK

#### Markup

<div class="govuk-error-summary optional-extra-class" aria-labelledby="error-summary-title" role="alert" tabindex="-1">
<div class="govuk-error-summary optional-extra-class" aria-labelledby="error-summary-title" role="alert" tabindex="-1" data-module="error-summary">
<h2 class="govuk-error-summary__title" id="error-summary-title">
Message to alert the user to a problem goes here
</h2>
Expand Down
17 changes: 17 additions & 0 deletions src/error-summary/error-summary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import '../globals/polyfills/Event' // addEventListener

function ErrorSummary ($module) {
this.$module = $module
}

ErrorSummary.prototype.init = function () {
var $module = this.$module
if (!$module) {
return
}
window.addEventListener('load', function () {
$module.focus()
})
}

export default ErrorSummary
31 changes: 31 additions & 0 deletions src/error-summary/error-summary.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @jest-environment ./lib/puppeteer/environment.js
*/
/* eslint-env jest */

const configPaths = require('../../config/paths.json')
const PORT = configPaths.ports.test

let browser
let page
let baseUrl = 'http://localhost:' + PORT

beforeAll(async (done) => {
browser = global.__BROWSER__
page = await browser.newPage()
done()
})

afterAll(async (done) => {
await page.close()
done()
})

describe('Error Summary', () => {
it('is automatically focused when the page loads', async () => {
await page.goto(baseUrl + '/components/error-summary/preview', { waitUntil: 'load' })

const moduleName = await page.evaluate(() => document.activeElement.dataset.module)
expect(moduleName).toBe('error-summary')
})
})
3 changes: 3 additions & 0 deletions src/error-summary/error-summary.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
accessibilityCriteria: |
- Must be focused when the page loads
examples:
- name: default
data:
Expand Down
2 changes: 1 addition & 1 deletion src/error-summary/template.njk
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="govuk-error-summary
{%- if params.classes %} {{ params.classes }}{% endif %}" aria-labelledby="error-summary-title" role="alert" tabindex="-1"
{%- for attribute, value in params.attributes %} {{ attribute }}="{{ value }}"{% endfor %}>
{%- for attribute, value in params.attributes %} {{ attribute }}="{{ value }}"{% endfor %} data-module="error-summary">
<h2 class="govuk-error-summary__title" id="error-summary-title">
{{ params.titleHtml | safe if params.titleHtml else params.titleText }}
</h2>
Expand Down

0 comments on commit 53d2ccc

Please sign in to comment.