-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
/
Copy pathTablePagination.js
534 lines (501 loc) · 16.6 KB
/
TablePagination.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import integerPropType from '@mui/utils/integerPropType';
import chainPropTypes from '@mui/utils/chainPropTypes';
import composeClasses from '@mui/utils/composeClasses';
import { styled } from '../zero-styled';
import memoTheme from '../utils/memoTheme';
import { useDefaultProps } from '../DefaultPropsProvider';
import InputBase from '../InputBase';
import MenuItem from '../MenuItem';
import Select from '../Select';
import TableCell from '../TableCell';
import Toolbar from '../Toolbar';
import TablePaginationActions from './TablePaginationActions';
import useId from '../utils/useId';
import tablePaginationClasses, { getTablePaginationUtilityClass } from './tablePaginationClasses';
import useSlot from '../utils/useSlot';
const TablePaginationRoot = styled(TableCell, {
name: 'MuiTablePagination',
slot: 'Root',
overridesResolver: (props, styles) => styles.root,
})(
memoTheme(({ theme }) => ({
overflow: 'auto',
color: (theme.vars || theme).palette.text.primary,
fontSize: theme.typography.pxToRem(14),
// Increase the specificity to override TableCell.
'&:last-child': {
padding: 0,
},
})),
);
const TablePaginationToolbar = styled(Toolbar, {
name: 'MuiTablePagination',
slot: 'Toolbar',
overridesResolver: (props, styles) => ({
[`& .${tablePaginationClasses.actions}`]: styles.actions,
...styles.toolbar,
}),
})(
memoTheme(({ theme }) => ({
minHeight: 52,
paddingRight: 2,
[`${theme.breakpoints.up('xs')} and (orientation: landscape)`]: {
minHeight: 52,
},
[theme.breakpoints.up('sm')]: {
minHeight: 52,
paddingRight: 2,
},
[`& .${tablePaginationClasses.actions}`]: {
flexShrink: 0,
marginLeft: 20,
},
})),
);
const TablePaginationSpacer = styled('div', {
name: 'MuiTablePagination',
slot: 'Spacer',
overridesResolver: (props, styles) => styles.spacer,
})({
flex: '1 1 100%',
});
const TablePaginationSelectLabel = styled('p', {
name: 'MuiTablePagination',
slot: 'SelectLabel',
overridesResolver: (props, styles) => styles.selectLabel,
})(
memoTheme(({ theme }) => ({
...theme.typography.body2,
flexShrink: 0,
})),
);
const TablePaginationSelect = styled(Select, {
name: 'MuiTablePagination',
slot: 'Select',
overridesResolver: (props, styles) => ({
[`& .${tablePaginationClasses.selectIcon}`]: styles.selectIcon,
[`& .${tablePaginationClasses.select}`]: styles.select,
...styles.input,
...styles.selectRoot,
}),
})({
color: 'inherit',
fontSize: 'inherit',
flexShrink: 0,
marginRight: 32,
marginLeft: 8,
[`& .${tablePaginationClasses.select}`]: {
paddingLeft: 8,
paddingRight: 24,
textAlign: 'right',
textAlignLast: 'right', // Align <select> on Chrome.
},
});
const TablePaginationMenuItem = styled(MenuItem, {
name: 'MuiTablePagination',
slot: 'MenuItem',
overridesResolver: (props, styles) => styles.menuItem,
})({});
const TablePaginationDisplayedRows = styled('p', {
name: 'MuiTablePagination',
slot: 'DisplayedRows',
overridesResolver: (props, styles) => styles.displayedRows,
})(
memoTheme(({ theme }) => ({
...theme.typography.body2,
flexShrink: 0,
})),
);
function defaultLabelDisplayedRows({ from, to, count }) {
return `${from}–${to} of ${count !== -1 ? count : `more than ${to}`}`;
}
function defaultGetAriaLabel(type) {
return `Go to ${type} page`;
}
const useUtilityClasses = (ownerState) => {
const { classes } = ownerState;
const slots = {
root: ['root'],
toolbar: ['toolbar'],
spacer: ['spacer'],
selectLabel: ['selectLabel'],
select: ['select'],
input: ['input'],
selectIcon: ['selectIcon'],
menuItem: ['menuItem'],
displayedRows: ['displayedRows'],
actions: ['actions'],
};
return composeClasses(slots, getTablePaginationUtilityClass, classes);
};
/**
* A `TableCell` based component for placing inside `TableFooter` for pagination.
*/
const TablePagination = React.forwardRef(function TablePagination(inProps, ref) {
const props = useDefaultProps({ props: inProps, name: 'MuiTablePagination' });
const {
ActionsComponent = TablePaginationActions,
backIconButtonProps,
colSpan: colSpanProp,
component = TableCell,
count,
disabled = false,
getItemAriaLabel = defaultGetAriaLabel,
labelDisplayedRows = defaultLabelDisplayedRows,
labelRowsPerPage = 'Rows per page:',
nextIconButtonProps,
onPageChange,
onRowsPerPageChange,
page,
rowsPerPage,
rowsPerPageOptions = [10, 25, 50, 100],
SelectProps = {},
showFirstButton = false,
showLastButton = false,
slotProps = {},
slots = {},
...other
} = props;
const ownerState = props;
const classes = useUtilityClasses(ownerState);
const selectProps = slotProps?.select ?? SelectProps;
const MenuItemComponent = selectProps.native ? 'option' : TablePaginationMenuItem;
let colSpan;
if (component === TableCell || component === 'td') {
colSpan = colSpanProp || 1000; // col-span over everything
}
const selectId = useId(selectProps.id);
const labelId = useId(selectProps.labelId);
const getLabelDisplayedRowsTo = () => {
if (count === -1) {
return (page + 1) * rowsPerPage;
}
return rowsPerPage === -1 ? count : Math.min(count, (page + 1) * rowsPerPage);
};
const externalForwardedProps = { slots, slotProps };
const [RootSlot, rootSlotProps] = useSlot('root', {
ref,
className: classes.root,
elementType: TablePaginationRoot,
externalForwardedProps: {
...externalForwardedProps,
component,
...other,
},
ownerState,
additionalProps: {
colSpan,
},
});
const [ToolbarSlot, toolbarSlotProps] = useSlot('toolbar', {
className: classes.toolbar,
elementType: TablePaginationToolbar,
externalForwardedProps,
ownerState,
});
const [SpacerSlot, spacerSlotProps] = useSlot('spacer', {
className: classes.spacer,
elementType: TablePaginationSpacer,
externalForwardedProps,
ownerState,
});
const [SelectLabelSlot, selectLabelSlotProps] = useSlot('selectLabel', {
className: classes.selectLabel,
elementType: TablePaginationSelectLabel,
externalForwardedProps,
ownerState,
additionalProps: {
id: labelId,
},
});
const [SelectSlot, selectSlotProps] = useSlot('select', {
className: classes.select,
elementType: TablePaginationSelect,
externalForwardedProps,
ownerState,
});
const [MenuItemSlot, menuItemSlotProps] = useSlot('menuItem', {
className: classes.menuItem,
elementType: MenuItemComponent,
externalForwardedProps,
ownerState,
});
const [DisplayedRows, displayedRowsProps] = useSlot('displayedRows', {
className: classes.displayedRows,
elementType: TablePaginationDisplayedRows,
externalForwardedProps,
ownerState,
});
return (
<RootSlot {...rootSlotProps}>
<ToolbarSlot {...toolbarSlotProps}>
<SpacerSlot {...spacerSlotProps} />
{rowsPerPageOptions.length > 1 && (
<SelectLabelSlot {...selectLabelSlotProps}>{labelRowsPerPage}</SelectLabelSlot>
)}
{rowsPerPageOptions.length > 1 && (
<SelectSlot
variant="standard"
{...(!selectProps.variant && { input: <InputBase /> })}
value={rowsPerPage}
onChange={onRowsPerPageChange}
id={selectId}
labelId={labelId}
{...selectProps}
classes={{
...selectProps.classes,
// TODO v5 remove `classes.input`
root: clsx(classes.input, classes.selectRoot, (selectProps.classes || {}).root),
select: clsx(classes.select, (selectProps.classes || {}).select),
// TODO v5 remove `selectIcon`
icon: clsx(classes.selectIcon, (selectProps.classes || {}).icon),
}}
disabled={disabled}
{...selectSlotProps}
>
{rowsPerPageOptions.map((rowsPerPageOption) => (
<MenuItemSlot
{...menuItemSlotProps}
key={rowsPerPageOption.label ? rowsPerPageOption.label : rowsPerPageOption}
value={rowsPerPageOption.value ? rowsPerPageOption.value : rowsPerPageOption}
>
{rowsPerPageOption.label ? rowsPerPageOption.label : rowsPerPageOption}
</MenuItemSlot>
))}
</SelectSlot>
)}
<DisplayedRows {...displayedRowsProps}>
{labelDisplayedRows({
from: count === 0 ? 0 : page * rowsPerPage + 1,
to: getLabelDisplayedRowsTo(),
count: count === -1 ? -1 : count,
page,
})}
</DisplayedRows>
<ActionsComponent
className={classes.actions}
backIconButtonProps={backIconButtonProps}
count={count}
nextIconButtonProps={nextIconButtonProps}
onPageChange={onPageChange}
page={page}
rowsPerPage={rowsPerPage}
showFirstButton={showFirstButton}
showLastButton={showLastButton}
slotProps={slotProps.actions}
slots={slots.actions}
getItemAriaLabel={getItemAriaLabel}
disabled={disabled}
/>
</ToolbarSlot>
</RootSlot>
);
});
TablePagination.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`. │
// └─────────────────────────────────────────────────────────────────────┘
/**
* The component used for displaying the actions.
* Either a string to use a HTML element or a component.
* @default TablePaginationActions
*/
ActionsComponent: PropTypes.elementType,
/**
* Props applied to the back arrow [`IconButton`](https://mui.com/material-ui/api/icon-button/) component.
*
* This prop is an alias for `slotProps.actions.previousButton` and will be overriden by it if both are used.
* @deprecated Use `slotProps.actions.previousButton` instead.
*/
backIconButtonProps: PropTypes.object,
/**
* Override or extend the styles applied to the component.
*/
classes: PropTypes.object,
/**
* @ignore
*/
colSpan: PropTypes.number,
/**
* The component used for the root node.
* Either a string to use a HTML element or a component.
*/
component: PropTypes.elementType,
/**
* The total number of rows.
*
* To enable server side pagination for an unknown number of items, provide -1.
*/
count: integerPropType.isRequired,
/**
* 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 ('first' | 'last' | 'next' | 'previous').
* @returns {string}
* @default function defaultGetAriaLabel(type) {
* return `Go to ${type} page`;
* }
*/
getItemAriaLabel: PropTypes.func,
/**
* Customize the displayed rows label. Invoked with a `{ from, to, count, page }`
* object.
*
* For localization purposes, you can use the provided [translations](https://mui.com/material-ui/guides/localization/).
* @default function defaultLabelDisplayedRows({ from, to, count }) {
* return `${from}–${to} of ${count !== -1 ? count : `more than ${to}`}`;
* }
*/
labelDisplayedRows: PropTypes.func,
/**
* Customize the rows per page label.
*
* For localization purposes, you can use the provided [translations](https://mui.com/material-ui/guides/localization/).
* @default 'Rows per page:'
*/
labelRowsPerPage: PropTypes.node,
/**
* Props applied to the next arrow [`IconButton`](https://mui.com/material-ui/api/icon-button/) element.
*
* This prop is an alias for `slotProps.actions.nextButton` and will be overriden by it if both are used.
* @deprecated Use `slotProps.actions.nextButton` instead.
*/
nextIconButtonProps: PropTypes.object,
/**
* Callback fired when the page is changed.
*
* @param {React.MouseEvent<HTMLButtonElement> | null} event The event source of the callback.
* @param {number} page The page selected.
*/
onPageChange: PropTypes.func.isRequired,
/**
* Callback fired when the number of rows per page is changed.
*
* @param {React.ChangeEvent<HTMLTextAreaElement | HTMLInputElement>} event The event source of the callback.
*/
onRowsPerPageChange: PropTypes.func,
/**
* The zero-based index of the current page.
*/
page: chainPropTypes(integerPropType.isRequired, (props) => {
const { count, page, rowsPerPage } = props;
if (count === -1) {
return null;
}
const newLastPage = Math.max(0, Math.ceil(count / rowsPerPage) - 1);
if (page < 0 || page > newLastPage) {
return new Error(
'MUI: The page prop of a TablePagination is out of range ' +
`(0 to ${newLastPage}, but page is ${page}).`,
);
}
return null;
}),
/**
* The number of rows per page.
*
* Set -1 to display all the rows.
*/
rowsPerPage: integerPropType.isRequired,
/**
* Customizes the options of the rows per page select field. If less than two options are
* available, no select field will be displayed.
* Use -1 for the value with a custom label to show all the rows.
* @default [10, 25, 50, 100]
*/
rowsPerPageOptions: PropTypes.arrayOf(
PropTypes.oneOfType([
PropTypes.number,
PropTypes.shape({
label: PropTypes.string.isRequired,
value: PropTypes.number.isRequired,
}),
]).isRequired,
),
/**
* Props applied to the rows per page [`Select`](https://mui.com/material-ui/api/select/) element.
*
* This prop is an alias for `slotProps.select` and will be overriden by it if both are used.
* @deprecated Use `slotProps.select` instead.
*
* @default {}
*/
SelectProps: PropTypes.object,
/**
* If `true`, show the first-page button.
* @default false
*/
showFirstButton: PropTypes.bool,
/**
* If `true`, show the last-page button.
* @default false
*/
showLastButton: PropTypes.bool,
/**
* The props used for each slot inside.
* @default {}
*/
slotProps: PropTypes.shape({
actions: PropTypes.shape({
firstButton: PropTypes.object,
firstButtonIcon: PropTypes.object,
lastButton: PropTypes.object,
lastButtonIcon: PropTypes.object,
nextButton: PropTypes.object,
nextButtonIcon: PropTypes.object,
previousButton: PropTypes.object,
previousButtonIcon: PropTypes.object,
}),
displayedRows: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
menuItem: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
root: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
select: PropTypes.object,
selectLabel: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
spacer: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
toolbar: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
}),
/**
* The components used for each slot inside.
* @default {}
*/
slots: PropTypes.shape({
actions: PropTypes.shape({
firstButton: PropTypes.elementType,
firstButtonIcon: PropTypes.elementType,
lastButton: PropTypes.elementType,
lastButtonIcon: PropTypes.elementType,
nextButton: PropTypes.elementType,
nextButtonIcon: PropTypes.elementType,
previousButton: PropTypes.elementType,
previousButtonIcon: PropTypes.elementType,
}),
displayedRows: PropTypes.elementType,
menuItem: PropTypes.elementType,
root: PropTypes.elementType,
select: PropTypes.elementType,
selectLabel: PropTypes.elementType,
spacer: PropTypes.elementType,
toolbar: PropTypes.elementType,
}),
/**
* 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,
]),
};
export default TablePagination;