Skip to content

Commit

Permalink
Add ability to customise character count fallback text
Browse files Browse the repository at this point in the history
  • Loading branch information
querkmachine committed Aug 2, 2022
1 parent 6b282b8 commit c4a97ef
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/govuk/components/character-count/character-count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('Character count', () => {
await page.setJavaScriptEnabled(true)
})

it('shows the static message', async () => {
it('shows the fallback message', async () => {
await goToExample()
const message = await page.$eval('.govuk-character-count__message', el => el.innerHTML.trim())

Expand Down Expand Up @@ -320,4 +320,13 @@ describe('Character count', () => {
})
})
})

describe('custom options', () => {
it('allows customisation of the fallback message', async () => {
await goToExample('with-custom-fallback-text')
const message = await page.$eval('.govuk-character-count__message', el => el.innerHTML.trim())

expect(message).toEqual('Gallwch ddefnyddio hyd at 10 nod')
})
})
})
12 changes: 12 additions & 0 deletions src/govuk/components/character-count/character-count.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ params:
required: false
description: Options for the hint component.
isComponent: true
- name: fallbackHintText
type: string
required: false
description: Text describing the maximum number of characters or words that can be entered, which is announced to screen readers and displayed as a fallback if the character count JavaScript does not run. Instances of `%{count}` within the hint will be replaced by the value of `maxwords` if configured, otherwise it uses `maxlength`. By default, fallback text is provided in English based on what parameters are provided to the component.
- name: errorMessage
type: object
required: false
Expand Down Expand Up @@ -81,6 +85,14 @@ examples:
maxlength: 10
label:
text: Can you provide more detail?

- name: with custom fallback text
description: with no-js fallback text translated into Welsh
data:
name: custom-fallback
id: custom-fallback
maxlength: 10
fallbackHintText: Gallwch ddefnyddio hyd at %{count} nod

- name: with hint
data:
Expand Down
4 changes: 3 additions & 1 deletion src/govuk/components/character-count/template.njk
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
errorMessage: params.errorMessage,
attributes: params.attributes
}) }}
{%- set fallbackHintLength = params.maxlength or params.maxwords %}
{%- set fallbackHintDefault = 'You can enter up to %{count} ' + ('words' if params.maxwords else 'characters') %}
{{ govukHint({
text: 'You can enter up to ' + (params.maxlength or params.maxwords) + (' words' if params.maxwords else ' characters'),
text: (params.fallbackHintText or fallbackHintDefault) | replace('%{count}', fallbackHintLength),
id: params.id + '-info',
classes: 'govuk-character-count__message' + (' ' + params.countMessage.classes if params.countMessage.classes)
}) }}
Expand Down

0 comments on commit c4a97ef

Please sign in to comment.