Sha256: 147826355590343cb13cfa58fc274ad455ec7dd4b68d85f480779ced382715ce

Contents?: true

Size: 1.83 KB

Versions: 43

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

module Primer
  module Alpha
    class ActionList
      # Utility component for wrapping ActionLists or individual ActionList::Items in forms.
      class FormWrapper < Primer::Component
        DEFAULT_HTTP_METHOD = :get
        HTTP_METHOD_OPTIONS = [
          DEFAULT_HTTP_METHOD,
          :post,
          :patch,
          :put,
          :delete,
          :head
        ].freeze

        def initialize(list:, action: nil, **form_arguments)
          @list = list
          @form_arguments = form_arguments

          @action = action
          @http_method = extract_http_method(@form_arguments)

          name = @form_arguments.delete(:name)
          value = @form_arguments.delete(:value) || name
          inputs = @form_arguments.delete(:inputs) || []

          # For the older version of this component that only allowed you to
          # specify a single input
          if inputs.empty?
            inputs << {
              name: name,
              value: value,
              **(@form_arguments.delete(:input_arguments) || {})
            }
          end

          @inputs = inputs.map do |input_data|
            input_data = input_data.dup
            input_data[:type] ||= :hidden
            input_data[:data] ||= {}
            input_data[:data][:list_item_input] = true
            input_data
          end
        end

        def get?
          @http_method == :get
        end

        def form_required?
          @action && !get?
        end

        def render_inputs?
          @inputs.present?
        end

        private

        def extract_http_method(args)
          if (http_method = args.delete(:method))
            HTTP_METHOD_OPTIONS.include?(http_method) ? http_method : DEFAULT_HTTP_METHOD
          else
            DEFAULT_HTTP_METHOD
          end
        end
      end
    end
  end
end

Version data entries

43 entries across 43 versions & 2 rubygems

Version Path
primer_view_components-0.18.1 app/components/primer/alpha/action_list/form_wrapper.rb
openproject-primer_view_components-0.22.0 app/components/primer/alpha/action_list/form_wrapper.rb
primer_view_components-0.18.0 app/components/primer/alpha/action_list/form_wrapper.rb
openproject-primer_view_components-0.21.0 app/components/primer/alpha/action_list/form_wrapper.rb
primer_view_components-0.17.0 app/components/primer/alpha/action_list/form_wrapper.rb
openproject-primer_view_components-0.20.0 app/components/primer/alpha/action_list/form_wrapper.rb
primer_view_components-0.16.1 app/components/primer/alpha/action_list/form_wrapper.rb
primer_view_components-0.16.0 app/components/primer/alpha/action_list/form_wrapper.rb
openproject-primer_view_components-0.19.0 app/components/primer/alpha/action_list/form_wrapper.rb
primer_view_components-0.15.1 app/components/primer/alpha/action_list/form_wrapper.rb
primer_view_components-0.15.0 app/components/primer/alpha/action_list/form_wrapper.rb
openproject-primer_view_components-0.18.1 app/components/primer/alpha/action_list/form_wrapper.rb
openproject-primer_view_components-0.17.1 app/components/primer/alpha/action_list/form_wrapper.rb
openproject-primer_view_components-0.17.0 app/components/primer/alpha/action_list/form_wrapper.rb
primer_view_components-0.14.0 app/components/primer/alpha/action_list/form_wrapper.rb
openproject-primer_view_components-0.16.0 app/components/primer/alpha/action_list/form_wrapper.rb
openproject-primer_view_components-0.15.0 app/components/primer/alpha/action_list/form_wrapper.rb
openproject-primer_view_components-0.14.0 app/components/primer/alpha/action_list/form_wrapper.rb
openproject-primer_view_components-0.13.1 app/components/primer/alpha/action_list/form_wrapper.rb
primer_view_components-0.13.2 app/components/primer/alpha/action_list/form_wrapper.rb