class GovukPublishingComponents::Presenters::CheckboxesHelper

Attributes

css_classes[R]
description[R]
error[R]
has_conditional[R]
has_exclusive[R]
has_nested[R]
heading_caption[R]
hint_text[R]
id[R]
items[R]
list_classes[R]
name[R]

Public Class Methods

new(options) click to toggle source
# File lib/govuk_publishing_components/presenters/checkboxes_helper.rb, line 20
def initialize(options)
  @items = options[:items] || []
  @name = options[:name]
  @css_classes = %w[gem-c-checkboxes govuk-form-group]
  @css_classes << "govuk-form-group--error" if options[:error]
  @css_classes << "govuk-checkboxes--small" if options[:small]
  @error = true if options[:error]

  @list_classes = %w[govuk-checkboxes gem-c-checkboxes__list]

  # check if any item is set as being conditional
  @has_conditional = options[:items].any? { |item| item.is_a?(Hash) && item[:conditional] }
  @has_exclusive = options[:items].any? { |item| item.is_a?(Hash) && item[:exclusive] }
  @has_nested = options[:items].any? { |item| item.is_a?(Hash) && item[:items] }

  @id = options[:id] || "checkboxes-#{SecureRandom.hex(4)}"
  @heading = options[:heading] || nil
  @heading_caption = options[:heading_caption] || nil
  @heading_size = options[:heading_size] || nil
  @is_page_heading = options[:is_page_heading]
  @description = options[:description] || nil
  @no_hint_text = options[:no_hint_text]
  @hint_text = options[:hint_text] || "Select all that apply." unless @no_hint_text
  @visually_hide_heading = options[:visually_hide_heading]
end

Public Instance Methods

checkbox_markup(checkbox, index) click to toggle source
# File lib/govuk_publishing_components/presenters/checkboxes_helper.rb, line 91
def checkbox_markup(checkbox, index)
  checkbox_id = checkbox[:id] || "#{@id}-#{index}"
  controls = checkbox[:controls]
  aria_controls = "#{checkbox_id}-conditional-#{index || rand(1..100)}" if checkbox[:conditional].present?
  checkbox_name = checkbox[:name].present? ? checkbox[:name] : @name
  checked = true if checkbox[:checked].present?
  data = checkbox[:data_attributes] || {}
  data[:controls] = controls
  data["aria-controls"] = aria_controls
  data[:exclusive] = checkbox[:exclusive]

  capture do
    concat check_box_tag checkbox_name, checkbox[:value], checked, class: "govuk-checkboxes__input", id: checkbox_id, data: data
    concat content_tag(:label, checkbox[:label], for: checkbox_id, class: "govuk-label govuk-checkboxes__label")
    concat content_tag(:span, checkbox[:hint], id: "#{checkbox_id}-item-hint", class: "govuk-hint govuk-checkboxes__hint") if checkbox[:hint]
  end
end
fieldset_describedby() click to toggle source
# File lib/govuk_publishing_components/presenters/checkboxes_helper.rb, line 52
def fieldset_describedby
  unless @no_hint_text
    text = %w[]
    text << "#{id}-hint" if @hint_text
    text << "#{id}-error" if @error
    text
  end
end
heading_markup() click to toggle source
# File lib/govuk_publishing_components/presenters/checkboxes_helper.rb, line 71
def heading_markup
  return unless @heading.present?

  if @is_page_heading
    content_tag(
      :legend,
      class: "govuk-fieldset__legend govuk-fieldset__legend--#{size}",
    ) do
      concat content_tag(:span, heading_caption, class: "govuk-caption-#{size}") if heading_caption.present?
      concat content_tag(:h1, @heading, class: "gem-c-checkboxes__heading-text govuk-fieldset__heading")
    end
  else
    classes = %w[govuk-fieldset__legend]
    classes << "govuk-fieldset__legend--#{size}"
    classes << "gem-c-checkboxes__legend--hidden" if @visually_hide_heading

    content_tag(:legend, @heading, class: classes)
  end
end
should_have_fieldset() click to toggle source

should have a fieldset if there's a heading, or if more than one checkbox separate check is in the view for if more than one checkbox and no heading, in which case fail

# File lib/govuk_publishing_components/presenters/checkboxes_helper.rb, line 48
def should_have_fieldset
  @items.length > 1 || @heading
end
size() click to toggle source
# File lib/govuk_publishing_components/presenters/checkboxes_helper.rb, line 61
def size
  if %w[s m l xl].include?(@heading_size)
    @heading_size
  elsif @is_page_heading
    "xl"
  else
    "m"
  end
end