-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreactrouter.js
63 lines (51 loc) · 1.58 KB
/
reactrouter.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
import React from 'react'
import PropTypes from 'prop-types'
import qs from 'qs'
import ReactstrapPagination from './reactstrap'
const NOOP = () => {}
const DEFAULT_CONTEXT = 2 // number of pages to show before and after current page.
/**
* Returns a link combining existing query parameters `current` with new parameters
* `replace`.
* @param {Object} current - Existing query parameters
* @param {Object} replace - New query parameters
* @return {String} - Relative link with combined parameters
*/
const combineQuery = (current, replace) =>
`?${qs.stringify({ ...current, ...replace })}`
const Pagination = (props, context) => {
let pageParam = 'page'
if (props.pageParamId) {
pageParam = `${pageParam}${props.pageParamId}`
}
const query = (
context.router &&
context.router.history.location
) ? qs.parse(context.router.history.location.search.substr(1)) : {}
const push = context.router ? context.router.history.push : NOOP
const onClick = e => {
e.preventDefault()
push(e.target.getAttribute('href'))
}
return (
<ReactstrapPagination
href={page => combineQuery(query, { [pageParam]: page })}
onClick={onClick}
{...props}
/>
)
}
Pagination.contextTypes = {
router: PropTypes.shape({
history: PropTypes.shape({
push: PropTypes.func,
location: PropTypes.shape({
search: PropTypes.string,
}),
}),
}),
}
Pagination.defaultProps = {
context: DEFAULT_CONTEXT,
}
export default Pagination