Class: Ariadne::CounterComponent

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

Overview

Use ‘Counter` to add a count to navigational elements and buttons.

Constant Summary collapse

DEFAULT_CLASSES =
"inline-flex items-center p-1 border border-transparent rounded-full shadow-sm text-white bg-gray-600 hover:bg-gray-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-gray-500"

Constants inherited from Component

Ariadne::Component::INVALID_ARIA_LABEL_TAGS

Constants included from Status::Dsl

Status::Dsl::STATUSES

Constants included from ViewHelper

ViewHelper::HELPERS

Constants included from TestSelectorHelper

TestSelectorHelper::TEST_SELECTOR_TAG

Constants included from FetchOrFallbackHelper

FetchOrFallbackHelper::InvalidValueError, FetchOrFallbackHelper::TRUE_OR_FALSE

Instance Method Summary collapse

Methods included from LoggerHelper

#logger, #silence_deprecations?, #silence_warnings?

Methods included from JoinStyleArgumentsHelper

#join_style_arguments

Methods included from TestSelectorHelper

#add_test_selector

Methods included from FetchOrFallbackHelper

#check_incoming_attribute, #check_incoming_tag, #check_incoming_value, #fetch_or_raise, #fetch_or_raise_boolean

Methods included from ClassNameHelper

#class_names

Constructor Details

#initialize(tag: :span, count: 0, limit: 9_000, hide_if_zero: false, text: "", round: false, classes: "", attributes: {}) ⇒ CounterComponent

Returns a new instance of CounterComponent.

Examples:

Default

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

Schemes

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

Parameters:

  • tag (Symbol, String) (defaults to: :span)

    The rendered tag name

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

    <%= link_to_classes_docs %>

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

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

  • limit (Integer, nil) (defaults to: 9_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 rounding logic to value.

  • attributes (Hash) (defaults to: {})

    <%= link_to_attributes_docs %>



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/components/ariadne/counter_component.rb', line 28

def initialize(
  tag: :span,
  count: 0,
  limit: 9_000,
  hide_if_zero: false,
  text: "",
  round: false,
  classes: "",
  attributes: {}
)
  @count = count
  @limit = limit
  @hide_if_zero = hide_if_zero
  @text = text
  @round = round
  @attributes = attributes

  @has_limit = !@limit.nil?

  @tag = check_incoming_tag(:span, tag)

  @attributes[:title] = title

  @classes = class_names(
    DEFAULT_CLASSES,
    classes
  )
  @attributes[:hidden] = true if count == 0 && hide_if_zero
end

Instance Method Details

#callObject



58
59
60
# File 'app/components/ariadne/counter_component.rb', line 58

def call
  render(Ariadne::BaseComponent.new(tag: @tag, classes: @classes, attributes: @attributes)) { value }
end