-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
90 lines (75 loc) · 1.88 KB
/
index.js
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
// ___JSX_IMPORT___
import { omit } from './util'
const DEFAULT_CONTEXT = 2 // number of pages to show before and after current page.
const pagination = (page, last, context) => {
const left = page - context
const right = page + context + 1
const range = [1]
const rangeWithSpacers = []
for (let i = page - context; i <= page + context; i += 1) {
if (i >= left && i < right && i < last && i > 1) {
range.push(i)
}
}
range.push(last)
if (range.length === 2 && range[0] === range[1]) {
return []
}
let l
range.forEach(i => {
if (l) {
if (i - l === 2) {
rangeWithSpacers.push(l + 1)
} else if (i - l !== 1) {
rangeWithSpacers.push(`_${l + 1}`)
}
}
rangeWithSpacers.push(i)
l = i
})
return rangeWithSpacers
}
const Pagination = props => {
const {
page,
perPage,
count,
context,
previous,
next,
item,
spacer,
} = props
const rest = omit(props, [
'page',
'perPage',
'pageParamId',
'count',
'context',
'previous',
'next',
'item',
'spacer',
'wrapper',
'children',
])
const last = Math.ceil(count / perPage)
const pages = pagination(page, last, context)
return pages.length ? (
<props.wrapper {...rest}>
{(page !== 1) && previous(page - 1)}
{pages.map(_page => (
(typeof _page === 'number') ? (
item(_page, _page === page)
) : (
spacer(_page)
)
))}
{(page !== last) && next(page + 1)}
</props.wrapper>
) : null
}
Pagination.defaultProps = {
context: DEFAULT_CONTEXT,
}
export default Pagination