Sha256: 2732637dab2a345920e0b0f51fd0a11e3f34bfaaaeae842a09eee6a19c6db766

Contents?: true

Size: 1.9 KB

Versions: 2

Compression:

Stored size: 1.9 KB

Contents

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

      def initialize(builder, object_name, attribute_name, legend:, caption:)
        super(builder, object_name, attribute_name)

        case legend
        when Proc
          @raw = legend.call
        when Hash
          defaults.merge(legend).tap do |l|
            @text    = text(l.dig(:text))
            @hidden  = l.dig(:hidden)
            @tag     = l.dig(:tag)
            @size    = l.dig(:size)
            @caption = caption
          end
        else
          fail(ArgumentError, %(legend must be a Proc or Hash))
        end
      end

      def html
        @raw || content
      end

    private

      def content
        if @text.present?
          content_tag('legend', class: classes) do
            content_tag(@tag, class: heading_classes) do
              safe_join([caption_element, @text])
            end
          end
        end
      end

      def text(supplied_text)
        supplied_text || localised_text(:legend)
      end

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

      def size_class
        case @size
        when 'xl' then %(#{brand}-fieldset__legend--xl)
        when 'l'  then %(#{brand}-fieldset__legend--l)
        when 'm'  then %(#{brand}-fieldset__legend--m)
        when 's'  then %(#{brand}-fieldset__legend--s)
        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

      def defaults
        {
          hidden: false,
          text: nil,
          tag:  config.default_legend_tag,
          size: config.default_legend_size
        }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
govuk_design_system_formbuilder-1.2.6 lib/govuk_design_system_formbuilder/elements/legend.rb
govuk_design_system_formbuilder-1.2.5 lib/govuk_design_system_formbuilder/elements/legend.rb