Class: Primer::CounterComponent

Inherits:
Component
  • Object
show all
Defined in:
app/components/primer/counter_component.rb

Overview

Use Primer::CounterComponent to add a count to navigational elements and buttons.

Constant Summary collapse

DEFAULT_SCHEME =
:default
SCHEME_MAPPINGS =
{
  DEFAULT_SCHEME => "Counter",
  :gray => "Counter Counter--gray",
  :light_gray => "Counter Counter--gray-light"
}.freeze

Constants inherited from Component

Primer::Component::STATUSES

Constants included from FetchOrFallbackHelper

FetchOrFallbackHelper::InvalidValueError

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ViewHelper

#primer

Methods included from JoinStyleArgumentsHelper

#join_style_arguments

Methods included from FetchOrFallbackHelper

#fetch_or_fallback, #fetch_or_fallback_boolean

Methods included from ClassNameHelper

#class_names

Constructor Details

#initialize(count: 0, scheme: DEFAULT_SCHEME, limit: 5_000, hide_if_zero: false, text: "", round: false, **system_arguments) ⇒ CounterComponent

Returns a new instance of CounterComponent.

Examples:

Default

<%= render(Primer::CounterComponent.new(count: 25)) %>

Parameters:

  • count (Integer, Float::INFINITY, nil) (defaults to: 0)

    The number to be displayed (e.x. # of issues, pull requests)

  • scheme (Symbol) (defaults to: DEFAULT_SCHEME)

    Color scheme. One of ‘SCHEME_MAPPINGS.keys`.

  • limit (Integer, nil) (defaults to: 5_000)

    Maximum value to display. Pass ‘nil` for no limit. (e.x. if `count` == 6,000 and `limit` == 5000, counter will display “5,000+”)

  • hide_if_zero (Boolean) (defaults to: false)

    If true, a ‘hidden` attribute is added to the counter if `count` is zero.

  • text (String) (defaults to: "")

    Text to display instead of count.

  • round (Boolean) (defaults to: false)

    Whether to apply our standard rounding logic to value.

  • system_arguments (Hash)

    <%= link_to_system_arguments_docs %>



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/components/primer/counter_component.rb', line 24

def initialize(
  count: 0,
  scheme: DEFAULT_SCHEME,
  limit: 5_000,
  hide_if_zero: false,
  text: "",
  round: false,
  **system_arguments
)
  @count = count
  @limit = limit
  @hide_if_zero = hide_if_zero
  @text = text
  @round = round
  @system_arguments = system_arguments

  @has_limit = !@limit.nil?
  @system_arguments[:title] = title
  @system_arguments[:tag] = :span
  @system_arguments[:classes] = class_names(
    @system_arguments[:classes],
    SCHEME_MAPPINGS[fetch_or_fallback(SCHEME_MAPPINGS.keys, scheme, DEFAULT_SCHEME)]
  )
  @system_arguments[:hidden] = true if count == 0 && hide_if_zero # rubocop:disable Style/NumericPredicate
end

Class Method Details

.statusObject



54
55
56
# File 'app/components/primer/counter_component.rb', line 54

def self.status
  Primer::Component::STATUSES[:beta]
end

Instance Method Details

#callObject



50
51
52
# File 'app/components/primer/counter_component.rb', line 50

def call
  render(Primer::BaseComponent.new(**@system_arguments)) { value }
end