Class: Ariadne::CounterComponent

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

Overview

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

Constant Summary collapse

DEFAULT_CLASSES =
"ariadne-inline-flex ariadne-items-center ariadne-p-1 ariadne-border ariadne-border-transparent ariadne-rounded-full ariadne-shadow-sm focus:ariadne-outline-none focus:ariadne-ring-2 focus:ariadne-ring-offset-2"

Constants inherited from Component

Ariadne::Component::BASE_BODY_CLASSES, Ariadne::Component::BASE_HTML_CLASSES, Ariadne::Component::BASE_MAIN_CLASSES, Ariadne::Component::BASE_WRAPPER_CLASSES, Ariadne::Component::INVALID_ARIA_LABEL_TAGS

Constants included from ActionViewExtensions::FormHelper

ActionViewExtensions::FormHelper::DEFAULT_FORM_CLASSES

Constants included from Status::Dsl

Status::Dsl::STATUSES

Constants included from ViewHelper

ViewHelper::HELPERS

Constants included from FetchOrFallbackHelper

FetchOrFallbackHelper::INTEGER_TYPES, FetchOrFallbackHelper::InvalidValueError, FetchOrFallbackHelper::TRUE_OR_FALSE

Instance Method Summary collapse

Methods included from ActionViewExtensions::FormHelper

#ariadne_form_with

Methods included from ClassNameHelper

#class_names

Methods included from LoggerHelper

#logger, #silence_deprecations?, #silence_warnings?

Methods included from FetchOrFallbackHelper

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

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