Sha256: 3763483b5642b62aac836fdeb7d50730cc34b997a1413486a932233cfd7cf282

Contents?: true

Size: 1.61 KB

Versions: 4

Compression:

Stored size: 1.61 KB

Contents

module GOVUKDesignSystemFormBuilder
  module Elements
    class Submit < Base
      using PrefixableArray

      def initialize(builder, text, warning:, secondary:, classes:, prevent_double_click:, validate:, &block)
        fail ArgumentError, 'buttons can be warning or secondary' if warning && secondary

        @builder              = builder
        @text                 = text
        @prevent_double_click = prevent_double_click
        @warning              = warning
        @secondary            = secondary
        @classes              = classes
        @validate             = validate
        @block_content        = capture { block.call } if block_given?
      end

      def html
        safe_join(
          [
            @builder.submit(
              @text,
              class: %w(button).prefix(brand).push(
                warning_class,
                secondary_class,
                @classes,
                padding_class(@block_content.present?)
              ).compact,
              **extra_args
            ),
            @block_content
          ]
        )
      end

    private

      def warning_class
        %(#{brand}-button--warning) if @warning
      end

      def secondary_class
        %(#{brand}-button--secondary) if @secondary
      end

      def padding_class(content_present)
        %(#{brand}-!-margin-right-1) if content_present
      end

      def extra_args
        {
          formnovalidate: !@validate,
          data: {
            module: %(#{brand}-button), 'prevent-double-click' => @prevent_double_click
          }.select { |_k, v| v.present? }
        }
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
govuk_design_system_formbuilder-1.2.0 lib/govuk_design_system_formbuilder/elements/submit.rb
govuk_design_system_formbuilder-1.2.0b4 lib/govuk_design_system_formbuilder/elements/submit.rb
govuk_design_system_formbuilder-1.2.0b2 lib/govuk_design_system_formbuilder/elements/submit.rb
govuk_design_system_formbuilder-1.2.0b1 lib/govuk_design_system_formbuilder/elements/submit.rb