From 06bd4852c83740a70ab131a4c00de8da88ff3d68 Mon Sep 17 00:00:00 2001 From: Patrick Cartlidge Date: Thu, 2 May 2024 10:46:20 +0100 Subject: [PATCH] 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. --- .../src/govuk/components/input/input.yaml | 8 +++ .../src/govuk/components/input/template.njk | 64 ++++++++++++++++--- .../govuk/components/input/template.test.js | 7 ++ 3 files changed, 69 insertions(+), 10 deletions(-) diff --git a/packages/govuk-frontend/src/govuk/components/input/input.yaml b/packages/govuk-frontend/src/govuk/components/input/input.yaml index 439bc6a333..09c74b6fe2 100644 --- a/packages/govuk-frontend/src/govuk/components/input/input.yaml +++ b/packages/govuk-frontend/src/govuk/components/input/input.yaml @@ -399,6 +399,14 @@ examples: label: text: With value value: QQ 12 34 56 C + - name: zero value + hidden: true + options: + id: with-zero-value + name: with-zero-value + label: + text: With zero value + value: 0 - name: with describedBy hidden: true options: diff --git a/packages/govuk-frontend/src/govuk/components/input/template.njk b/packages/govuk-frontend/src/govuk/components/input/template.njk index bb4666d4ac..124821b60d 100644 --- a/packages/govuk-frontend/src/govuk/components/input/template.njk +++ b/packages/govuk-frontend/src/govuk/components/input/template.njk @@ -3,6 +3,17 @@ {% from "../hint/macro.njk" import govukHint %} {% from "../label/macro.njk" import govukLabel %} +{#- Set classes for this component #} +{%- set classNames = "govuk-input" -%} + +{%- if params.classes %} + {% set classNames = classNames + " " + params.classes %} +{% endif %} + +{%- if params.errorMessage %} + {% set classNames = classNames + " govuk-input--error" %} +{% endif %} + {#- a record of other elements that we need to associate with the input using aria-describedby – for example hints or error messages -#} {% set describedBy = params.describedBy if params.describedBy else "" -%} @@ -13,15 +24,48 @@ {%- set hasAfterInput = true if params.formGroup.afterInput and (params.formGroup.afterInput.text or params.formGroup.afterInput.html) else false %} {%- macro _inputElement(params) -%} - {%- endmacro -%} @@ -67,7 +111,7 @@ {%- if hasPrefix or hasSuffix or hasBeforeInput or hasAfterInput %}
+ {{- govukAttributes(params.inputWrapper.attributes) }}> {% if hasBeforeInput %} {{- params.formGroup.beforeInput.html | safe | trim | indent(4, true) if params.formGroup.beforeInput.html else params.formGroup.beforeInput.text }} {% endif %} diff --git a/packages/govuk-frontend/src/govuk/components/input/template.test.js b/packages/govuk-frontend/src/govuk/components/input/template.test.js index 013b983694..d8795ba14a 100644 --- a/packages/govuk-frontend/src/govuk/components/input/template.test.js +++ b/packages/govuk-frontend/src/govuk/components/input/template.test.js @@ -71,6 +71,13 @@ describe('Input', () => { expect($component.val()).toBe('QQ 12 34 56 C') }) + it('renders with zero value', () => { + const $ = render('input', examples['zero value']) + + const $component = $('.govuk-input') + expect($component.val()).toBe('0') + }) + it('renders with aria-describedby', () => { const $ = render('input', examples['with describedBy'])