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

Make Character Count use hint component for message and allow custom … #1650

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
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 #1655: Ensure components use public `govuk-media-query` mixin](https://github.com/alphagov/govuk-frontend/pull/1655).
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({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am still a little unsure about this change – it generally feels a bit strange to me that we're using the hint component at all here (as opposed to just having a 'character count message' which happens to look a bit like a hint…) – however, I don't think this makes it any harder for us to change this later, so I think it's OK for now.

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'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this isn't 'new', but we're changing the implementation and it doesn't look like there's a test checking that this attribute is set correctly. Given how important this is for assistive tech, could we add one?

}
}) }}
</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