Class: NitroKit::Pagination

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

Defined Under Namespace

Classes: Item

Constant Summary collapse

ITEM_KINDS =
%i[previous page ellipsis next].freeze

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(label: I18n.t("nitro_kit.pagination.label"), pagy: nil, page_url: nil, id: nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ Pagination

Returns a new instance of Pagination.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/components/nitro_kit/pagination.rb', line 12

def initialize(
  label: I18n.t("nitro_kit.pagination.label"),
  pagy: nil,
  page_url: nil,
  id: nil,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil
)
  validate_label!(label, name: "label")
  validate_aria!(aria, reserved: %w[label])
  validate_pagy!(pagy, page_url:)
  @pagy = pagy
  @page_url = page_url
  @items = []

  super(
    component: :pagination,
    attributes: { id: },
    html:,
    aria: aria.merge(label:),
    data:,
    desperately_need_a_class:
  )
end

Instance Method Details

#ellipsis(label: I18n.t("nitro_kit.pagination.ellipsis")) ⇒ Object



138
139
140
141
142
# File 'app/components/nitro_kit/pagination.rb', line 138

def ellipsis(label: I18n.t("nitro_kit.pagination.ellipsis"))
  validate_label!(label, name: "ellipsis label")

  append(Item.new(kind: :ellipsis, button: nil, current_page: nil, content: nil, current: false, label:))
end

#next(text = UNSET, href: nil, icon: "arrow-right", disabled: false, id: nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil, &content) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'app/components/nitro_kit/pagination.rb', line 144

def next(
  text = UNSET,
  href: nil,
  icon: "arrow-right",
  disabled: false,
  id: nil,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil,
  &content
)
  text = default_text(text, I18n.t("nitro_kit.pagination.next"), content)
  validate_boolean!(:disabled, disabled)
  append_navigation_item(
    :next,
    text:,
    href:,
    icon_end: icon,
    disabled: disabled || missing_href?(href),
    default_label: I18n.t("nitro_kit.pagination.next_page"),
    id:,
    html:,
    aria:,
    data:,
    desperately_need_a_class:,
    &content
  )
end

#page(text = nil, href: nil, current: false, id: nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil, &content) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/components/nitro_kit/pagination.rb', line 85

def page(
  text = nil,
  href: nil,
  current: false,
  id: nil,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil,
  &content
)
  validate_boolean!(:current, current)
  if !text.nil? && content
    raise ArgumentError, "Pagination page accepts text or a block, not both"
  end
  validate_content!(text, content, name: "page label")
  if missing_href?(href) && !current
    raise ArgumentError, "Pagination page href is required unless the page is current"
  end
  validate_aria!(aria, reserved: %w[current disabled])

  item_aria = current ? aria.merge(current: "page") : aria
  button = nil
  current_page = nil

  if current && missing_href?(href)
    current_page = CurrentPage.new(
      text:,
      attributes: slot_attributes(
        :current,
        attributes: { id: }.compact,
        html:,
        aria: item_aria,
        data:,
        desperately_need_a_class:
      )
    )
  else
    button = pagination_button(
      text,
      href:,
      disabled: false,
      id:,
      html:,
      aria: item_aria,
      data:,
      desperately_need_a_class:
    )
  end

  append(Item.new(kind: :page, button:, current_page:, content:, current:, label: nil))
end

#prev(text = UNSET, href: nil, icon: "arrow-left", disabled: false, id: nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil, &content) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/components/nitro_kit/pagination.rb', line 55

def prev(
  text = UNSET,
  href: nil,
  icon: "arrow-left",
  disabled: false,
  id: nil,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil,
  &content
)
  text = default_text(text, I18n.t("nitro_kit.pagination.previous"), content)
  validate_boolean!(:disabled, disabled)
  append_navigation_item(
    :previous,
    text:,
    href:,
    icon:,
    disabled: disabled || missing_href?(href),
    default_label: I18n.t("nitro_kit.pagination.previous_page"),
    id:,
    html:,
    aria:,
    data:,
    desperately_need_a_class:,
    &content
  )
end

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

Yields:

  • (_self)

Yield Parameters:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/components/nitro_kit/pagination.rb', line 39

def view_template(&content)
  if @pagy && content
    raise ArgumentError, "Pagination accepts a Pagy object or a block, not both"
  end

  append_pagy_items if @pagy
  yield self if content
  validate_collection!

  nav(**root_attributes) do
    ol(**slot_attributes(:list)) do
      @items.each { |item| render_item(item) }
    end
  end
end