Sha256: ac1e9bd52a5acd9ae07a679ac5ad10e5975bbc09a812ac8ec2e683f661306974

Contents?: true

Size: 1.39 KB

Versions: 6

Compression:

Stored size: 1.39 KB

Contents

module GOVUKDesignSystemFormBuilder
  module Elements
    class Submit < GOVUKDesignSystemFormBuilder::Base
      def initialize(builder, text, warning:, secondary:, prevent_double_click:, &block)
        @builder              = builder
        @text                 = text
        @prevent_double_click = prevent_double_click

        fail ArgumentError, 'buttons can be warning or secondary' if (warning && secondary)
        @warning = warning
        @secondary = secondary
        @block_content = @builder.capture { block.call } if block_given?
      end

      def html
        @builder.content_tag('div', class: %w(govuk-form-group)) do
          @builder.safe_join([
            @builder.submit(@text, class: submit_button_classes(@block_content.present?), **extra_args),
            @block_content
          ])
        end
      end
    private

      def submit_button_classes(content_present)
        %w(govuk-button).tap do |classes|
          classes.push('govuk-button--warning') if @warning
          classes.push('govuk-button--secondary') if @secondary

          # NOTE only this input will receive a right margin, block
          # contents must be addressed individually
          classes.push('govuk-!-margin-right-1') if content_present
        end
      end

      def extra_args
        { data: { 'prevent-double-click' => (@prevent_double_click || nil) }.compact }
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
govuk_design_system_formbuilder-0.7.6 lib/govuk_design_system_formbuilder/elements/submit.rb
govuk_design_system_formbuilder-0.7.5 lib/govuk_design_system_formbuilder/elements/submit.rb
govuk_design_system_formbuilder-0.7.4 lib/govuk_design_system_formbuilder/elements/submit.rb
govuk_design_system_formbuilder-0.7.3 lib/govuk_design_system_formbuilder/elements/submit.rb
govuk_design_system_formbuilder-0.7.2 lib/govuk_design_system_formbuilder/elements/submit.rb
govuk_design_system_formbuilder-0.7.1 lib/govuk_design_system_formbuilder/elements/submit.rb