-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
/
Copy pathPagination.js
257 lines (242 loc) · 7.24 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import composeClasses from '@mui/utils/composeClasses';
import integerPropType from '@mui/utils/integerPropType';
import { getPaginationUtilityClass } from './paginationClasses';
import usePagination from '../usePagination';
import PaginationItem from '../PaginationItem';
import { styled } from '../zero-styled';
import { useDefaultProps } from '../DefaultPropsProvider';
const useUtilityClasses = (ownerState) => {
const { classes, variant } = ownerState;
const slots = {
root: ['root', variant],
ul: ['ul'],
};
return composeClasses(slots, getPaginationUtilityClass, classes);
};
const PaginationRoot = styled('nav', {
name: 'MuiPagination',
slot: 'Root',
overridesResolver: (props, styles) => {
const { ownerState } = props;
return [styles.root, styles[ownerState.variant]];
},
})({});
const PaginationUl = styled('ul', {
name: 'MuiPagination',
slot: 'Ul',
overridesResolver: (props, styles) => styles.ul,
})({
display: 'flex',
flexWrap: 'wrap',
alignItems: 'center',
padding: 0,
margin: 0,
listStyle: 'none',
});
function defaultGetAriaLabel(type, page, selected) {
if (type === 'page') {
return `${selected ? '' : 'Go to '}page ${page}`;
}
return `Go to ${type} page`;
}
const Pagination = React.forwardRef(function Pagination(inProps, ref) {
const props = useDefaultProps({ props: inProps, name: 'MuiPagination' });
const {
boundaryCount = 1,
className,
color = 'standard',
count = 1,
defaultPage = 1,
disabled = false,
getItemAriaLabel = defaultGetAriaLabel,
hideNextButton = false,
hidePrevButton = false,
onChange,
page,
renderItem = (item) => <PaginationItem {...item} />,
shape = 'circular',
showFirstButton = false,
showLastButton = false,
siblingCount = 1,
size = 'medium',
variant = 'text',
...other
} = props;
const { items } = usePagination({ ...props, componentName: 'Pagination' });
const ownerState = {
...props,
boundaryCount,
color,
count,
defaultPage,
disabled,
getItemAriaLabel,
hideNextButton,
hidePrevButton,
renderItem,
shape,
showFirstButton,
showLastButton,
siblingCount,
size,
variant,
};
const classes = useUtilityClasses(ownerState);
return (
<PaginationRoot
aria-label="pagination navigation"
className={clsx(classes.root, className)}
ownerState={ownerState}
ref={ref}
{...other}
>
<PaginationUl className={classes.ul} ownerState={ownerState}>
{items.map((item, index) => (
<li key={index}>
{renderItem({
...item,
color,
'aria-label': getItemAriaLabel(item.type, item.page, item.selected),
shape,
size,
variant,
})}
</li>
))}
</PaginationUl>
</PaginationRoot>
);
});
// @default tags synced with default values from usePagination
Pagination.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
// │ To update them, edit the d.ts file and run `pnpm proptypes`. │
// └─────────────────────────────────────────────────────────────────────┘
/**
* Number of always visible pages at the beginning and end.
* @default 1
*/
boundaryCount: integerPropType,
/**
* Override or extend the styles applied to the component.
*/
classes: PropTypes.object,
/**
* @ignore
*/
className: PropTypes.string,
/**
* The active color.
* It supports both default and custom theme colors, which can be added as shown in the
* [palette customization guide](https://mui.com/material-ui/customization/palette/#custom-colors).
* @default 'standard'
*/
color: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['primary', 'secondary', 'standard']),
PropTypes.string,
]),
/**
* The total number of pages.
* @default 1
*/
count: integerPropType,
/**
* The page selected by default when the component is uncontrolled.
* @default 1
*/
defaultPage: integerPropType,
/**
* If `true`, the component is disabled.
* @default false
*/
disabled: PropTypes.bool,
/**
* Accepts a function which returns a string value that provides a user-friendly name for the current page.
* This is important for screen reader users.
*
* For localization purposes, you can use the provided [translations](https://mui.com/material-ui/guides/localization/).
* @param {string} type The link or button type to format ('page' | 'first' | 'last' | 'next' | 'previous' | 'start-ellipsis' | 'end-ellipsis'). Defaults to 'page'.
* @param {number | null} page The page number to format.
* @param {boolean} selected If true, the current page is selected.
* @returns {string}
*/
getItemAriaLabel: PropTypes.func,
/**
* If `true`, hide the next-page button.
* @default false
*/
hideNextButton: PropTypes.bool,
/**
* If `true`, hide the previous-page button.
* @default false
*/
hidePrevButton: PropTypes.bool,
/**
* Callback fired when the page is changed.
*
* @param {React.ChangeEvent<unknown>} event The event source of the callback.
* @param {number} page The page selected.
*/
onChange: PropTypes.func,
/**
* The current page. Unlike `TablePagination`, which starts numbering from `0`, this pagination starts from `1`.
*/
page: integerPropType,
/**
* Render the item.
* @param {PaginationRenderItemParams} params The props to spread on a PaginationItem.
* @returns {ReactNode}
* @default (item) => <PaginationItem {...item} />
*/
renderItem: PropTypes.func,
/**
* The shape of the pagination items.
* @default 'circular'
*/
shape: PropTypes.oneOf(['circular', 'rounded']),
/**
* If `true`, show the first-page button.
* @default false
*/
showFirstButton: PropTypes.bool,
/**
* If `true`, show the last-page button.
* @default false
*/
showLastButton: PropTypes.bool,
/**
* Number of always visible pages before and after the current page.
* @default 1
*/
siblingCount: integerPropType,
/**
* The size of the component.
* @default 'medium'
*/
size: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['small', 'medium', 'large']),
PropTypes.string,
]),
/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])),
PropTypes.func,
PropTypes.object,
]),
/**
* The variant to use.
* @default 'text'
*/
variant: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['outlined', 'text']),
PropTypes.string,
]),
};
export default Pagination;