{% macro govukRadios(params) %} {% from "govuk_frontend_jinja/macros/attributes.html" import govukAttributes %} {% from "govuk_frontend_jinja/components/error-message/macro.html" import govukErrorMessage %} {% from "govuk_frontend_jinja/components/fieldset/macro.html" import govukFieldset %} {% from "govuk_frontend_jinja/components/hint/macro.html" import govukHint %} {% from "govuk_frontend_jinja/components/label/macro.html" import govukLabel %} {#- 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 %} {% set ns = namespace() %} {#- a record of other elements that we need to associate with the input using aria-describedby – for example hints or error messages -#} {% set ns.describedBy = params.fieldset.describedBy if params.fieldset and params.fieldset.describedBy else "" %} {#- fieldset is false by default -#} {% set hasFieldset = true if params.fieldset else false %} {%- macro _radioItem(params, item, index) %} {#- If the user explicitly sets an id, use this instead of the regular idPrefix -#} {#- The first id should not have a number suffix so it's easy to link to from the error summary component -#} {% set itemId = item.id if item.id else idPrefix + ("-" ~ index if index > 1 else "") %} {% set conditionalId = "conditional-" + itemId %} {%- if item.divider %}
{{ item.divider }}
{%- else %} {% set isChecked = item.checked | default((item.value == params.value and item.checked != false) if params.value else false, true) %} {% set hasHint = true if item.hint and (item.hint.text or item.hint.html) %} {% set itemHintId = itemId + '-item-hint' %}
{{ govukLabel({ 'html': item.html, 'text': item.text, 'classes': 'govuk-radios__label' + (' ' + item.label.classes if item.label and item.label.classes else ''), 'attributes': item.label.attributes if item.label, 'for': itemId }) | trim | indent(6) }} {% if hasHint %} {{ govukHint({ 'id': itemHintId, 'classes': 'govuk-radios__hint' + (' ' + item.hint.classes if item.hint.classes else ''), 'attributes': item.hint.attributes, 'html': item.hint.html, 'text': item.hint.text }) | trim | indent(6) }} {% endif %}
{% if item.conditional and item.conditional.html %}
{{ item.conditional.html | safe | trim }}
{% endif %} {% endif %} {%- endmacro %} {#- Capture the HTML so we can optionally nest it in a fieldset -#} {% set innerHtml %} {% if params.hint %} {% set hintId = idPrefix + '-hint' %} {% set ns.describedBy = ns.describedBy + ' ' + hintId if ns.describedBy else hintId %} {{ govukHint({ 'id': hintId, 'classes': params.hint.classes, 'attributes': params.hint.attributes, 'html': params.hint.html, 'text': params.hint.text }) | trim | indent(2) }} {% endif %} {% if params.errorMessage %} {% set errorId = idPrefix + '-error' %} {% set ns.describedBy = ns.describedBy + ' ' + errorId if ns.describedBy else errorId %} {{ govukErrorMessage({ 'id': errorId, 'classes': params.errorMessage.classes, 'attributes': params.errorMessage.attributes, 'html': params.errorMessage.html, 'text': params.errorMessage.text, 'visuallyHiddenText': params.errorMessage.visuallyHiddenText }) | trim | indent(2) }} {% endif %}
{% if params.formGroup and params.formGroup.beforeInputs %} {{ params.formGroup.beforeInputs.html | safe | trim | indent(4) if params.formGroup.beforeInputs.html else params.formGroup.beforeInputs.text }} {% endif %} {% for item in params['items'] %} {% if item %} {{- _radioItem(params, item, loop.index) -}} {% endif %} {% endfor %} {% if params.formGroup and params.formGroup.afterInputs %} {{ params.formGroup.afterInputs.html | safe | trim | indent(4) if params.formGroup.afterInputs.html else params.formGroup.afterInputs.text }} {% endif %}
{% endset -%}
{% if hasFieldset %} {{ govukFieldset({ 'describedBy': ns.describedBy, 'classes': params.fieldset.classes, 'attributes': params.fieldset.attributes, 'legend': params.fieldset.legend, 'html': innerHtml | trim }) | trim | indent(2) }} {% else %} {{ innerHtml | safe | trim }} {% endif %}
{% endmacro %}