Sha256: 8aa9a50c971e427ed3d935184262a5e40a6b07777ee1ed92d9addf2353d7eb5b

Contents?: true

Size: 1.28 KB

Versions: 5

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

module Primer
  # Base component used by other Primer components.
  #
  # tag(symbol): HTML tag name to be passed to tag.send(tag)
  # class_names(string): CSS class name value to be concatenated with generated Primer CSS classes
  # args(hash): Combination of arguments for classes_from_hash and content_tag
  #
  # Example usage:
  # <%= render Primer::BaseComponent, tag: :a, href: "http://www.google.com", mt: 4 do %>Link<% end %>
  # generates:
  # <a href="http://www.google.com" class="mt-4">Link</a>
  class BaseComponent < Primer::Component
    TEST_SELECTOR_TAG = :test_selector

    def initialize(tag:, classes: nil, **args)
      @tag = tag
      @result = Primer::Classify.call(**args.merge(classes: classes))

      # Filter out Primer keys so they don't get assigned as HTML attributes
      @content_tag_args = add_test_selector(args).except(*Primer::Classify::VALID_KEYS)
    end

    def call
      tag.public_send(
        @tag, content, **@content_tag_args.merge(@result)
      )
    end

    private

    def add_test_selector(args)
      if args.key?(TEST_SELECTOR_TAG) && !Rails.env.production?
        args[:data] ||= {}
        args[:data][TEST_SELECTOR_TAG] = args[TEST_SELECTOR_TAG]
      end

      args.except(TEST_SELECTOR_TAG)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
primer_view_components-0.0.7 app/components/primer/base_component.rb
primer_view_components-0.0.6 app/components/primer/base_component.rb
primer_view_components-0.0.5 app/components/primer/base_component.rb
primer_view_components-0.0.4 app/components/primer/base_component.rb
primer_view_components-0.0.3 app/components/primer/base_component.rb