<% id_prefix ||= "radio-#{SecureRandom.hex(4)}" items ||= [] label ||= nil hint ||= nil error_message ||= nil hint_id = "hint-#{SecureRandom.hex(4)}" if hint error_message_id = "error-message-#{SecureRandom.hex(4)}" if error_message form_group_css_classes = %w(govuk-form-group) form_group_css_classes << "govuk-form-group--error" if error_message # check if any item is set as being conditional has_conditional = items.any? { |item| item.is_a?(Hash) && item[:conditional] } %> <%= content_tag :div, class: form_group_css_classes do %> <% if hint %> <%= render "govuk_publishing_components/components/hint", { id: hint_id, text: hint } %> <% end %> <% if error_message %> <%= render "govuk_publishing_components/components/error_message", { id: error_message_id, text: error_message } %> <% end %> <%= content_tag :div, class: "govuk-radios", data: { module: ('radios' if has_conditional) } do %> <% items.each_with_index do |item, index| %> <% if item === :or %>
<%= t('components.radio.or') %>
<% else %> <% item_next = items[index + 1] unless index === items.size - 1 label_id = item[:id] ? item[:id] : "#{id_prefix}-#{index}" label_hint_id = "label-hint-#{SecureRandom.hex(4)}" if item[:hint_text].present? conditional_id = "conditional-#{SecureRandom.hex(4)}" if item[:conditional].present? %> <%= tag.div class: %w( gem-c-radio govuk-radios__item ) do %> <%= check_box_tag name, item[:value], item[:checked], { class: "govuk-radios__input", id: label_id, type: "radio", aria: { describedby: label_hint_id }, data: { "aria-controls": conditional_id } } %> <%= render "govuk_publishing_components/components/label", { hint_id: label_hint_id, html_for: label_id, classes: "govuk-radios__label", hint_text_classes: "govuk-radios__hint", hint_text: item[:hint_text], text: item[:text], bold: item[:bold] } %> <% end %> <% if item[:conditional] %>
<%= item[:conditional] %>
<% end %> <% end %> <% end %> <% end %> <% end %>