Skip to content

Commit

Permalink
Merge pull request #2742 from alphagov/kg-i18n-character-count
Browse files Browse the repository at this point in the history
Add ability to customise character count fallback text
  • Loading branch information
querkmachine committed Aug 30, 2022
2 parents dc6fc67 + 8bdc08b commit 6825ec3
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 you can enter, which is announced to screen readers. The text is displayed as a fallback if the character count JavaScript does not run. Depending on how you configure this component and what parameters you add, instances of `%{count}` are replaced by the value of `maxwords`. If not configured, it uses `maxlength`. By default, fallback text is provided in English.
- 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.maxwords or params.maxlength %}
{%- 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 6825ec3

Please sign in to comment.