feat(client): add Pagination component and integrate with Table and pages
- add Pagination component with prev/next, ellipsis page range, and optional page-size selector - add Pagination and Table pagination stories (WithPagination, WithPaginationAndSizeSelector) - extend Table with optional pageSize and pageSizeOptions props for built-in client-side pagination - add Timestamp column to Table story fixtures - enable pagination on all four pages with pageSize=10 and size selector - add pagination i18n keys to en.json
This commit is contained in:
1 parent
b4d576c6e9
commit
077cd6ad5f
11 files changed
+358
-35
No files matched your search
@@ -0,0 +1,105 @@
|
||||
import { ChevronLeft, ChevronRight } from 'lucide-react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import styles from './Pagination.module.css';
|
||||
|
||||
export interface PaginationProps {
|
||||
/** 1-indexed current page */
|
||||
page: number;
|
||||
pageSize: number;
|
||||
total: number;
|
||||
onPageChange: (page: number) => void;
|
||||
onPageSizeChange?: (size: number) => void;
|
||||
pageSizeOptions?: number[];
|
||||
}
|
||||
|
||||
function buildPages(current: number, last: number): (number | '…')[] {
|
||||
if (last <= 7) return Array.from({ length: last }, (_, i) => i + 1);
|
||||
|
||||
const pages: (number | '…')[] = [1];
|
||||
|
||||
if (current > 3) pages.push('…');
|
||||
|
||||
const start = Math.max(2, current - 1);
|
||||
const end = Math.min(last - 1, current + 1);
|
||||
for (let i = start; i <= end; i++) pages.push(i);
|
||||
|
||||
if (current < last - 2) pages.push('…');
|
||||
|
||||
pages.push(last);
|
||||
return pages;
|
||||
}
|
||||
|
||||
export function Pagination({
|
||||
page,
|
||||
pageSize,
|
||||
total,
|
||||
onPageChange,
|
||||
onPageSizeChange,
|
||||
pageSizeOptions = [10, 25, 50],
|
||||
}: PaginationProps) {
|
||||
const { t } = useTranslation();
|
||||
const lastPage = Math.max(1, Math.ceil(total / pageSize));
|
||||
const from = Math.min(total, (page - 1) * pageSize + 1);
|
||||
const to = Math.min(total, page * pageSize);
|
||||
|
||||
return (
|
||||
<div className={styles.root}>
|
||||
<span className={styles.info}>{t('pagination.range', { from, to, total })}</span>
|
||||
|
||||
<div className={styles.pages}>
|
||||
<button
|
||||
className={styles.arrow}
|
||||
onClick={() => onPageChange(page - 1)}
|
||||
disabled={page <= 1}
|
||||
aria-label={t('pagination.prev')}
|
||||
>
|
||||
<ChevronLeft size={14} />
|
||||
</button>
|
||||
|
||||
{buildPages(page, lastPage).map((p, i) =>
|
||||
p === '…' ? (
|
||||
<span key={`ellipsis-${i}`} className={styles.ellipsis}>
|
||||
…
|
||||
</span>
|
||||
) : (
|
||||
<button
|
||||
key={p}
|
||||
className={`${styles.pageBtn} ${p === page ? styles.active : ''}`}
|
||||
onClick={() => onPageChange(p)}
|
||||
aria-current={p === page ? 'page' : undefined}
|
||||
>
|
||||
{p}
|
||||
</button>
|
||||
),
|
||||
)}
|
||||
|
||||
<button
|
||||
className={styles.arrow}
|
||||
onClick={() => onPageChange(page + 1)}
|
||||
disabled={page >= lastPage}
|
||||
aria-label={t('pagination.next')}
|
||||
>
|
||||
<ChevronRight size={14} />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{onPageSizeChange && (
|
||||
<select
|
||||
className={styles.sizeSelect}
|
||||
value={pageSize}
|
||||
onChange={(e) => {
|
||||
onPageSizeChange(Number(e.target.value));
|
||||
onPageChange(1);
|
||||
}}
|
||||
aria-label={t('pagination.page_size')}
|
||||
>
|
||||
{pageSizeOptions.map((s) => (
|
||||
<option key={s} value={s}>
|
||||
{t('pagination.per_page', { count: s })}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user