mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-05-20 08:39:21 -04:00
- Replace usaRadioLabel with usaLabel for checkbox labels - Update hint classes from govuk-checkboxes__hint to usa-checkbox__label-description - Change conditional reveal classes from govuk-specific to USWDS display-none utility - Update template comments to reflect USWDS migration - Add checkbox-specific routes to BackstopJS configuration for testing This establishes a baseline for checkbox component migration with expected spacing differences between GOV.UK and USWDS implementations.
144 lines
5.9 KiB
Plaintext
144 lines
5.9 KiB
Plaintext
{% from "components/components/error-message/macro.njk" import usaErrorMessage -%}
|
||
{% from "components/components/fieldset/macro.njk" import govukFieldset %}
|
||
{% from "components/components/hint/macro.njk" import usaHint %}
|
||
{% from "components/components/label/macro.njk" import usaLabel %}
|
||
|
||
|
||
{#- Updated from GOV.UK components to USWDS patterns
|
||
Changed from original GOV.UK template:
|
||
- Uses USWDS checkbox classes (usa-checkbox, usa-checkbox__input, usa-checkbox__label)
|
||
- Conditional content uses display-none utility class instead of govuk-specific classes
|
||
- Label uses usaLabel macro instead of radio label
|
||
- Hint text uses usa-checkbox__label-description class
|
||
- Maintains all original functionality including conditional reveals and nested checkboxes -#}
|
||
{#- If an id 'prefix' is not passed, fall back to using the name attribute
|
||
instead. We need this for error messages and hints as well -#}
|
||
{% set idPrefix = params.idPrefix if params.idPrefix else params.name %}
|
||
|
||
{% if 'formGroup' not in params %}
|
||
{% set formGroup = True %}
|
||
{% else %}
|
||
{% set formGroup = params.formGroup %}
|
||
{% 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 "" %}
|
||
{% if params.fieldset.describedBy %}
|
||
{% set describedBy = params.fieldset.describedBy %}
|
||
{% endif %}
|
||
|
||
{#- set the types of element used for the checkboxes and their group based on
|
||
whether asList is set -#}
|
||
{% if params.asList %}
|
||
{% set groupElement = 'ul' %}
|
||
{% set groupItemElement = 'li' %}
|
||
{% else %}
|
||
{% set groupElement = 'div' %}
|
||
{% set groupItemElement = 'div' %}
|
||
{% endif %}
|
||
|
||
{% set isConditional = false %}
|
||
{% for item in params.items %}
|
||
{% if item.conditional %}
|
||
{% set isConditional = true %}
|
||
{% endif %}
|
||
{% endfor %}
|
||
|
||
{#- fieldset is false by default -#}
|
||
{% set hasFieldset = true if params.fieldset else false %}
|
||
|
||
{#- Capture the HTML so we can optionally nest it in a fieldset -#}
|
||
{% set innerHtml %}
|
||
{% if params.hint %}
|
||
{% set hintId = idPrefix + '-hint' %}
|
||
{% set describedBy = describedBy + ' ' + hintId if describedBy else hintId %}
|
||
{{ usaHint({
|
||
id: hintId,
|
||
classes: params.hint.classes,
|
||
attributes: params.hint.attributes,
|
||
html: params.hint.html,
|
||
text: params.hint.text
|
||
}) | indent(2) | trim }}
|
||
{% endif %}
|
||
{% if params.errorMessage %}
|
||
{% set errorId = idPrefix + '-error' %}
|
||
{% set describedBy = describedBy + ' ' + errorId if describedBy else errorId %}
|
||
{{ usaErrorMessage({
|
||
id: errorId,
|
||
classes: params.errorMessage.classes,
|
||
attributes: params.errorMessage.attributes,
|
||
html: params.errorMessage.html,
|
||
text: params.errorMessage.text,
|
||
visuallyHiddenText: params.errorMessage.visuallyHiddenText
|
||
}) | indent(2) | trim }}
|
||
{% endif %}
|
||
<{{ groupElement }} class="{%- if params.classes %} {{ params.classes }}{% endif %}"
|
||
{%- for attribute, value in params.attributes %} {{ attribute }}="{{ value }}"{% endfor %}
|
||
{%- if isConditional %} data-module="checkboxes"{% endif -%}>
|
||
{% for item in params.items %}
|
||
{% set id = item.id if item.id else idPrefix + "-" + loop.index %}
|
||
{% set name = item.name if item.name else params.name %}
|
||
{% set conditionalId = "conditional-" + id %}
|
||
{% set hasHint = true if item.hint.text or item.hint.html %}
|
||
{% set itemHintId = id + "-item-hint" if hasHint else "" %}
|
||
{% set itemDescribedBy = describedBy if not hasFieldset else "" %}
|
||
{% set itemDescribedBy = (itemDescribedBy + " " + itemHintId) | trim %}
|
||
<{{ groupItemElement }} class="usa-checkbox {%- if item.classes %} {{ item.classes }}{% endif %}">
|
||
{%- if item.before %}{{ item.before }}{% endif -%}
|
||
<input class="usa-checkbox__input" id="{{ id }}" name="{{ name }}" type="checkbox" value="{{ item.value }}"
|
||
{{-" checked" if item.checked }}
|
||
{{-" disabled" if item.disabled }}
|
||
{%- if item.conditional %} data-aria-controls="{{ conditionalId }}"{% endif -%}
|
||
{%- if itemDescribedBy %} aria-describedby="{{ itemDescribedBy }}"{% endif -%}
|
||
{%- for attribute, value in item.attributes %} {{ attribute }}="{{ value }}"{% endfor -%}>
|
||
{{ usaLabel({
|
||
html: item.html,
|
||
text: item.text,
|
||
classes: 'usa-checkbox__label' + (' ' + item.label.classes if item.label.classes),
|
||
attributes: item.label.attributes,
|
||
for: id
|
||
}) | indent(6) | trim }}
|
||
{%- if hasHint %}
|
||
{{ usaHint({
|
||
id: itemHintId,
|
||
classes: 'usa-checkbox__label-description' + (' ' + item.hint.classes if item.hint.classes),
|
||
attributes: item.hint.attributes,
|
||
html: item.hint.html,
|
||
text: item.hint.text
|
||
}) | indent(6) | trim }}
|
||
{%- endif %}
|
||
{%- if item.children %}
|
||
{{ item.children | safe }}
|
||
{%- endif %}
|
||
{% if params.asList and item.conditional %}
|
||
<div class="usa-checkbox__conditional{% if not item.checked %} display-none{% endif %}" id="{{ conditionalId }}">
|
||
{{ item.conditional.html | safe }}
|
||
</div>
|
||
{% endif %}
|
||
{%- if item.after %}{{ item.after }}{% endif -%}
|
||
</{{ groupItemElement }}>
|
||
{% if not params.asList and item.conditional %}
|
||
<div class="usa-checkbox__conditional{% if not item.checked %} display-none{% endif %}" id="{{ conditionalId }}">
|
||
{{ item.conditional.html | safe }}
|
||
</div>
|
||
{% endif %}
|
||
{% endfor %}
|
||
</{{ groupElement }}>
|
||
{% endset -%}
|
||
|
||
<div class="{% if formGroup %}usa-form-group{% endif %} {%- if params.errorMessage %} usa-form-group--error{% endif %} {%- if params.formGroup.classes %} {{ params.formGroup.classes }}{% endif %}">
|
||
{% if params.fieldset %}
|
||
{% call govukFieldset({
|
||
describedBy: describedBy,
|
||
classes: params.fieldset.classes,
|
||
attributes: params.fieldset.attributes,
|
||
legend: params.fieldset.legend
|
||
}) %}
|
||
{{ innerHtml | trim | safe }}
|
||
{% endcall %}
|
||
{% else %}
|
||
{{ innerHtml | trim | safe }}
|
||
{% endif %}
|
||
</div>
|