Sha256: 2bf27f8c483b2778fb3eed0e90c1a20f3c49fd1b1409f1299c4910b9cc142517

Contents?: true

Size: 1.77 KB

Versions: 2

Compression:

Stored size: 1.77 KB

Contents

module GOVUKDesignSystemFormBuilder
  module Elements
    class Submit < Base
      using PrefixableArray

      def initialize(builder, text, warning:, secondary:, classes:, prevent_double_click:, validate:, disabled:, &block)
        super(builder, nil, nil)

        fail ArgumentError, 'buttons can be warning or secondary' if warning && secondary

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

      def html
        safe_join([submit, @block_content])
      end

    private

      def submit
        @builder.submit(@text, class: classes, **options)
      end

      def classes
        %w(button)
          .prefix(brand)
          .push(warning_class, secondary_class, disabled_class, padding_class, custom_classes)
          .flatten
          .compact
      end

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

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

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

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

      def disabled_class
        %(#{brand}-button--disabled) if @disabled
      end

      def custom_classes
        Array.wrap(@classes)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
govuk_design_system_formbuilder-2.1.7 lib/govuk_design_system_formbuilder/elements/submit.rb
govuk_design_system_formbuilder-2.1.7b2 lib/govuk_design_system_formbuilder/elements/submit.rb