diff --git a/app/helpers/question_view_helper.rb b/app/helpers/question_view_helper.rb new file mode 100644 index 000000000..ecf29e574 --- /dev/null +++ b/app/helpers/question_view_helper.rb @@ -0,0 +1,25 @@ +module QuestionViewHelper + def caption(caption, page_header, conditional) + return nil unless caption && page_header.blank? && !conditional + + { text: caption.html_safe, size: "l" } + end + + def legend(question, page_header, conditional) + { + text: question.header.html_safe, + size: label_size(page_header, conditional), + tag: label_tag(page_header, conditional), + } + end + +private + + def label_size(page_header, conditional) + page_header.blank? && !conditional ? "l" : "m" + end + + def label_tag(page_header, conditional) + page_header.blank? && !conditional ? "h1" : "h2" + end +end diff --git a/app/views/form/_checkbox_question.html.erb b/app/views/form/_checkbox_question.html.erb index d351be1d1..97120a5cd 100644 --- a/app/views/form/_checkbox_question.html.erb +++ b/app/views/form/_checkbox_question.html.erb @@ -1,8 +1,8 @@ <%= render partial: "form/guidance/#{question.guidance_partial}" if question.guidance_partial %> <%= f.govuk_check_boxes_fieldset question.id.to_sym, - caption: caption && !page_header.present? ? { text: caption.html_safe, size: "l" } : nil, - legend: { text: question.header.html_safe, size: !page_header.present? ? "l" : "m", tag: !page_header.present? ? "h1" : "h2" }, + caption: caption(caption, page_header, conditional), + legend: legend(question, page_header, conditional), hint: { text: question.hint_text&.html_safe } do %> <% question.answer_options.map do |key, val| %> diff --git a/app/views/form/_date_question.html.erb b/app/views/form/_date_question.html.erb index 3a1ddafa8..d71db38e4 100644 --- a/app/views/form/_date_question.html.erb +++ b/app/views/form/_date_question.html.erb @@ -1,8 +1,8 @@ <%= render partial: "form/guidance/#{question.guidance_partial}" if question.guidance_partial %> <%= f.govuk_date_field question.id.to_sym, - caption: caption && !page_header.present? ? { text: caption.html_safe, size: "l" } : nil, - legend: { text: question.header.html_safe, size: !page_header.present? ? "l" : "m", tag: !page_header.present? ? "h1" : "h2" }, + caption: caption(caption, page_header, conditional), + legend: legend(question, page_header, conditional), hint: { text: question.hint_text&.html_safe }, width: 20, **stimulus_html_attributes(question) diff --git a/app/views/form/_numeric_question.html.erb b/app/views/form/_numeric_question.html.erb index a0cdcb625..8356a121f 100644 --- a/app/views/form/_numeric_question.html.erb +++ b/app/views/form/_numeric_question.html.erb @@ -1,8 +1,8 @@ <%= render partial: "form/guidance/#{question.guidance_partial}" if question.guidance_partial %> <%= f.govuk_number_field question.id.to_sym, - caption: caption && !page_header.present? ? { text: caption.html_safe, size: "l" } : nil, - label: { text: question.header.html_safe, size: !page_header.present? ? "l" : "m", tag: !page_header.present? ? "h1" : "h2" }, + caption: caption(caption, page_header, conditional), + label: legend(question, page_header, conditional), hint: { text: question.hint_text&.html_safe }, min: question.min, max: question.max, step: question.step, width: question.width, :readonly => question.read_only?, diff --git a/app/views/form/_radio_question.html.erb b/app/views/form/_radio_question.html.erb index ffd18a536..83cf9f101 100644 --- a/app/views/form/_radio_question.html.erb +++ b/app/views/form/_radio_question.html.erb @@ -1,8 +1,8 @@ <%= render partial: "form/guidance/#{question.guidance_partial}" if question.guidance_partial %> <%= f.govuk_radio_buttons_fieldset question.id.to_sym, - caption: caption && !page_header.present? ? { text: caption.html_safe, size: "l" } : nil, - legend: { text: question.header.html_safe, size: !page_header.present? ? "l" : "m", tag: !page_header.present? ? "h1" : "h2" }, + caption: caption(caption, page_header, conditional), + legend: legend(question, page_header, conditional), hint: { text: question.hint_text&.html_safe } do %> <% question.answer_options.map do |key, val| %> diff --git a/app/views/form/_select_question.html.erb b/app/views/form/_select_question.html.erb index 6909d8470..e02142869 100644 --- a/app/views/form/_select_question.html.erb +++ b/app/views/form/_select_question.html.erb @@ -6,8 +6,8 @@ answers, :id, :name, - caption: caption && !page_header.present? ? { text: caption.html_safe, size: "l" } : nil, - label: { text: question.header, size: !page_header.present? ? "l" : "m", tag: !page_header.present? ? "h1" : "h2" }, + caption: caption(caption, page_header, conditional), + label: legend(question, page_header, conditional), hint: { text: question.hint_text&.html_safe }, options: { disabled: [""], selected: selected }, "data-controller": "accessible-autocomplete" diff --git a/app/views/form/_text_question.html.erb b/app/views/form/_text_question.html.erb index dbf1cd92e..11bf9ad18 100644 --- a/app/views/form/_text_question.html.erb +++ b/app/views/form/_text_question.html.erb @@ -1,8 +1,8 @@ <%= render partial: "form/guidance/#{question.guidance_partial}" if question.guidance_partial %> <%= f.govuk_text_field question.id.to_sym, - caption: caption && !page_header.present? ? { text: caption.html_safe, size: "l" } : nil, - label: { text: question.header.html_safe, size: !page_header.present? ? "l" : "m", tag: !page_header.present? ? "h1" : "h2" }, + caption: caption(caption, page_header, conditional), + label: legend(question, page_header, conditional), hint: { text: question.hint_text&.html_safe }, width: question.width ? question.width : nil, **stimulus_html_attributes(question) diff --git a/app/views/form/_textarea_question.html.erb b/app/views/form/_textarea_question.html.erb index 3623d32c7..8ac2e44d7 100644 --- a/app/views/form/_textarea_question.html.erb +++ b/app/views/form/_textarea_question.html.erb @@ -1,8 +1,8 @@ <%= render partial: "form/guidance/#{question.guidance_partial}" if question.guidance_partial %> <%= f.govuk_text_area question.id.to_sym, - caption: caption && !page_header.present? ? { text: caption.html_safe, size: "l" } : nil, - label: { text: question.header.html_safe, size: !page_header.present? ? "l" : "m", tag: !page_header.present? ? "h1" : "h2" }, + caption: caption(caption, page_header, conditional), + label: legend(question, page_header, conditional), hint: { text: question.hint_text&.html_safe }, width: question.width ? question.width : nil, **stimulus_html_attributes(question) diff --git a/app/views/form/page.html.erb b/app/views/form/page.html.erb index 92922f76c..e2ec34dc8 100644 --- a/app/views/form/page.html.erb +++ b/app/views/form/page.html.erb @@ -36,7 +36,7 @@ <% if question.read_only? %>
<% end %> - <%= render partial: "form/#{question.type}_question", locals: { question: question, caption: @subsection.label, page_header: @page.header, f: f } %> + <%= render partial: "form/#{question.type}_question", locals: { question: question, caption: @subsection.label, page_header: @page.header, f: f, conditional: false } %> <% end %>