Skip to content

Commit

Permalink
Add non-broken fallback text when maxlength/maxwords is 0
Browse files Browse the repository at this point in the history
this may become more common as we allow customising the attribute in JavaScript now.

The wording may need to be reviewed, but we should output something under the field in any case to avoid a layout shift when the JavaScript kicks in. Unfortunately, before JavaScript kicks in, we can't tell if it'll be characters or words (nor the limit)
  • Loading branch information
romaricpascal committed Oct 13, 2022
1 parent f199990 commit e2235a1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/govuk/components/character-count/character-count.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,9 @@ examples:
name: address
label:
text: Full address
- name: no maximum
data:
id: no-maximum
name: no-maximum
label:
text: Full address
8 changes: 7 additions & 1 deletion src/govuk/components/character-count/template.njk
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@
attributes: params.attributes
}) }}
{%- set fallbackHintLength = params.maxwords or params.maxlength %}
{%- set fallbackHintDefault = 'You can enter up to %{count} ' + ('words' if params.maxwords else 'characters') %}
{%- set fallbackHintDefault -%}
{%- if fallbackHintLength -%}
You can enter up to %{count} {{ ('words' if params.maxwords else 'characters') }}
{%- else -%}
You can enter a limited amount of content
{%- endif -%}
{%- endset -%}
{{ govukHint({
text: (params.fallbackHintText or fallbackHintDefault) | replace('%{count}', fallbackHintLength),
id: params.id + '-info',
Expand Down
9 changes: 9 additions & 0 deletions src/govuk/components/character-count/template.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,13 @@ describe('Character count', () => {
expect($component.attr('data-threshold')).toEqual('75')
})
})

describe('no maximum', () => {
it('shows an error message that does not look broken', () => {
const $ = render('character-count', examples['no maximum'])

const $countMessage = $('.govuk-character-count__message')
expect($countMessage.text()).toContain('You can enter a limited amount of content')
})
})
})

0 comments on commit e2235a1

Please sign in to comment.