Sha256: 4773726f0b599de6bb4ec9931c45f2b2b5627e63e115b22a5d8119a49d7420bc

Contents?: true

Size: 1.87 KB

Versions: 10

Compression:

Stored size: 1.87 KB

Contents

module GOVUKDesignSystemFormBuilder
  module Elements
    class Legend < Base
      include Traits::Caption
      include Traits::Localisation

      def initialize(builder, object_name, attribute_name, text: nil, size: config.default_legend_size, hidden: false, tag: config.default_legend_tag, caption: nil, content: nil, **kwargs)
        super(builder, object_name, attribute_name)

        if content
          @content = capture { content.call }
        else
          @text            = retrieve_text(text)
          @tag             = tag
          @size_class      = size_class(size)
          @tag             = tag
          @caption         = caption
          @hidden          = hidden
          @html_attributes = kwargs
        end
      end

      def html
        @content || legend
      end

    private

      def active?
        [@text, @content].any?(&:present?)
      end

      def legend
        return unless active?

        tag.legend(legend_content, class: classes, **@html_attributes)
      end

      def legend_content
        caption_and_text = safe_join([caption_element, @text])

        if @tag.present?
          content_tag(@tag, class: heading_classes) { caption_and_text }
        else
          caption_and_text
        end
      end

      def retrieve_text(supplied_text)
        [supplied_text, localised_text(:legend), @attribute_name&.capitalize].compact.first
      end

      def classes
        [%(#{brand}-fieldset__legend), @size_class, visually_hidden_class].compact
      end

      def size_class(size)
        if size.in?(%w(xl l m s))
          %(#{brand}-fieldset__legend--#{size})
        else
          fail "invalid size '#{size}', must be xl, l, m or s"
        end
      end

      def visually_hidden_class
        %(#{brand}-visually-hidden) if @hidden
      end

      def heading_classes
        %(#{brand}-fieldset__heading)
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
govuk_design_system_formbuilder-2.3.0b1 lib/govuk_design_system_formbuilder/elements/legend.rb
govuk_design_system_formbuilder-2.2.0 lib/govuk_design_system_formbuilder/elements/legend.rb
govuk_design_system_formbuilder-2.1.9 lib/govuk_design_system_formbuilder/elements/legend.rb
govuk_design_system_formbuilder-2.1.8 lib/govuk_design_system_formbuilder/elements/legend.rb
govuk_design_system_formbuilder-2.1.7 lib/govuk_design_system_formbuilder/elements/legend.rb
govuk_design_system_formbuilder-2.1.7b2 lib/govuk_design_system_formbuilder/elements/legend.rb
govuk_design_system_formbuilder-2.1.7b1 lib/govuk_design_system_formbuilder/elements/legend.rb
govuk_design_system_formbuilder-2.1.6 lib/govuk_design_system_formbuilder/elements/legend.rb
govuk_design_system_formbuilder-2.1.5 lib/govuk_design_system_formbuilder/elements/legend.rb
govuk_design_system_formbuilder-2.1.4 lib/govuk_design_system_formbuilder/elements/legend.rb