/
Pagination.js
150 lines (125 loc) · 4.06 KB
/
Pagination.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import _ from 'lodash'
import PropTypes from 'prop-types'
import React from 'react'
import {
ModernAutoControlledComponent as Component,
createPaginationItems,
customPropTypes,
getUnhandledProps,
} from '../../lib'
import Menu from '../../collections/Menu'
import PaginationItem from './PaginationItem'
/**
* A component to render a pagination.
*/
export default class Pagination extends Component {
static propTypes = {
/** A pagination item can have an aria label. */
'aria-label': PropTypes.string,
/** Initial activePage value. */
defaultActivePage: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
/** Index of the currently active page. */
activePage: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
/** Number of always visible pages at the beginning and end. */
boundaryRange: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
/** A pagination can be disabled. */
disabled: PropTypes.bool,
/** A shorthand for PaginationItem. */
ellipsisItem: customPropTypes.itemShorthand,
/** A shorthand for PaginationItem. */
firstItem: customPropTypes.itemShorthand,
/** A shorthand for PaginationItem. */
lastItem: customPropTypes.itemShorthand,
/** A shorthand for PaginationItem. */
nextItem: customPropTypes.itemShorthand,
/** A shorthand for PaginationItem. */
pageItem: customPropTypes.itemShorthand,
/** A shorthand for PaginationItem. */
prevItem: customPropTypes.itemShorthand,
/**
* Called on change of an active page.
*
* @param {SyntheticEvent} event - React's original SyntheticEvent.
* @param {object} data - All props.
*/
onPageChange: PropTypes.func,
/** Number of always visible pages before and after the current one. */
siblingRange: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
/** Total number of pages. */
totalPages: PropTypes.oneOfType([PropTypes.number, PropTypes.string]).isRequired,
}
static autoControlledProps = ['activePage']
static defaultProps = {
'aria-label': 'Pagination Navigation',
boundaryRange: 1,
ellipsisItem: '...',
firstItem: {
'aria-label': 'First item',
content: '«',
},
lastItem: {
'aria-label': 'Last item',
content: '»',
},
nextItem: {
'aria-label': 'Next item',
content: '⟩',
},
pageItem: {},
prevItem: {
'aria-label': 'Previous item',
content: '⟨',
},
siblingRange: 1,
}
static Item = PaginationItem
handleItemClick = (e, { value: nextActivePage }) => {
const { activePage: prevActivePage } = this.state
// Heads up! We need the cast to the "number" type there, as `activePage` can be a string
if (+prevActivePage === +nextActivePage) return
this.setState({ activePage: nextActivePage })
_.invoke(this.props, 'onPageChange', e, { ...this.props, activePage: nextActivePage })
}
handleItemOverrides = (active, type, value) => (predefinedProps) => ({
active,
type,
key: `${type}-${value}`,
onClick: (e, itemProps) => {
_.invoke(predefinedProps, 'onClick', e, itemProps)
if (itemProps.type !== 'ellipsisItem') this.handleItemClick(e, itemProps)
},
})
render() {
const {
'aria-label': ariaLabel,
boundaryRange,
disabled,
ellipsisItem,
siblingRange,
totalPages,
} = this.props
const { activePage } = this.state
const items = createPaginationItems({
activePage,
boundaryRange,
hideEllipsis: _.isNil(ellipsisItem),
siblingRange,
totalPages,
})
const rest = getUnhandledProps(Pagination, this.props)
return (
<Menu {...rest} aria-label={ariaLabel} pagination role='navigation'>
{_.map(items, ({ active, type, value }) =>
PaginationItem.create(this.props[type], {
defaultProps: {
content: value,
disabled,
value,
},
overrideProps: this.handleItemOverrides(active, type, value),
}),
)}
</Menu>
)
}
}