Sha256: e102ba855a7395bac14ad0623704b664e7a46aaf70f7b57f1d75ef1a95a991dd

Contents?: true

Size: 1.96 KB

Versions: 104

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

module Primer
  module Forms
    # :nodoc:
    class Button < BaseComponent
      # :nodoc:
      module ButtonAttributeGenerator
        extend ActionView::Helpers::FormTagHelper

        class << self
          alias submit_tag_attributes submit_tag
          alias button_tag_attributes button_tag

          private

          # FormTagHelper#submit_tag ultimately calls the #tag method. We return the options hash here instead
          # of returning a string so it can be merged into the hash of options we pass to the Primer::ButtonComponent.
          def tag(_name, options)
            options
          end

          # FormTagHelper#button_tag ultimately calls the #content_tag method. We return the options hash here instead
          # of returning a string so it can be merged into the hash of options we pass to the Primer::ButtonComponent.
          def content_tag(_name, _content, options)
            options
          end
        end
      end

      delegate :builder, :form, to: :@input

      def initialize(input:, type: :button)
        @input = input
        @type = type

        @input.add_input_classes("FormField-input flex-self-start")
        @input.merge_input_arguments!(tag_attributes.deep_symbolize_keys)

        # rails uses a string for this, but PVC wants a symbol
        @input.merge_input_arguments!(type: type.to_sym)
      end

      def input_arguments
        @input_arguments ||= @input.input_arguments.deep_dup.tap do |args|
          # rails uses :class but PVC wants :classes
          args[:classes] = class_names(
            args[:classes],
            args.delete(:class)
          )
        end
      end

      private

      def tag_attributes
        case @type
        when :submit
          ButtonAttributeGenerator.submit_tag_attributes(@input.label, name: @input.name)
        else
          ButtonAttributeGenerator.button_tag_attributes(@input.label, name: @input.name)
        end
      end
    end
  end
end

Version data entries

104 entries across 104 versions & 2 rubygems

Version Path
primer_view_components-0.20.0 lib/primer/forms/button.rb
openproject-primer_view_components-0.23.0 lib/primer/forms/button.rb
primer_view_components-0.19.0 lib/primer/forms/button.rb
openproject-primer_view_components-0.22.2 lib/primer/forms/button.rb
primer_view_components-0.18.2 lib/primer/forms/button.rb
openproject-primer_view_components-0.22.1 lib/primer/forms/button.rb
primer_view_components-0.18.1 lib/primer/forms/button.rb
openproject-primer_view_components-0.22.0 lib/primer/forms/button.rb
primer_view_components-0.18.0 lib/primer/forms/button.rb
openproject-primer_view_components-0.21.0 lib/primer/forms/button.rb
primer_view_components-0.17.0 lib/primer/forms/button.rb
openproject-primer_view_components-0.20.0 lib/primer/forms/button.rb
primer_view_components-0.16.1 lib/primer/forms/button.rb
primer_view_components-0.16.0 lib/primer/forms/button.rb
openproject-primer_view_components-0.19.0 lib/primer/forms/button.rb
primer_view_components-0.15.1 lib/primer/forms/button.rb
primer_view_components-0.15.0 lib/primer/forms/button.rb
openproject-primer_view_components-0.18.1 lib/primer/forms/button.rb
openproject-primer_view_components-0.17.1 lib/primer/forms/button.rb
openproject-primer_view_components-0.17.0 lib/primer/forms/button.rb