forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPagination.tsx
More file actions
123 lines (110 loc) · 2.9 KB
/
Pagination.tsx
File metadata and controls
123 lines (110 loc) · 2.9 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
import React, { useState } from 'react';
import MdFirstPage from 'react-icons/lib/md/first-page';
import MdChevronLeft from 'react-icons/lib/md/chevron-left';
import MdChevronRight from 'react-icons/lib/md/chevron-right';
import MdLastPage from 'react-icons/lib/md/last-page';
import { clamp, range } from '../../utils';
import { Navigation, Controls, Button as DefaultButton } from './elements';
type NavButton = React.FC<{
disabled?: boolean;
onClick: () => void;
}>;
interface IPaginationProps {
onChange?: (currentPage: number) => void;
neighbors?: number;
pages?: number;
initial?: number;
Button?: React.ReactType;
First?: NavButton;
Previous?: NavButton;
Next?: NavButton;
Last?: NavButton;
}
export const Pagination: React.FC<IPaginationProps> = ({
onChange = () => {},
neighbors = 2,
pages = 1,
initial = 1,
Button = DefaultButton,
First = props => (
<li>
<Button {...props} aria-label="First">
<MdFirstPage />
</Button>
</li>
),
Previous = props => (
<li>
<Button {...props} aria-label="Previous">
<MdChevronLeft />
</Button>
</li>
),
Next = props => (
<li>
<Button {...props} aria-label="Next">
<MdChevronRight />
</Button>
</li>
),
Last = props => (
<li>
<Button {...props} aria-label="Last">
<MdLastPage />
</Button>
</li>
),
...props
}) => {
const [currentPage, setCurrentPage] = useState(initial);
const fetchPageNumbers = () => {
if (pages <= 0) {
return [];
}
const middle = clamp(currentPage, neighbors + 1, pages - neighbors);
const start = Math.max(1, middle - neighbors);
const end = Math.min(pages, middle + neighbors);
return range(end - start + 1, start);
};
const gotoPage = (page: number) => {
const destination = Math.max(0, Math.min(page, pages));
setCurrentPage(destination);
onChange(destination);
};
const handleClick = (page: number) => () => {
gotoPage(page);
};
const handleFirst = () => {
gotoPage(1);
};
const handlePrevious = () => {
gotoPage(currentPage - 1);
};
const handleNext = () => {
gotoPage(currentPage + 1);
};
const handleLast = () => {
gotoPage(pages);
};
return (
<Navigation {...props}>
<Controls>
<First disabled={currentPage === 1} onClick={handleFirst} />
<Previous disabled={currentPage === 1} onClick={handlePrevious} />
{fetchPageNumbers().map((page: number) => (
<li key={page}>
<Button
active={currentPage === page}
disabled={currentPage === page}
onClick={handleClick(page)}
>
{page}
</Button>
</li>
))}
<Next disabled={currentPage === pages} onClick={handleNext} />
<Last disabled={currentPage === pages} onClick={handleLast} />
</Controls>
</Navigation>
);
};