Skip to content

Commit 51d1eb4

Browse files
Input component 0 value fix
Fixes #4669. Input component now uses govukAttributes macro to validate and format attributes. Test and example added for edge case outlined in issue #4669. Co-authored-by: Colin Rotherham <[email protected]>
1 parent f047499 commit 51d1eb4

File tree

3 files changed

+68
-9
lines changed

3 files changed

+68
-9
lines changed

packages/govuk-frontend/src/govuk/components/input/input.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,14 @@ examples:
399399
label:
400400
text: With value
401401
value: QQ 12 34 56 C
402+
- name: zero value
403+
hidden: true
404+
options:
405+
id: with-zero-value
406+
name: with-zero-value
407+
label:
408+
text: With zero value
409+
value: 0
402410
- name: with describedBy
403411
hidden: true
404412
options:

packages/govuk-frontend/src/govuk/components/input/template.njk

+53-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
{% from "../hint/macro.njk" import govukHint %}
44
{% from "../label/macro.njk" import govukLabel %}
55

6+
{#- Set classes for this component #}
7+
{%- set classNames = "govuk-input" -%}
8+
9+
{%- if params.classes %}
10+
{% set classNames = classNames + " " + params.classes %}
11+
{% endif %}
12+
13+
{%- if params.errorMessage %}
14+
{% set classNames = classNames + " govuk-input--error" %}
15+
{% endif %}
16+
617
{#- a record of other elements that we need to associate with the input using
718
aria-describedby – for example hints or error messages -#}
819
{% set describedBy = params.describedBy if params.describedBy else "" -%}
@@ -13,15 +24,48 @@
1324
{%- set hasAfterInput = true if params.formGroup.afterInput and (params.formGroup.afterInput.text or params.formGroup.afterInput.html) else false %}
1425

1526
{%- macro _inputElement(params) -%}
16-
<input class="govuk-input {%- if params.classes %} {{ params.classes }}{% endif %} {%- if params.errorMessage %} govuk-input--error{% endif %}" id="{{ params.id }}" name="{{ params.name }}" type="{{ params.type | default("text", true) }}"
17-
{%- if (params.spellcheck === false) or (params.spellcheck === true) %} spellcheck="{{ params.spellcheck }}"{% endif %}
18-
{%- if params.value %} value="{{ params.value }}"{% endif %}
19-
{%- if params.disabled %} disabled{% endif %}
20-
{%- if describedBy %} aria-describedby="{{ describedBy }}"{% endif %}
21-
{%- if params.autocomplete %} autocomplete="{{ params.autocomplete }}"{% endif %}
22-
{%- if params.pattern %} pattern="{{ params.pattern }}"{% endif %}
23-
{%- if params.inputmode %} inputmode="{{ params.inputmode }}"{% endif %}
24-
{%- if params.autocapitalize %} autocapitalize="{{ params.autocapitalize }}"{% endif %}
27+
<input
28+
{{- govukAttributes({
29+
class: classNames,
30+
id: params.id,
31+
name: params.name,
32+
type: params.type | default("text", true),
33+
spellcheck: {
34+
value: params.spellcheck | string
35+
if [true, false].includes(params.spellcheck)
36+
else false,
37+
optional: true
38+
},
39+
value: {
40+
value: params.value,
41+
optional: true
42+
},
43+
disabled: {
44+
value: params.disabled,
45+
optional: true
46+
},
47+
"aria-describedby": {
48+
value: describedBy,
49+
optional: true
50+
},
51+
autocomplete: {
52+
value: params.autocomplete,
53+
optional: true
54+
},
55+
autocapitalize: {
56+
value: params.autocapitalize,
57+
optional: true
58+
},
59+
pattern: {
60+
value: params.pattern,
61+
optional: true
62+
},
63+
inputmode: {
64+
value: params.inputmode,
65+
optional: true
66+
}
67+
}) -}}
68+
2569
{{- govukAttributes(params.attributes) }}>
2670
{%- endmacro -%}
2771

packages/govuk-frontend/src/govuk/components/input/template.test.js

+7
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ describe('Input', () => {
7171
expect($component.val()).toBe('QQ 12 34 56 C')
7272
})
7373

74+
it('renders with zero value', () => {
75+
const $ = render('input', examples['zero value'])
76+
77+
const $component = $('.govuk-input')
78+
expect($component.val()).toBe('0')
79+
})
80+
7481
it('renders with aria-describedby', () => {
7582
const $ = render('input', examples['with describedBy'])
7683

0 commit comments

Comments
 (0)