Skip to content

Commit cf6805a

Browse files
committed
Simplify the pagination component to just accept a prev and next link
1 parent a7f4cad commit cf6805a

File tree

8 files changed

+48
-70
lines changed

8 files changed

+48
-70
lines changed

admin/app/components/solidus_admin/products/index/component.html.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717

1818
<%= render @table_component.new(
1919
page: @page,
20+
footer: render(@pagination_component.new(
21+
prev_link: @page.first? ? nil : solidus_admin.products_path(page: @page.number - 1),
22+
next_link: @page.last? ? nil : solidus_admin.products_path(page: @page.number + 1)
23+
)),
2024
batch_actions: [
2125
{
2226
display_name: t('.batch_actions.delete'),

admin/app/components/solidus_admin/products/index/component.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ def initialize(
55
page:,
66
badge_component: component('ui/badge'),
77
table_component: component('ui/table'),
8+
pagination_component: component('ui/table/pagination'),
89
button_component: component("ui/button")
910
)
1011
@page = page
1112

1213
@badge_component = badge_component
1314
@table_component = table_component
1415
@button_component = button_component
16+
@pagination_component = pagination_component
1517
end
1618

1719
def image_column(product)

admin/app/components/solidus_admin/ui/table/component.html.erb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@
7878
<% end %>
7979
</tbody>
8080

81-
<%= render_table_footer %>
81+
<% if @footer.present? %>
82+
<tfoot>
83+
<tr>
84+
<td colspan="<%= @columns.size %>" class="py-4">
85+
<div class="flex justify-center">
86+
<%= @footer %>
87+
</div>
88+
</td>
89+
</tr>
90+
</tfoot>
91+
<% end %>
92+
8293
</table>
8394
</div>

admin/app/components/solidus_admin/ui/table/component.rb

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
class SolidusAdmin::UI::Table::Component < SolidusAdmin::BaseComponent
44
# @param page [GearedPagination::Page] The pagination page object.
5-
# @param path [Proc] A callable object that generates the path for pagination links.
65
#
76
# @param columns [Array<Hash>] The array of column definitions.
87
# @option columns [Symbol|Proc|#to_s] :header The column header.
@@ -15,28 +14,27 @@ class SolidusAdmin::UI::Table::Component < SolidusAdmin::BaseComponent
1514
# @option batch_actions [String] :action The batch action path.
1615
# @option batch_actions [String] :method The batch action HTTP method for the provided path.
1716
#
18-
# @param pagination_component [Class] The pagination component class (default: component("ui/table/pagination")).
17+
# @param footer [String] The content for the footer, e.g. pagination links.
18+
#
1919
# @param checkbox_componnent [Class] The checkbox component class (default: component("ui/forms/checkbox")).
2020
# @param button_component [Class] The button component class (default: component("ui/button")).
2121
# @param tab_component [Class] The tab component class (default: component("ui/tab")).
2222
def initialize(
2323
page:,
24-
path: nil,
2524
columns: [],
2625
batch_actions: [],
27-
pagination_component: component("ui/table/pagination"),
26+
footer: nil,
2827
checkbox_componnent: component("ui/forms/checkbox"),
2928
button_component: component("ui/button"),
3029
tab_component: component("ui/tab")
3130
)
3231
@page = page
33-
@path = path
3432
@columns = columns.map { Column.new(**_1) }
3533
@batch_actions = batch_actions.map { BatchAction.new(**_1) }
3634
@model_class = page.records.model
3735
@rows = page.records
36+
@footer = footer
3837

39-
@pagination_component = pagination_component
4038
@checkbox_componnent = checkbox_componnent
4139
@button_component = button_component
4240
@tab_component = tab_component
@@ -131,24 +129,6 @@ def render_data_cell(cell, data)
131129
content_tag(:td, content_tag(:div, cell, class: "flex items-center gap-1.5"), class: "py-2 px-4 h-10 vertical-align-middle leading-none")
132130
end
133131

134-
def render_table_footer
135-
if @pagination_component
136-
tag.tfoot do
137-
tag.tr do
138-
tag.td(colspan: @columns.size, class: "py-4") do
139-
tag.div(class: "flex justify-center") do
140-
render_pagination_component
141-
end
142-
end
143-
end
144-
end
145-
end
146-
end
147-
148-
def render_pagination_component
149-
@pagination_component.new(page: @page, path: @path).render_in(self)
150-
end
151-
152132
Column = Struct.new(:header, :data, :class_name, keyword_init: true)
153133
BatchAction = Struct.new(:display_name, :icon, :action, :method, keyword_init: true) # rubocop:disable Lint/StructNewOverride
154134
private_constant :Column, :BatchAction

admin/app/components/solidus_admin/ui/table/pagination/component.html.erb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22
<nav aria-label="pagination">
33
<ul class="flex items-center">
44
<li>
5-
<% if @page.first? %>
5+
<% if @prev_link.blank? %>
66
<span class="<%= link_classes(rounded: :left) %>", "aria-label"=<%= t(".prev") %>, "aria-disabled"=true>
77
<%= icon_tag("arrow-left-s-line", class: "fill-current text-grey-400 w-5 h-5") %>
88
</span>
99
<% else %>
10-
<%= link_to @path.call(@page.number - 1), class: link_classes(rounded: :left), "aria-label" => t(".prev") do %>
10+
<%= link_to @prev_link, class: link_classes(rounded: :left), "aria-label" => t(".prev") do %>
1111
<%= icon_tag("arrow-left-s-line", class: "w-5 h-5") %>
1212
<% end %>
1313
<% end %>
1414
</li>
1515

1616
<li>
17-
<% if @page.last? %>
17+
<% if @next_link.blank? %>
1818
<span class="<%= link_classes(rounded: :right) %>", "aria-label"=<%= t(".next") %>, "aria-disabled"=true>
1919
<%= icon_tag("arrow-right-s-line", class: "fill-current text-grey-400 w-5 h-5") %>
2020
</span>
2121
<% else %>
22-
<%= link_to @path.call(@page.next_param), class: link_classes(rounded: :right), "aria-label" => t(".next") do %>
22+
<%= link_to @next_link, class: link_classes(rounded: :right), "aria-label" => t(".next") do %>
2323
<%= icon_tag("arrow-right-s-line", class: "w-5 h-5") %>
2424
<% end %>
2525
<% end %>

admin/app/components/solidus_admin/ui/table/pagination/component.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
# frozen_string_literal: true
22

33
class SolidusAdmin::UI::Table::Pagination::Component < SolidusAdmin::BaseComponent
4-
# @param page [GearedPagination::Page] The Geared Pagination page object
5-
# @param path [Proc] (optional) A callable object that generates the path,
6-
# e.g. ->(page_number){ products_path(page: page_number) }
7-
def initialize(page:, path: nil)
8-
@page = page
9-
@path = path || default_path
4+
# @param prev_link [String] The link to the previous page.
5+
# @param next_link [String] The link to the next page.
6+
def initialize(prev_link: nil, next_link: nil)
7+
@prev_link = prev_link
8+
@next_link = next_link
109
end
1110

12-
def default_path
13-
model_name = @page.records.model.model_name.param_key
14-
lambda { |page_number| send("#{model_name.pluralize}_path", page: page_number) }
11+
def render?
12+
@prev_link.present? || @next_link.present?
1513
end
1614

1715
def link_classes(rounded: nil)

admin/spec/components/previews/solidus_admin/ui/table/pagination/component_preview.rb

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,15 @@ class SolidusAdmin::UI::Table::Pagination::ComponentPreview < ViewComponent::Pre
55
include SolidusAdmin::Preview
66

77
def overview
8-
render_with_template(
9-
locals: {
10-
page: page_proc,
11-
path: path_proc
12-
}
13-
)
8+
render_with_template
149
end
1510

16-
# @param left toggle
17-
# @param right toggle
18-
def playground(left: false, right: false)
11+
# @param prev_link
12+
# @param next_link
13+
def playground(prev_link: '#1', next_link: '#2')
1914
render current_component.new(
20-
page: page_proc.call(left, right),
21-
path: path_proc
15+
prev_link: prev_link.presence,
16+
next_link: next_link.presence,
2217
)
2318
end
24-
25-
private
26-
27-
def page_proc
28-
lambda { |left, right|
29-
Struct.new(:number, :next_param, :first?, :last?).new(1, '#', !left, !right)
30-
}
31-
end
32-
33-
def path_proc
34-
->(_page_number) { "#" }
35-
end
3619
end

admin/spec/components/previews/solidus_admin/ui/table/pagination/component_preview/overview.html.erb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<table>
2-
<% [
3-
{ title: 'Default', left: true, right: true },
4-
{ title: 'Left', left: true, right: false },
5-
{ title: 'Right', left: false, right: true },
6-
{ title: 'Disabled', left: false, right: false }
7-
].each do |config| %>
2+
<% {
3+
'Default': ['#1', '#2'],
4+
'Left': ['#1', nil],
5+
'Right': [nil, '#2'],
6+
'Disabled': [nil, nil],
7+
}.each do |title, (left, right)| %>
88
<tr>
9-
<td class="font-bold px-3 py-1"><%= config[:title] %></td>
9+
<td class="font-bold px-3 py-1"><%= title %></td>
1010
<td class="px-3 py-1 text-center">
1111
<%= render current_component.new(
12-
page: page.call(config[:left], config[:right]),
13-
path: path
12+
prev_link: (left && "#1"),
13+
next_link: (right && "#2"),
1414
) %>
1515
</td>
1616
</tr>

0 commit comments

Comments
 (0)