From 064b56783d2f4174e37cddcbfbc6e2d0d09b6864 Mon Sep 17 00:00:00 2001 From: beeps Date: Mon, 27 Jan 2025 16:36:42 +0000 Subject: [PATCH 1/3] Make form control ID attributes optional Update the following components to use their `name` attribute as the default value for the `id` attribute. Mark the `id` parameter as optional in documentation. - Character count - File upload - (Text) input - Password input - Select - Textarea --- .../components/character-count/character-count.yaml | 4 ++-- .../govuk/components/character-count/template.njk | 9 +++++---- .../govuk/components/file-upload/file-upload.yaml | 4 ++-- .../src/govuk/components/file-upload/template.njk | 12 +++++++----- .../src/govuk/components/input/input.yaml | 4 ++-- .../src/govuk/components/input/template.njk | 9 +++++---- .../components/password-input/password-input.yaml | 4 ++-- .../src/govuk/components/password-input/template.njk | 6 ++++-- .../src/govuk/components/select/select.yaml | 4 ++-- .../src/govuk/components/select/template.njk | 10 ++++++---- .../src/govuk/components/textarea/template.njk | 10 ++++++---- .../src/govuk/components/textarea/textarea.yaml | 4 ++-- 12 files changed, 45 insertions(+), 35 deletions(-) diff --git a/packages/govuk-frontend/src/govuk/components/character-count/character-count.yaml b/packages/govuk-frontend/src/govuk/components/character-count/character-count.yaml index cd7369e179..0bb16cf374 100644 --- a/packages/govuk-frontend/src/govuk/components/character-count/character-count.yaml +++ b/packages/govuk-frontend/src/govuk/components/character-count/character-count.yaml @@ -1,8 +1,8 @@ params: - name: id type: string - required: true - description: The ID of the textarea. + required: false + description: The ID of the textarea. Defaults to the value of `name`. - name: name type: string required: true diff --git a/packages/govuk-frontend/src/govuk/components/character-count/template.njk b/packages/govuk-frontend/src/govuk/components/character-count/template.njk index d2c4ce017b..bf524ba63b 100644 --- a/packages/govuk-frontend/src/govuk/components/character-count/template.njk +++ b/packages/govuk-frontend/src/govuk/components/character-count/template.njk @@ -12,11 +12,12 @@ {%- set textareaDescriptionLength = params.maxwords or params.maxlength -%} {%- set textareaDescriptionText = params.textareaDescriptionText or 'You can enter up to %{count} ' + ('words' if params.maxwords else 'characters') -%} {%- set textareaDescriptionTextNoLimit = textareaDescriptionText | replace('%{count}', textareaDescriptionLength) if not hasNoLimit -%} +{%- set id = params.id if params.id else params.name -%} {%- set countMessageHtml %} {{ govukHint({ text: textareaDescriptionTextNoLimit, - id: params.id + '-info', + id: id + '-info', classes: 'govuk-character-count__message' + (' ' + params.countMessage.classes if params.countMessage.classes) }) | trim }} {% if params.formGroup.afterInput %} @@ -91,9 +92,9 @@ {% endfor -%} {{ govukTextarea({ - id: params.id, + id: id, name: params.name, - describedBy: params.id + '-info', + describedBy: id + '-info', rows: params.rows, spellcheck: params.spellcheck, value: params.value, @@ -112,7 +113,7 @@ classes: params.label.classes, isPageHeading: params.label.isPageHeading, attributes: params.label.attributes, - for: params.id + for: id }, hint: params.hint, errorMessage: params.errorMessage, diff --git a/packages/govuk-frontend/src/govuk/components/file-upload/file-upload.yaml b/packages/govuk-frontend/src/govuk/components/file-upload/file-upload.yaml index fde79e6588..455465362f 100644 --- a/packages/govuk-frontend/src/govuk/components/file-upload/file-upload.yaml +++ b/packages/govuk-frontend/src/govuk/components/file-upload/file-upload.yaml @@ -5,8 +5,8 @@ params: description: The name of the input, which is submitted with the form data. - name: id type: string - required: true - description: The ID of the input. + required: false + description: The ID of the input. Defaults to the value of `name`. - name: value type: string required: false diff --git a/packages/govuk-frontend/src/govuk/components/file-upload/template.njk b/packages/govuk-frontend/src/govuk/components/file-upload/template.njk index a3b11c7b90..d2386de84d 100644 --- a/packages/govuk-frontend/src/govuk/components/file-upload/template.njk +++ b/packages/govuk-frontend/src/govuk/components/file-upload/template.njk @@ -5,7 +5,9 @@ {#- 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 "" -%} +{%- set id = params.id if params.id else params.name -%} +
{{ govukLabel({ @@ -14,10 +16,10 @@ classes: params.label.classes, isPageHeading: params.label.isPageHeading, attributes: params.label.attributes, - for: params.id + for: id }) | trim | indent(2) }} {% if params.hint %} - {% set hintId = params.id + '-hint' %} + {% set hintId = id + '-hint' %} {% set describedBy = describedBy + ' ' + hintId if describedBy else hintId %} {{ govukHint({ id: hintId, @@ -28,7 +30,7 @@ }) | trim | indent(2) }} {% endif %} {% if params.errorMessage %} - {% set errorId = params.id + '-error' %} + {% set errorId = id + '-error' %} {% set describedBy = describedBy + ' ' + errorId if describedBy else errorId %} {{ govukErrorMessage({ id: errorId, @@ -42,7 +44,7 @@ {% if params.formGroup.beforeInput %} {{ params.formGroup.beforeInput.html | safe | trim | indent(2) if params.formGroup.beforeInput.html else params.formGroup.beforeInput.text }} {% endif %} - {{ govukLabel({ @@ -14,10 +16,10 @@ classes: params.label.classes, isPageHeading: params.label.isPageHeading, attributes: params.label.attributes, - for: params.id + for: id }) | trim | indent(2) }} {% if params.hint %} - {% set hintId = params.id + '-hint' %} + {% set hintId = id + '-hint' %} {% set describedBy = describedBy + ' ' + hintId if describedBy else hintId %} {{ govukHint({ id: hintId, @@ -28,7 +30,7 @@ }) | trim | indent(2) }} {% endif %} {% if params.errorMessage %} - {% set errorId = params.id + '-error' %} + {% set errorId = id + '-error' %} {% set describedBy = describedBy + ' ' + errorId if describedBy else errorId %} {{ govukErrorMessage({ id: errorId, @@ -43,7 +45,7 @@ {{ params.formGroup.beforeInput.html | safe | trim | indent(2) if params.formGroup.beforeInput.html else params.formGroup.beforeInput.text }} {% endif %}