From 913b77176d327095f9bf4d860d1919898e115f2e Mon Sep 17 00:00:00 2001 From: Patrick Cartlidge Date: Thu, 2 May 2024 10:46:20 +0100 Subject: [PATCH] Fix input value not being set if the value was '0' 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 --- CHANGELOG.md | 1 + .../src/govuk/components/input/input.yaml | 8 +++ .../src/govuk/components/input/template.njk | 64 ++++++++++++++++--- .../govuk/components/input/template.test.js | 7 ++ 4 files changed, 70 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c7d6a7ce1..774b9cabf8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,7 @@ We've made fixes to GOV.UK Frontend in the following pull requests: - [#4942: Remove duplicate `errorMessage` argument for the password input component](https://github.com/alphagov/govuk-frontend/pull/4942) - thanks to [Tim South](https://github.com/tim-s-ccs) for contributing this change - [#4961: Fix tree-shaking when importing `govuk-frontend`](https://github.com/alphagov/govuk-frontend/pull/4961) +- [#4963: Fix input value not being set if the value was '0'](https://github.com/alphagov/govuk-frontend/pull/4963) – thanks to [@dwp-dmitri-algazin](https://github.com/dwp-dmitri-algazin) for reporting this issue ## 5.3.1 (Fix release) 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..c3e1431aef 100644 --- a/packages/govuk-frontend/src/govuk/components/input/template.njk +++ b/packages/govuk-frontend/src/govuk/components/input/template.njk @@ -3,9 +3,20 @@ {% 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 "" -%} +{% set describedBy = params.describedBy if params.describedBy else undefined -%} {%- set hasPrefix = true if params.prefix and (params.prefix.text or params.prefix.html) else false %} {%- set hasSuffix = true if params.suffix and (params.suffix.text or params.suffix.html) else false %} @@ -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 -%} 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'])