-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathPagination.js
More file actions
158 lines (132 loc) · 3.97 KB
/
Pagination.js
File metadata and controls
158 lines (132 loc) · 3.97 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
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
151
152
153
154
155
156
157
158
import _ from 'lodash'
import PropTypes from 'prop-types'
import React from 'react'
import {
createPaginationItems,
customPropTypes,
getUnhandledProps,
useAutoControlledValue,
} from '../../lib'
import Menu from '../../collections/Menu'
import PaginationItem from './PaginationItem'
/**
* A component to render a pagination.
*/
const Pagination = React.forwardRef(function (props, ref) {
const {
'aria-label': ariaLabel,
boundaryRange,
disabled,
ellipsisItem,
siblingRange,
totalPages,
} = props
const [activePage, setActivePage] = useAutoControlledValue({
state: props.activePage,
defaultState: props.defaultActivePage,
initialState: 1,
})
const handleItemClick = (e, { value: nextActivePage }) => {
const prevActivePage = activePage
// Heads up! We need the cast to the "number" type there, as `activePage` can be a string
if (+prevActivePage === +nextActivePage) {
return
}
setActivePage(nextActivePage)
props.onPageChange?.(e, { ...props, activePage: nextActivePage })
}
const handleItemOverrides = (active, type, value) => (predefinedProps) => ({
active,
type,
key: `${type}-${value}`,
onClick: (e, itemProps) => {
predefinedProps.onClick?.(e, itemProps)
if (itemProps.type !== 'ellipsisItem') {
handleItemClick(e, itemProps)
}
},
})
const items = createPaginationItems({
activePage,
boundaryRange,
hideEllipsis: _.isNil(ellipsisItem),
siblingRange,
totalPages,
})
const rest = getUnhandledProps(Pagination, props)
return (
<Menu {...rest} aria-label={ariaLabel} pagination role='navigation' ref={ref}>
{_.map(items, ({ active, type, value }) =>
PaginationItem.create(props[type], {
defaultProps: {
content: value,
disabled,
value,
},
overrideProps: handleItemOverrides(active, type, value),
}),
)}
</Menu>
)
})
Pagination.displayName = 'Pagination'
Pagination.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,
}
Pagination.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,
}
Pagination.Item = PaginationItem
export default Pagination