-
-
Notifications
You must be signed in to change notification settings - Fork 424
Expand file tree
/
Copy pathPaginationControls.vue
More file actions
234 lines (203 loc) · 7.74 KB
/
PaginationControls.vue
File metadata and controls
234 lines (203 loc) · 7.74 KB
1
2
3
4
5
6
7
8
9
10
11
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
84
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
137
138
139
140
141
142
143
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<script setup lang="ts">
import type { PageSize, PaginationMode, ViewMode } from '#shared/types/preferences'
import { PAGE_SIZE_OPTIONS } from '#shared/types/preferences'
const ALL_PAGES_VISIBLE_TRESHOLD = 7
const props = defineProps<{
totalItems: number
/** When in table view, force pagination mode (no infinite scroll for tables) */
viewMode?: ViewMode
}>()
const mode = defineModel<PaginationMode>('mode', { required: true })
const pageSize = defineModel<PageSize>('pageSize', { required: true })
const currentPage = defineModel<number>('currentPage', { required: true })
const pageSizeSelectValue = computed(() => String(pageSize.value))
// Whether we should show pagination controls (table view always uses pagination)
const shouldShowControls = computed(() => props.viewMode === 'table' || mode.value === 'paginated')
// Table view forces pagination mode, otherwise use the provided mode
const effectiveMode = computed<PaginationMode>(() =>
shouldShowControls.value ? 'paginated' : 'infinite',
)
const totalPages = computed(() => Math.ceil(props.totalItems / (pageSize.value as number)))
// Whether to show the mode toggle (hidden in table view since table always uses pagination)
const showModeToggle = computed(() => props.viewMode !== 'table')
const startItem = computed(() => {
if (props.totalItems === 0) return 0
return (currentPage.value - 1) * (pageSize.value as number) + 1
})
const endItem = computed(() => {
return Math.min(currentPage.value * (pageSize.value as number), props.totalItems)
})
const canGoPrev = computed(() => currentPage.value > 1)
const canGoNext = computed(() => currentPage.value < totalPages.value)
function goToPage(page: number) {
if (page >= 1 && page <= totalPages.value) {
currentPage.value = page
}
}
function goPrev() {
if (canGoPrev.value) {
currentPage.value = currentPage.value - 1
}
}
function goNext() {
if (canGoNext.value) {
currentPage.value = currentPage.value + 1
}
}
// Generate visible page numbers with ellipsis
const visiblePages = computed(() => {
const total = totalPages.value
const current = currentPage.value
const pages: (number | 'ellipsis')[] = []
if (total <= ALL_PAGES_VISIBLE_TRESHOLD) {
// Show all pages
for (let i = 1; i <= total; i++) {
pages.push(i)
}
} else {
// Always show first page
pages.push(1)
if (current > 3) {
pages.push('ellipsis')
}
// Show pages around current
const start = Math.max(2, current - 1)
const end = Math.min(total - 1, current + 1)
for (let i = start; i <= end; i++) {
pages.push(i)
}
if (current < total - 2) {
pages.push('ellipsis')
}
// Always show last page
if (total > 1) {
pages.push(total)
}
}
return pages
})
// disable last page button to prevent TOO MANY REQUESTS error
function isPageButtonDisabled(page: number): boolean {
return totalPages.value > ALL_PAGES_VISIBLE_TRESHOLD && page > currentPage.value + 2
}
function handlePageSizeChange(event: Event) {
const target = event.target as HTMLSelectElement
const value = target.value
const newSize = Number(value) as PageSize
pageSize.value = newSize
// Reset to page 1 when changing page size
currentPage.value = 1
}
</script>
<template>
<!-- Only show when in paginated mode (table view or explicit paginated mode) -->
<div
v-if="shouldShowControls"
class="flex flex-wrap items-center justify-between gap-4 py-4 mt-2"
>
<!-- Left: Mode toggle and page size -->
<div class="flex items-center gap-4">
<!-- Pagination mode toggle (hidden in table view - tables always use pagination) -->
<div
v-if="showModeToggle"
class="inline-flex rounded-md border border-border p-0.5 bg-bg-subtle"
role="group"
:aria-label="$t('filters.pagination.mode_label')"
>
<button
type="button"
class="px-2.5 py-1 text-xs font-mono rounded-sm transition-colors duration-200 focus-visible:ring-2 focus-visible:ring-fg focus-visible:ring-offset-1"
:class="mode === 'infinite' ? 'bg-bg-muted text-fg' : 'text-fg-muted hover:text-fg'"
:aria-pressed="mode === 'infinite'"
@click="mode = 'infinite'"
>
{{ $t('filters.pagination.infinite') }}
</button>
<button
type="button"
class="px-2.5 py-1 text-xs font-mono rounded-sm transition-colors duration-200 focus-visible:ring-2 focus-visible:ring-fg focus-visible:ring-offset-1"
:class="mode === 'paginated' ? 'bg-bg-muted text-fg' : 'text-fg-muted hover:text-fg'"
:aria-pressed="mode === 'paginated'"
@click="mode = 'paginated'"
>
{{ $t('filters.pagination.paginated') }}
</button>
</div>
<!-- Page size (shown when paginated or table view) -->
<div v-if="effectiveMode === 'paginated'" class="relative shrink-0">
<SelectField
:label="$t('filters.pagination.items_per_page')"
hidden-label
id="page-size"
v-model="pageSizeSelectValue"
@change="handlePageSizeChange"
:items="
PAGE_SIZE_OPTIONS.map(size => ({
label: $t('filters.pagination.per_page', { count: size }),
value: String(size),
}))
"
/>
</div>
</div>
<!-- Right: Page info and navigation (paginated mode only) -->
<div v-if="effectiveMode === 'paginated'" class="flex items-center gap-4">
<!-- Showing X-Y of Z -->
<span class="text-sm font-mono text-fg-muted">
{{
$t('filters.pagination.showing', {
start: $n(startItem),
end: $n(endItem),
total: $n(totalItems),
})
}}
</span>
<!-- Page navigation -->
<nav
v-if="totalPages > 1"
class="flex items-center gap-1"
:aria-label="$t('filters.pagination.nav_label')"
>
<!-- Previous button -->
<button
type="button"
class="p-1.5 rounded hover:bg-bg-muted text-fg-muted hover:text-fg disabled:opacity-40 disabled:cursor-not-allowed transition-colors duration-200 focus-visible:ring-2 focus-visible:ring-fg focus-visible:ring-offset-1"
:disabled="!canGoPrev"
:aria-label="$t('filters.pagination.previous')"
@click="goPrev"
>
<span class="i-lucide:chevron-left block rtl-flip w-4 h-4" aria-hidden="true" />
</button>
<!-- Page numbers -->
<template v-for="(page, idx) in visiblePages" :key="idx">
<span v-if="page === 'ellipsis'" class="px-2 text-fg-subtle font-mono">…</span>
<button
v-else
type="button"
:disabled="isPageButtonDisabled(page)"
class="min-w-[32px] h-8 px-2 font-mono text-sm rounded transition-colors duration-200 focus-visible:ring-2 focus-visible:ring-fg focus-visible:ring-offset-1"
:class="
page === currentPage
? 'bg-fg text-bg'
: 'text-fg-muted hover:text-fg hover:bg-bg-muted'
"
:aria-current="page === currentPage ? 'page' : undefined"
@click="goToPage(page)"
>
{{ page }}
</button>
</template>
<!-- Next button -->
<button
type="button"
class="p-1.5 rounded hover:bg-bg-muted text-fg-muted hover:text-fg disabled:opacity-40 disabled:cursor-not-allowed transition-colors duration-200 focus-visible:ring-2 focus-visible:ring-fg focus-visible:ring-offset-1"
:disabled="!canGoNext"
:aria-label="$t('filters.pagination.next')"
@click="goNext"
>
<span class="i-lucide:chevron-right block rtl-flip w-4 h-4" aria-hidden="true" />
</button>
</nav>
</div>
</div>
</template>