From 30a2fb3be6fa5ad1068b5eb819d946aa642577b3 Mon Sep 17 00:00:00 2001 From: Alistair Laing Date: Fri, 2 Nov 2018 14:28:58 +0000 Subject: [PATCH 1/2] Add aria-describeby to single checkbox with no fieldset --- src/components/checkboxes/checkboxes.yaml | 10 ++++++++++ src/components/checkboxes/template.njk | 5 +++++ src/components/checkboxes/template.test.js | 8 ++++++++ 3 files changed, 23 insertions(+) diff --git a/src/components/checkboxes/checkboxes.yaml b/src/components/checkboxes/checkboxes.yaml index cf9003a0f4..4bbf4face1 100644 --- a/src/components/checkboxes/checkboxes.yaml +++ b/src/components/checkboxes/checkboxes.yaml @@ -220,6 +220,16 @@ examples: - value: blue text: Blue +- name: with single option set 'aria-describeby' on input + readme: false + data: + name: t-and-c + errorMessage: + text: Please accept the terms and conditions + items: + - value: yes + text: I agree to the terms and conditions + - name: with all fieldset attributes data: idPrefix: example diff --git a/src/components/checkboxes/template.njk b/src/components/checkboxes/template.njk index 8f39b79e5d..1ee7a9e373 100644 --- a/src/components/checkboxes/template.njk +++ b/src/components/checkboxes/template.njk @@ -18,6 +18,9 @@ {% endif %} {% endfor %} +{#- fieldset is false by default -#} +{% set hasFieldset = true if params.fieldset else false %} + {#- Capture the HTML so we can optionally nest it in a fieldset -#} {% set innerHtml %} {% if params.hint %} @@ -54,6 +57,8 @@ 0) %} aria-describedby="{{ describedBy }}"{% endif -%} {%- if item.conditional %} data-aria-controls="{{ conditionalId }}"{% endif -%} {%- if hasHint %} aria-describedby="{{ itemHintId }}"{% endif -%} {%- for attribute, value in item.attributes %} {{ attribute }}="{{ value }}"{% endfor -%}> diff --git a/src/components/checkboxes/template.test.js b/src/components/checkboxes/template.test.js index 8b49fac466..0123eae7ae 100644 --- a/src/components/checkboxes/template.test.js +++ b/src/components/checkboxes/template.test.js @@ -643,4 +643,12 @@ describe('Checkboxes', () => { expect(htmlWithClassName($, '.govuk-fieldset')).toMatchSnapshot() }) }) + + describe('single checkbox without a fieldset', () => { + it('adds aria-describe to input if there is an error', () => { + const $ = render('checkboxes', examples["with single option set 'aria-describeby' on input"]) + const $input = $('input') + expect($input.attr('aria-describedby')).toMatch('t-and-c-error') + }) + }) }) From 1247fd063b5722daf14ca791b7b73873db525755 Mon Sep 17 00:00:00 2001 From: Alistair Laing Date: Mon, 5 Nov 2018 20:54:13 +0000 Subject: [PATCH 2/2] Adds entry to CHANGELOG.md fixes log --- CHANGELOG.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4bea17ce72..48ce30449a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,15 @@ ([PR #N](https://github.com/alphagov/govuk-frontend/pull/N)) +- Single field with error should have 'aria-describeby' attribute + + Although we discourage using checkboxes without fieldsets, this fix + ensures that if there are no fieldset then the aria-describeby will + still be usable by screenreaders by adding the element ids to the checkbox + input elements 'aria-describeby' attribute. + + ([PR #1054](https://github.com/alphagov/govuk-frontend/pull/1054)) + ## 2.3.0 (Feature release) 🆕 New features: