UNPKG

15.3 kBJavaScriptView Raw
1'use client';
2
3var _InputBase;
4import * as React from 'react';
5import PropTypes from 'prop-types';
6import clsx from 'clsx';
7import integerPropType from '@mui/utils/integerPropType';
8import chainPropTypes from '@mui/utils/chainPropTypes';
9import composeClasses from '@mui/utils/composeClasses';
10import isHostComponent from "../utils/isHostComponent.js";
11import { styled } from "../zero-styled/index.js";
12import memoTheme from "../utils/memoTheme.js";
13import { useDefaultProps } from "../DefaultPropsProvider/index.js";
14import InputBase from "../InputBase/index.js";
15import MenuItem from "../MenuItem/index.js";
16import Select from "../Select/index.js";
17import TableCell from "../TableCell/index.js";
18import Toolbar from "../Toolbar/index.js";
19import TablePaginationActions from "./TablePaginationActions.js";
20import useId from "../utils/useId.js";
21import tablePaginationClasses, { getTablePaginationUtilityClass } from "./tablePaginationClasses.js";
22import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
23import { createElement as _createElement } from "react";
24const TablePaginationRoot = styled(TableCell, {
25 name: 'MuiTablePagination',
26 slot: 'Root',
27 overridesResolver: (props, styles) => styles.root
28})(memoTheme(({
29 theme
30}) => ({
31 overflow: 'auto',
32 color: (theme.vars || theme).palette.text.primary,
33 fontSize: theme.typography.pxToRem(14),
34 // Increase the specificity to override TableCell.
35 '&:last-child': {
36 padding: 0
37 }
38})));
39const TablePaginationToolbar = styled(Toolbar, {
40 name: 'MuiTablePagination',
41 slot: 'Toolbar',
42 overridesResolver: (props, styles) => ({
43 [`& .${tablePaginationClasses.actions}`]: styles.actions,
44 ...styles.toolbar
45 })
46})(memoTheme(({
47 theme
48}) => ({
49 minHeight: 52,
50 paddingRight: 2,
51 [`${theme.breakpoints.up('xs')} and (orientation: landscape)`]: {
52 minHeight: 52
53 },
54 [theme.breakpoints.up('sm')]: {
55 minHeight: 52,
56 paddingRight: 2
57 },
58 [`& .${tablePaginationClasses.actions}`]: {
59 flexShrink: 0,
60 marginLeft: 20
61 }
62})));
63const TablePaginationSpacer = styled('div', {
64 name: 'MuiTablePagination',
65 slot: 'Spacer',
66 overridesResolver: (props, styles) => styles.spacer
67})({
68 flex: '1 1 100%'
69});
70const TablePaginationSelectLabel = styled('p', {
71 name: 'MuiTablePagination',
72 slot: 'SelectLabel',
73 overridesResolver: (props, styles) => styles.selectLabel
74})(memoTheme(({
75 theme
76}) => ({
77 ...theme.typography.body2,
78 flexShrink: 0
79})));
80const TablePaginationSelect = styled(Select, {
81 name: 'MuiTablePagination',
82 slot: 'Select',
83 overridesResolver: (props, styles) => ({
84 [`& .${tablePaginationClasses.selectIcon}`]: styles.selectIcon,
85 [`& .${tablePaginationClasses.select}`]: styles.select,
86 ...styles.input,
87 ...styles.selectRoot
88 })
89})({
90 color: 'inherit',
91 fontSize: 'inherit',
92 flexShrink: 0,
93 marginRight: 32,
94 marginLeft: 8,
95 [`& .${tablePaginationClasses.select}`]: {
96 paddingLeft: 8,
97 paddingRight: 24,
98 textAlign: 'right',
99 textAlignLast: 'right' // Align <select> on Chrome.
100 }
101});
102const TablePaginationMenuItem = styled(MenuItem, {
103 name: 'MuiTablePagination',
104 slot: 'MenuItem',
105 overridesResolver: (props, styles) => styles.menuItem
106})({});
107const TablePaginationDisplayedRows = styled('p', {
108 name: 'MuiTablePagination',
109 slot: 'DisplayedRows',
110 overridesResolver: (props, styles) => styles.displayedRows
111})(memoTheme(({
112 theme
113}) => ({
114 ...theme.typography.body2,
115 flexShrink: 0
116})));
117function defaultLabelDisplayedRows({
118 from,
119 to,
120 count
121}) {
122 return `${from}${to} of ${count !== -1 ? count : `more than ${to}`}`;
123}
124function defaultGetAriaLabel(type) {
125 return `Go to ${type} page`;
126}
127const useUtilityClasses = ownerState => {
128 const {
129 classes
130 } = ownerState;
131 const slots = {
132 root: ['root'],
133 toolbar: ['toolbar'],
134 spacer: ['spacer'],
135 selectLabel: ['selectLabel'],
136 select: ['select'],
137 input: ['input'],
138 selectIcon: ['selectIcon'],
139 menuItem: ['menuItem'],
140 displayedRows: ['displayedRows'],
141 actions: ['actions']
142 };
143 return composeClasses(slots, getTablePaginationUtilityClass, classes);
144};
145
146/**
147 * A `TableCell` based component for placing inside `TableFooter` for pagination.
148 */
149const TablePagination = /*#__PURE__*/React.forwardRef(function TablePagination(inProps, ref) {
150 const props = useDefaultProps({
151 props: inProps,
152 name: 'MuiTablePagination'
153 });
154 const {
155 ActionsComponent = TablePaginationActions,
156 backIconButtonProps,
157 className,
158 colSpan: colSpanProp,
159 component = TableCell,
160 count,
161 disabled = false,
162 getItemAriaLabel = defaultGetAriaLabel,
163 labelDisplayedRows = defaultLabelDisplayedRows,
164 labelRowsPerPage = 'Rows per page:',
165 nextIconButtonProps,
166 onPageChange,
167 onRowsPerPageChange,
168 page,
169 rowsPerPage,
170 rowsPerPageOptions = [10, 25, 50, 100],
171 SelectProps = {},
172 showFirstButton = false,
173 showLastButton = false,
174 slotProps = {},
175 slots = {},
176 ...other
177 } = props;
178 const ownerState = props;
179 const classes = useUtilityClasses(ownerState);
180 const selectProps = slotProps?.select ?? SelectProps;
181 const MenuItemComponent = selectProps.native ? 'option' : TablePaginationMenuItem;
182 let colSpan;
183 if (component === TableCell || component === 'td') {
184 colSpan = colSpanProp || 1000; // col-span over everything
185 }
186 const selectId = useId(selectProps.id);
187 const labelId = useId(selectProps.labelId);
188 const getLabelDisplayedRowsTo = () => {
189 if (count === -1) {
190 return (page + 1) * rowsPerPage;
191 }
192 return rowsPerPage === -1 ? count : Math.min(count, (page + 1) * rowsPerPage);
193 };
194 return /*#__PURE__*/_jsx(TablePaginationRoot, {
195 colSpan: colSpan,
196 ref: ref,
197 as: component,
198 ownerState: ownerState,
199 className: clsx(classes.root, className),
200 ...other,
201 children: /*#__PURE__*/_jsxs(TablePaginationToolbar, {
202 className: classes.toolbar,
203 children: [/*#__PURE__*/_jsx(TablePaginationSpacer, {
204 className: classes.spacer
205 }), rowsPerPageOptions.length > 1 && /*#__PURE__*/_jsx(TablePaginationSelectLabel, {
206 className: classes.selectLabel,
207 id: labelId,
208 children: labelRowsPerPage
209 }), rowsPerPageOptions.length > 1 && /*#__PURE__*/_jsx(TablePaginationSelect, {
210 variant: "standard",
211 ...(!selectProps.variant && {
212 input: _InputBase || (_InputBase = /*#__PURE__*/_jsx(InputBase, {}))
213 }),
214 value: rowsPerPage,
215 onChange: onRowsPerPageChange,
216 id: selectId,
217 labelId: labelId,
218 ...selectProps,
219 classes: {
220 ...selectProps.classes,
221 // TODO v5 remove `classes.input`
222 root: clsx(classes.input, classes.selectRoot, (selectProps.classes || {}).root),
223 select: clsx(classes.select, (selectProps.classes || {}).select),
224 // TODO v5 remove `selectIcon`
225 icon: clsx(classes.selectIcon, (selectProps.classes || {}).icon)
226 },
227 disabled: disabled,
228 children: rowsPerPageOptions.map(rowsPerPageOption => /*#__PURE__*/_createElement(MenuItemComponent, {
229 ...(!isHostComponent(MenuItemComponent) && {
230 ownerState
231 }),
232 className: classes.menuItem,
233 key: rowsPerPageOption.label ? rowsPerPageOption.label : rowsPerPageOption,
234 value: rowsPerPageOption.value ? rowsPerPageOption.value : rowsPerPageOption
235 }, rowsPerPageOption.label ? rowsPerPageOption.label : rowsPerPageOption))
236 }), /*#__PURE__*/_jsx(TablePaginationDisplayedRows, {
237 className: classes.displayedRows,
238 children: labelDisplayedRows({
239 from: count === 0 ? 0 : page * rowsPerPage + 1,
240 to: getLabelDisplayedRowsTo(),
241 count: count === -1 ? -1 : count,
242 page
243 })
244 }), /*#__PURE__*/_jsx(ActionsComponent, {
245 className: classes.actions,
246 backIconButtonProps: backIconButtonProps,
247 count: count,
248 nextIconButtonProps: nextIconButtonProps,
249 onPageChange: onPageChange,
250 page: page,
251 rowsPerPage: rowsPerPage,
252 showFirstButton: showFirstButton,
253 showLastButton: showLastButton,
254 slotProps: slotProps.actions,
255 slots: slots.actions,
256 getItemAriaLabel: getItemAriaLabel,
257 disabled: disabled
258 })]
259 })
260 });
261});
262process.env.NODE_ENV !== "production" ? TablePagination.propTypes /* remove-proptypes */ = {
263 // ┌────────────────────────────── Warning ──────────────────────────────┐
264 // │ These PropTypes are generated from the TypeScript type definitions. │
265 // │ To update them, edit the d.ts file and run `pnpm proptypes`. │
266 // └─────────────────────────────────────────────────────────────────────┘
267 /**
268 * The component used for displaying the actions.
269 * Either a string to use a HTML element or a component.
270 * @default TablePaginationActions
271 */
272 ActionsComponent: PropTypes.elementType,
273 /**
274 * Props applied to the back arrow [`IconButton`](https://mui.com/material-ui/api/icon-button/) component.
275 *
276 * This prop is an alias for `slotProps.actions.previousButton` and will be overriden by it if both are used.
277 * @deprecated Use `slotProps.actions.previousButton` instead.
278 */
279 backIconButtonProps: PropTypes.object,
280 /**
281 * Override or extend the styles applied to the component.
282 */
283 classes: PropTypes.object,
284 /**
285 * @ignore
286 */
287 className: PropTypes.string,
288 /**
289 * @ignore
290 */
291 colSpan: PropTypes.number,
292 /**
293 * The component used for the root node.
294 * Either a string to use a HTML element or a component.
295 */
296 component: PropTypes.elementType,
297 /**
298 * The total number of rows.
299 *
300 * To enable server side pagination for an unknown number of items, provide -1.
301 */
302 count: integerPropType.isRequired,
303 /**
304 * If `true`, the component is disabled.
305 * @default false
306 */
307 disabled: PropTypes.bool,
308 /**
309 * Accepts a function which returns a string value that provides a user-friendly name for the current page.
310 * This is important for screen reader users.
311 *
312 * For localization purposes, you can use the provided [translations](https://mui.com/material-ui/guides/localization/).
313 * @param {string} type The link or button type to format ('first' | 'last' | 'next' | 'previous').
314 * @returns {string}
315 * @default function defaultGetAriaLabel(type) {
316 * return `Go to ${type} page`;
317 * }
318 */
319 getItemAriaLabel: PropTypes.func,
320 /**
321 * Customize the displayed rows label. Invoked with a `{ from, to, count, page }`
322 * object.
323 *
324 * For localization purposes, you can use the provided [translations](https://mui.com/material-ui/guides/localization/).
325 * @default function defaultLabelDisplayedRows({ from, to, count }) {
326 * return `${from}–${to} of ${count !== -1 ? count : `more than ${to}`}`;
327 * }
328 */
329 labelDisplayedRows: PropTypes.func,
330 /**
331 * Customize the rows per page label.
332 *
333 * For localization purposes, you can use the provided [translations](https://mui.com/material-ui/guides/localization/).
334 * @default 'Rows per page:'
335 */
336 labelRowsPerPage: PropTypes.node,
337 /**
338 * Props applied to the next arrow [`IconButton`](https://mui.com/material-ui/api/icon-button/) element.
339 *
340 * This prop is an alias for `slotProps.actions.nextButton` and will be overriden by it if both are used.
341 * @deprecated Use `slotProps.actions.nextButton` instead.
342 */
343 nextIconButtonProps: PropTypes.object,
344 /**
345 * Callback fired when the page is changed.
346 *
347 * @param {React.MouseEvent<HTMLButtonElement> | null} event The event source of the callback.
348 * @param {number} page The page selected.
349 */
350 onPageChange: PropTypes.func.isRequired,
351 /**
352 * Callback fired when the number of rows per page is changed.
353 *
354 * @param {React.ChangeEvent<HTMLTextAreaElement | HTMLInputElement>} event The event source of the callback.
355 */
356 onRowsPerPageChange: PropTypes.func,
357 /**
358 * The zero-based index of the current page.
359 */
360 page: chainPropTypes(integerPropType.isRequired, props => {
361 const {
362 count,
363 page,
364 rowsPerPage
365 } = props;
366 if (count === -1) {
367 return null;
368 }
369 const newLastPage = Math.max(0, Math.ceil(count / rowsPerPage) - 1);
370 if (page < 0 || page > newLastPage) {
371 return new Error('MUI: The page prop of a TablePagination is out of range ' + `(0 to ${newLastPage}, but page is ${page}).`);
372 }
373 return null;
374 }),
375 /**
376 * The number of rows per page.
377 *
378 * Set -1 to display all the rows.
379 */
380 rowsPerPage: integerPropType.isRequired,
381 /**
382 * Customizes the options of the rows per page select field. If less than two options are
383 * available, no select field will be displayed.
384 * Use -1 for the value with a custom label to show all the rows.
385 * @default [10, 25, 50, 100]
386 */
387 rowsPerPageOptions: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.number, PropTypes.shape({
388 label: PropTypes.string.isRequired,
389 value: PropTypes.number.isRequired
390 })]).isRequired),
391 /**
392 * Props applied to the rows per page [`Select`](https://mui.com/material-ui/api/select/) element.
393 *
394 * This prop is an alias for `slotProps.select` and will be overriden by it if both are used.
395 * @deprecated Use `slotProps.select` instead.
396 *
397 * @default {}
398 */
399 SelectProps: PropTypes.object,
400 /**
401 * If `true`, show the first-page button.
402 * @default false
403 */
404 showFirstButton: PropTypes.bool,
405 /**
406 * If `true`, show the last-page button.
407 * @default false
408 */
409 showLastButton: PropTypes.bool,
410 /**
411 * The props used for each slot inside the TablePagination.
412 * @default {}
413 */
414 slotProps: PropTypes.shape({
415 actions: PropTypes.shape({
416 firstButton: PropTypes.object,
417 firstButtonIcon: PropTypes.object,
418 lastButton: PropTypes.object,
419 lastButtonIcon: PropTypes.object,
420 nextButton: PropTypes.object,
421 nextButtonIcon: PropTypes.object,
422 previousButton: PropTypes.object,
423 previousButtonIcon: PropTypes.object
424 }),
425 select: PropTypes.object
426 }),
427 /**
428 * The components used for each slot inside the TablePagination.
429 * Either a string to use a HTML element or a component.
430 * @default {}
431 */
432 slots: PropTypes.shape({
433 actions: PropTypes.shape({
434 firstButton: PropTypes.elementType,
435 firstButtonIcon: PropTypes.elementType,
436 lastButton: PropTypes.elementType,
437 lastButtonIcon: PropTypes.elementType,
438 nextButton: PropTypes.elementType,
439 nextButtonIcon: PropTypes.elementType,
440 previousButton: PropTypes.elementType,
441 previousButtonIcon: PropTypes.elementType
442 })
443 }),
444 /**
445 * The system prop that allows defining system overrides as well as additional CSS styles.
446 */
447 sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object])
448} : void 0;
449export default TablePagination;
\No newline at end of file