Skip to content

Commit

Permalink
Merge pull request #1650 from LBHELewis/feature/make-character-count-…
Browse files Browse the repository at this point in the history
…use-hint

Make Character Count use hint component for message and allow custom …
  • Loading branch information
NickColley authored Dec 10, 2019
2 parents cc4e3cc + 6abc78b commit cfca85f
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

### New features

#### Add classes to the character count component's count message

If you're using Nunjucks, you can now add classes to the character count component's count message using the `countMessage.classes` option.

- [Pull request #1650: Make Character Count use hint component for message and allow custom classes to be added](https://github.com/alphagov/govuk-frontend/pull/1650)

### Fixes

- [Pull request #1670: Make width-container margins more targetted to avoid specificity issues](https://github.com/alphagov/govuk-frontend/pull/1670).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ exports[`Character count when it includes a hint renders with hint 1`] = `
class="govuk-hint govuk-character-count__message"
aria-live="polite"
>
You can enter up to characters
You can enter up to 10 characters
</span>
`;

Expand Down
10 changes: 9 additions & 1 deletion src/govuk/components/character-count/character-count.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,15 @@ params:
type: object
required: false
description: HTML attributes (for example data attributes) to add to the textarea.

- name: countMessage
type: object
required: false
description: Options for the count message
params:
- name: classes
type: string
required: false
description: Classes to add to the count message

examples:
- name: default
Expand Down
12 changes: 9 additions & 3 deletions src/govuk/components/character-count/template.njk
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% from "../textarea/macro.njk" import govukTextarea %}
{% from "../hint/macro.njk" import govukHint %}

<div class="govuk-character-count" data-module="govuk-character-count"
{%- if params.maxlength %} data-maxlength="{{ params.maxlength }}"{% endif %}
Expand All @@ -24,7 +25,12 @@
errorMessage: params.errorMessage,
attributes: params.attributes
}) }}
<span id="{{ params.id }}-info" class="govuk-hint govuk-character-count__message" aria-live="polite">
You can enter up to {{ params.maxlength or params.maxwords }} {{'words' if params.maxwords else 'characters' }}
</span>
{{ govukHint({
text: 'You can enter up to ' + (params.maxlength or params.maxwords) + (' words' if params.maxwords else ' characters'),
id: params.id + '-info',
classes: 'govuk-character-count__message' + (' ' + params.countMessage.classes if params.countMessage.classes),
attributes: {
'aria-live': 'polite'
}
}) }}
</div>
51 changes: 51 additions & 0 deletions src/govuk/components/character-count/template.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,61 @@ describe('Character count', () => {
})
})

describe('count message', () => {
it('renders with the amount of characters expected', () => {
const $ = render('character-count', {
maxlength: 10
})

const $countMessage = $('.govuk-character-count__message')
expect($countMessage.text()).toContain('You can enter up to 10 characters')
})
it('renders with the amount of words expected', () => {
const $ = render('character-count', {
maxwords: 10
})

const $countMessage = $('.govuk-character-count__message')
expect($countMessage.text()).toContain('You can enter up to 10 words')
})
it('is associated with the textarea', () => {
const $ = render('character-count', {
maxlength: 10
})

const $textarea = $('.govuk-js-character-count')
const $countMessage = $('.govuk-character-count__message')

const hintId = new RegExp(
WORD_BOUNDARY + $countMessage.attr('id') + WORD_BOUNDARY
)

expect($textarea.attr('aria-describedby'))
.toMatch(hintId)
})
it('renders with custom classes', () => {
const $ = render('character-count', {
countMessage: {
classes: 'app-custom-count-message'
}
})

const $countMessage = $('.govuk-character-count__message')
expect($countMessage.hasClass('app-custom-count-message')).toBeTruthy()
})
it('renders with aria live set to polite', () => {
const $ = render('character-count', {})

const $countMessage = $('.govuk-character-count__message')
expect($countMessage.attr('aria-live')).toEqual('polite')
})
})

describe('when it includes a hint', () => {
it('renders with hint', () => {
const $ = render('character-count', {
id: 'character-count-with-hint',
maxlength: 10,
hint: {
text: 'It’s on your National Insurance card, benefit letter, payslip or P60. For example, ‘QQ 12 34 56 C’.'
}
Expand Down

0 comments on commit cfca85f

Please sign in to comment.