MCPcopy
hub / github.com/msgbyte/tianji / PaginationControls

Function PaginationControls

src/client/components/PaginationControls.tsx:25–150  ·  view source on GitHub ↗
({
  page,
  totalPages,
  onPageChange,
  disabled = false,
  className,
  siblingCount = 1,
})

Source from the content-addressed store, hash-verified

23type PaginationItemType = number | 'ellipsis';
24
25export const PaginationControls: React.FC<PaginationControlsProps> = ({
26 page,
27 totalPages,
28 onPageChange,
29 disabled = false,
30 className,
31 siblingCount = 1,
32}) => {
33 const items = useMemo<PaginationItemType[]>(() => {
34 const normalizedTotalPages = Math.max(0, Math.floor(totalPages));
35 const normalizedSiblingCount = Math.max(0, Math.floor(siblingCount));
36
37 if (normalizedTotalPages <= 0) {
38 return [];
39 }
40
41 const rawPages = Array.from(
42 { length: normalizedTotalPages },
43 (_, index) => index + 1
44 ).filter(
45 (pageNumber) =>
46 pageNumber === 1 ||
47 pageNumber === normalizedTotalPages ||
48 Math.abs(pageNumber - page) <= normalizedSiblingCount
49 );
50
51 const parsed: PaginationItemType[] = [];
52
53 rawPages.forEach((pageNumber, index) => {
54 const previousPage = rawPages[index - 1];
55 if (index > 0 && previousPage && pageNumber - previousPage > 1) {
56 parsed.push('ellipsis');
57 }
58
59 parsed.push(pageNumber);
60 });
61
62 return parsed;
63 }, [page, siblingCount, totalPages]);
64
65 if (totalPages <= 1 || items.length === 0) {
66 return null;
67 }
68
69 const isPrevDisabled = disabled || page <= 1;
70 const isNextDisabled = disabled || page >= totalPages;
71
72 const handleChange = (nextPage: number) => {
73 if (nextPage === page || nextPage < 1 || nextPage > totalPages) {
74 return;
75 }
76
77 onPageChange(nextPage);
78 };
79
80 return (
81 <Pagination className={className}>
82 <PaginationContent>

Callers

nothing calls this directly

Calls 2

cnFunction · 0.90
handleChangeFunction · 0.70

Tested by

no test coverage detected