Sha256: 834f5279a959bf3c286b0d7b16ce30c22d2868ec80f4db7ecbf3771747832d02

Contents?: true

Size: 1.52 KB

Versions: 67

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

module Primer
  module Forms
    # :nodoc:
    class AutoComplete < BaseComponent
      ARGUMENT_TYPES = %i(keyreq key).freeze

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

      def initialize(input:)
        @input = input
        @input.merge_input_arguments!(text_field_attributes.deep_symbolize_keys)
      end

      def self.auto_complete_argument_names
        @auto_complete_argument_names ||=
          Primer::Beta::AutoComplete.instance_method(:initialize)
            .parameters
            .filter_map { |(type, param_name)| next param_name if ARGUMENT_TYPES.include?(type) }
      end

      private

      def all_input_arguments
        @all_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

      def auto_complete_arguments
        all_args = all_input_arguments
        all_args
          .slice(*self.class.auto_complete_argument_names)
          .merge(
            input_name: all_args[:name],
            input_id: all_args[:id],
            label_text: @input.label,
            list_id: "#{all_args[:id]}-list"
          )
      end

      def input_arguments
        all_input_arguments.except(*self.class.auto_complete_argument_names, :id, :name)
      end

      def text_field_attributes
        builder.text_field_attributes(@input.name).except("size", "value")
      end
    end
  end
end

Version data entries

67 entries across 67 versions & 2 rubygems

Version Path
openproject-primer_view_components-0.43.1 lib/primer/forms/auto_complete.rb
openproject-primer_view_components-0.43.0 lib/primer/forms/auto_complete.rb
primer_view_components-0.32.0 lib/primer/forms/auto_complete.rb
openproject-primer_view_components-0.42.0 lib/primer/forms/auto_complete.rb
primer_view_components-0.31.0 lib/primer/forms/auto_complete.rb
openproject-primer_view_components-0.41.1 lib/primer/forms/auto_complete.rb
openproject-primer_view_components-0.41.0 lib/primer/forms/auto_complete.rb
openproject-primer_view_components-0.40.0 lib/primer/forms/auto_complete.rb
primer_view_components-0.30.0 lib/primer/forms/auto_complete.rb
openproject-primer_view_components-0.39.1 lib/primer/forms/auto_complete.rb
openproject-primer_view_components-0.39.0 lib/primer/forms/auto_complete.rb
openproject-primer_view_components-0.38.0 lib/primer/forms/auto_complete.rb
primer_view_components-0.29.0 lib/primer/forms/auto_complete.rb
primer_view_components-0.28.0 lib/primer/forms/auto_complete.rb
openproject-primer_view_components-0.37.1 lib/primer/forms/auto_complete.rb
openproject-primer_view_components-0.37.0 lib/primer/forms/auto_complete.rb
openproject-primer_view_components-0.36.2 lib/primer/forms/auto_complete.rb
openproject-primer_view_components-0.36.1 lib/primer/forms/auto_complete.rb
openproject-primer_view_components-0.36.0 lib/primer/forms/auto_complete.rb
primer_view_components-0.27.0 lib/primer/forms/auto_complete.rb