Class: NitroKit::StatGrid

Inherits:
Component
  • Object
show all
Defined in:
app/components/nitro_kit/stat_grid.rb

Defined Under Namespace

Classes: Stat

Constant Summary collapse

DEFAULT_COLUMNS =
"1 sm:2 lg:3"

Constants inherited from Component

Component::ADDITIVE_DATA_ATTRIBUTES, Component::COMPONENT_OWNED_DATA_ATTRIBUTES, Component::FORBIDDEN_ATTRIBUTES, Component::RESERVED_DATA_ATTRIBUTES

Instance Method Summary collapse

Constructor Details

#initialize(cols: DEFAULT_COLUMNS, gap: 4, id: nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ StatGrid

Returns a new instance of StatGrid.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/components/nitro_kit/stat_grid.rb', line 8

def initialize(
  cols: DEFAULT_COLUMNS,
  gap: 4,
  id: nil,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil
)
  @stats = []
  @grid = Grid.new(cols:, gap:)

  super(
    component: :stat_grid,
    attributes: { id: }.compact,
    html:,
    aria:,
    data:,
    desperately_need_a_class:
  )
end

Instance Method Details

#stat(key:, label:, value:, detail: nil) ⇒ Object

Raises:

  • (ArgumentError)


43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/components/nitro_kit/stat_grid.rb', line 43

def stat(key:, label:, value:, detail: nil)
  key = normalize_identity(key, name: "stat key")
  raise ArgumentError, "StatGrid stat keys must be unique: #{key.inspect}" if @stats.any? { |stat| stat.key == key }

  @stats << Stat.new(
    key:,
    label: present_text!(:label, label),
    value: present_text!(:value, value),
    detail: detail.nil? ? nil : present_text!(:detail, detail)
  )
  nil
end

#view_template {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/components/nitro_kit/stat_grid.rb', line 30

def view_template
  yield self if block_given?
  raise ArgumentError, "StatGrid requires at least one stat" if @stats.empty?

  div(**root_attributes) do
    render_in_slot(@grid, :grid) do
      html_dl(**slot_attributes(:list)) do
        @stats.each { |stat| render_stat(stat) }
      end
    end
  end
end