Skip to content

Commit

Permalink
Add aria-describeby to single checkbox with no fieldset
Browse files Browse the repository at this point in the history
  • Loading branch information
aliuk2012 committed Nov 5, 2018
1 parent 3e7f8f1 commit 30a2fb3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/components/checkboxes/checkboxes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/components/checkboxes/template.njk
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down Expand Up @@ -54,6 +57,8 @@
<input class="govuk-checkboxes__input" id="{{ id }}" name="{{ name }}" type="checkbox" value="{{ item.value }}"
{{-" checked" if item.checked }}
{{-" disabled" if item.disabled }}
{#- fieldset is false by default -#}
{%- if (not hasFieldset) and ((describedBy | length) > 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 -%}>
Expand Down
8 changes: 8 additions & 0 deletions src/components/checkboxes/template.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})
})

0 comments on commit 30a2fb3

Please sign in to comment.