Sha256: c6d375e8d4e28874b62b186432dacd80fd0d75e063d509c6c726946715b4df98

Contents?: true

Size: 1.96 KB

Versions: 3

Compression:

Stored size: 1.96 KB

Contents

module GOVUKDesignSystemFormBuilder
  module Elements
    class Label < GOVUKDesignSystemFormBuilder::Base
      def initialize(builder, object_name, attribute_name, text: nil, value: nil, size: nil, hidden: false, radio: false, checkbox: false, tag: nil, link_errors: true)
        super(builder, object_name, attribute_name)

        @text           = label_text(text, hidden)
        @value          = value # used by field_id
        @size_class     = label_size_class(size)
        @radio_class    = radio_class(radio)
        @checkbox_class = checkbox_class(checkbox)
        @tag            = tag
        @link_errors    = link_errors
      end

      def html
        return nil if @text.blank?

        if @tag.present?
          content_tag(@tag, class: 'govuk-label-wrapper') { build_label }
        else
          build_label
        end
      end

    private

      def build_label
        @builder.label(
          @attribute_name,
          value: @value,
          for: field_id(link_errors: @link_errors),
          class: %w(govuk-label).push(@size_class, @weight_class, @radio_class, @checkbox_class).compact
        ) do
          @text
        end
      end

      def label_text(option_text, hidden)
        text = [option_text, @value, @attribute_name.capitalize].compact.first

        if hidden
          tag.span(text, class: %w(govuk-visually-hidden))
        else
          raw(text)
        end
      end

      def radio_class(radio)
        radio ? 'govuk-radios__label' : nil
      end

      def checkbox_class(checkbox)
        checkbox ? 'govuk-checkboxes__label' : nil
      end

      def label_size_class(size)
        case size
        when 'xl'      then "govuk-label--xl"
        when 'l'       then "govuk-label--l"
        when 'm'       then "govuk-label--m"
        when 's'       then "govuk-label--s"
        when nil       then nil
        else
          fail "invalid size '#{size}', must be xl, l, m, s or nil"
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
govuk_design_system_formbuilder-1.0.1 lib/govuk_design_system_formbuilder/elements/label.rb
govuk_design_system_formbuilder-1.0.0 lib/govuk_design_system_formbuilder/elements/label.rb
govuk_design_system_formbuilder-0.9.8 lib/govuk_design_system_formbuilder/elements/label.rb