Sha256: e5c272e3adf72f095c69609e6cf6dd6ba35ae9029f824659b8c709a1b113cbbb

Contents?: true

Size: 1.46 KB

Versions: 24

Compression:

Stored size: 1.46 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

          @input_arguments = {
            type: :hidden,
            name: name,
            value: value,
            data: { list_item_input: true },
            **(@form_arguments.delete(:input_arguments) || {})
          }
        end

        def get?
          @http_method == :get
        end

        def form_required?
          @action && !get?
        end

        def render_input?
          @input_arguments[:name].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

24 entries across 24 versions & 2 rubygems

Version Path
primer_view_components-0.12.0 app/components/primer/alpha/action_list/form_wrapper.rb
primer_view_components-0.11.0 app/components/primer/alpha/action_list/form_wrapper.rb
openproject-primer_view_components-0.12.1 app/components/primer/alpha/action_list/form_wrapper.rb
primer_view_components-0.10.0 app/components/primer/alpha/action_list/form_wrapper.rb
openproject-primer_view_components-0.11.0 app/components/primer/alpha/action_list/form_wrapper.rb
openproject-primer_view_components-0.10.0 app/components/primer/alpha/action_list/form_wrapper.rb
primer_view_components-0.9.0 app/components/primer/alpha/action_list/form_wrapper.rb
openproject-primer_view_components-0.9.1 app/components/primer/alpha/action_list/form_wrapper.rb
openproject-primer_view_components-0.9.0 app/components/primer/alpha/action_list/form_wrapper.rb
openproject-primer_view_components-0.8.0 app/components/primer/alpha/action_list/form_wrapper.rb
primer_view_components-0.8.0 app/components/primer/alpha/action_list/form_wrapper.rb
primer_view_components-0.7.0 app/components/primer/alpha/action_list/form_wrapper.rb
openproject-primer_view_components-0.7.0 app/components/primer/alpha/action_list/form_wrapper.rb
openproject-primer_view_components-0.6.3 app/components/primer/alpha/action_list/form_wrapper.rb
primer_view_components-0.6.0 app/components/primer/alpha/action_list/form_wrapper.rb
primer_view_components-0.5.1 app/components/primer/alpha/action_list/form_wrapper.rb
primer_view_components-0.5.0 app/components/primer/alpha/action_list/form_wrapper.rb
primer_view_components-0.4.0 app/components/primer/alpha/action_list/form_wrapper.rb
primer_view_components-0.3.1 app/components/primer/alpha/action_list/form_wrapper.rb
primer_view_components-0.3.0 app/components/primer/alpha/action_list/form_wrapper.rb