UNPKG

13 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
3import * as React from 'react';
4import PropTypes from 'prop-types';
5import { unstable_useId as useId, chainPropTypes, integerPropType } from '@mui/utils';
6import { useSlotProps } from '../utils';
7import composeClasses from '../composeClasses';
8import isHostComponent from '../utils/isHostComponent';
9import TablePaginationActions from './TablePaginationActions';
10import { getTablePaginationUtilityClass } from './tablePaginationClasses';
11import { useClassNamesOverride } from '../utils/ClassNameConfigurator';
12import { jsx as _jsx } from "react/jsx-runtime";
13import { createElement as _createElement } from "react";
14import { jsxs as _jsxs } from "react/jsx-runtime";
15function defaultLabelDisplayedRows(_ref) {
16 var from = _ref.from,
17 to = _ref.to,
18 count = _ref.count;
19 return "".concat(from, "\u2013").concat(to, " of ").concat(count !== -1 ? count : "more than ".concat(to));
20}
21function defaultGetAriaLabel(type) {
22 return "Go to ".concat(type, " page");
23}
24var useUtilityClasses = function useUtilityClasses() {
25 var slots = {
26 root: ['root'],
27 toolbar: ['toolbar'],
28 spacer: ['spacer'],
29 selectLabel: ['selectLabel'],
30 select: ['select'],
31 input: ['input'],
32 selectIcon: ['selectIcon'],
33 menuItem: ['menuItem'],
34 displayedRows: ['displayedRows'],
35 actions: ['actions']
36 };
37 return composeClasses(slots, useClassNamesOverride(getTablePaginationUtilityClass));
38};
39
40/**
41 * A pagination for tables.
42 *
43 * Demos:
44 *
45 * - [Table Pagination](https://mui.com/base/react-table-pagination/)
46 *
47 * API:
48 *
49 * - [TablePagination API](https://mui.com/base/react-table-pagination/components-api/#table-pagination)
50 */
51var TablePagination = /*#__PURE__*/React.forwardRef(function TablePagination(props, forwardedRef) {
52 var _slots$root, _slots$select, _slots$actions, _slots$menuItem, _slots$selectLabel, _slots$displayedRows, _slots$toolbar, _slots$spacer;
53 var colSpanProp = props.colSpan,
54 count = props.count,
55 _props$getItemAriaLab = props.getItemAriaLabel,
56 getItemAriaLabel = _props$getItemAriaLab === void 0 ? defaultGetAriaLabel : _props$getItemAriaLab,
57 _props$labelDisplayed = props.labelDisplayedRows,
58 labelDisplayedRows = _props$labelDisplayed === void 0 ? defaultLabelDisplayedRows : _props$labelDisplayed,
59 labelIdProp = props.labelId,
60 _props$labelRowsPerPa = props.labelRowsPerPage,
61 labelRowsPerPage = _props$labelRowsPerPa === void 0 ? 'Rows per page:' : _props$labelRowsPerPa,
62 onPageChange = props.onPageChange,
63 onRowsPerPageChange = props.onRowsPerPageChange,
64 page = props.page,
65 rowsPerPage = props.rowsPerPage,
66 _props$rowsPerPageOpt = props.rowsPerPageOptions,
67 rowsPerPageOptions = _props$rowsPerPageOpt === void 0 ? [10, 25, 50, 100] : _props$rowsPerPageOpt,
68 selectIdProp = props.selectId,
69 _props$slotProps = props.slotProps,
70 slotProps = _props$slotProps === void 0 ? {} : _props$slotProps,
71 _props$slots = props.slots,
72 slots = _props$slots === void 0 ? {} : _props$slots,
73 other = _objectWithoutProperties(props, ["colSpan", "count", "getItemAriaLabel", "labelDisplayedRows", "labelId", "labelRowsPerPage", "onPageChange", "onRowsPerPageChange", "page", "rowsPerPage", "rowsPerPageOptions", "selectId", "slotProps", "slots"]);
74 var ownerState = props;
75 var classes = useUtilityClasses();
76 var colSpan;
77 var Root = (_slots$root = slots.root) != null ? _slots$root : 'td';
78 if (Root === 'td' || !isHostComponent(Root)) {
79 colSpan = colSpanProp || 1000; // col-span over everything
80 }
81
82 var getLabelDisplayedRowsTo = function getLabelDisplayedRowsTo() {
83 if (count === -1) {
84 return (page + 1) * rowsPerPage;
85 }
86 return rowsPerPage === -1 ? count : Math.min(count, (page + 1) * rowsPerPage);
87 };
88 var selectId = useId(selectIdProp);
89 var labelId = useId(labelIdProp);
90 var rootProps = useSlotProps({
91 elementType: Root,
92 externalSlotProps: slotProps.root,
93 externalForwardedProps: other,
94 additionalProps: {
95 colSpan: colSpan,
96 ref: forwardedRef
97 },
98 ownerState: ownerState,
99 className: classes.root
100 });
101 var Select = (_slots$select = slots.select) != null ? _slots$select : 'select';
102 var selectProps = useSlotProps({
103 elementType: Select,
104 externalSlotProps: slotProps.select,
105 additionalProps: {
106 value: rowsPerPage,
107 id: selectId,
108 onChange: function onChange(event) {
109 return onRowsPerPageChange && onRowsPerPageChange(event);
110 },
111 'aria-label': rowsPerPage.toString(),
112 'aria-labelledby': [labelId, selectId].filter(Boolean).join(' ') || undefined
113 },
114 ownerState: ownerState,
115 className: classes.select
116 });
117 var Actions = (_slots$actions = slots.actions) != null ? _slots$actions : TablePaginationActions;
118 var actionsProps = useSlotProps({
119 elementType: Actions,
120 externalSlotProps: slotProps.actions,
121 additionalProps: {
122 page: page,
123 rowsPerPage: rowsPerPage,
124 count: count,
125 onPageChange: onPageChange,
126 getItemAriaLabel: getItemAriaLabel
127 },
128 ownerState: ownerState,
129 className: classes.actions
130 });
131 var MenuItem = (_slots$menuItem = slots.menuItem) != null ? _slots$menuItem : 'option';
132 var menuItemProps = useSlotProps({
133 elementType: MenuItem,
134 externalSlotProps: slotProps.menuItem,
135 additionalProps: {
136 value: undefined
137 },
138 ownerState: ownerState,
139 className: classes.menuItem
140 });
141 var SelectLabel = (_slots$selectLabel = slots.selectLabel) != null ? _slots$selectLabel : 'p';
142 var selectLabelProps = useSlotProps({
143 elementType: SelectLabel,
144 externalSlotProps: slotProps.selectLabel,
145 additionalProps: {
146 id: labelId
147 },
148 ownerState: ownerState,
149 className: classes.selectLabel
150 });
151 var DisplayedRows = (_slots$displayedRows = slots.displayedRows) != null ? _slots$displayedRows : 'p';
152 var displayedRowsProps = useSlotProps({
153 elementType: DisplayedRows,
154 externalSlotProps: slotProps.displayedRows,
155 ownerState: ownerState,
156 className: classes.displayedRows
157 });
158 var Toolbar = (_slots$toolbar = slots.toolbar) != null ? _slots$toolbar : 'div';
159 var toolbarProps = useSlotProps({
160 elementType: Toolbar,
161 externalSlotProps: slotProps.toolbar,
162 ownerState: ownerState,
163 className: classes.toolbar
164 });
165 var Spacer = (_slots$spacer = slots.spacer) != null ? _slots$spacer : 'div';
166 var spacerProps = useSlotProps({
167 elementType: Spacer,
168 externalSlotProps: slotProps.spacer,
169 ownerState: ownerState,
170 className: classes.spacer
171 });
172 return /*#__PURE__*/_jsx(Root, _extends({}, rootProps, {
173 children: /*#__PURE__*/_jsxs(Toolbar, _extends({}, toolbarProps, {
174 children: [/*#__PURE__*/_jsx(Spacer, _extends({}, spacerProps)), rowsPerPageOptions.length > 1 && /*#__PURE__*/_jsx(SelectLabel, _extends({}, selectLabelProps, {
175 children: labelRowsPerPage
176 })), rowsPerPageOptions.length > 1 && /*#__PURE__*/_jsx(Select, _extends({}, selectProps, {
177 children: rowsPerPageOptions.map(function (rowsPerPageOption) {
178 return /*#__PURE__*/_createElement(MenuItem, _extends({}, menuItemProps, {
179 key: typeof rowsPerPageOption !== 'number' && rowsPerPageOption.label ? rowsPerPageOption.label : rowsPerPageOption,
180 value: typeof rowsPerPageOption !== 'number' && rowsPerPageOption.value ? rowsPerPageOption.value : rowsPerPageOption
181 }), typeof rowsPerPageOption !== 'number' && rowsPerPageOption.label ? rowsPerPageOption.label : rowsPerPageOption);
182 })
183 })), /*#__PURE__*/_jsx(DisplayedRows, _extends({}, displayedRowsProps, {
184 children: labelDisplayedRows({
185 from: count === 0 ? 0 : page * rowsPerPage + 1,
186 to: getLabelDisplayedRowsTo(),
187 count: count === -1 ? -1 : count,
188 page: page
189 })
190 })), /*#__PURE__*/_jsx(Actions, _extends({}, actionsProps))]
191 }))
192 }));
193});
194process.env.NODE_ENV !== "production" ? TablePagination.propTypes /* remove-proptypes */ = {
195 // ----------------------------- Warning --------------------------------
196 // | These PropTypes are generated from the TypeScript type definitions |
197 // | To update them edit TypeScript types and run "yarn proptypes" |
198 // ----------------------------------------------------------------------
199 /**
200 * @ignore
201 */
202 colSpan: PropTypes.number,
203 /**
204 * The total number of rows.
205 *
206 * To enable server side pagination for an unknown number of items, provide -1.
207 */
208 count: PropTypes.number.isRequired,
209 /**
210 * Accepts a function which returns a string value that provides a user-friendly name for the current page.
211 * This is important for screen reader users.
212 *
213 * For localization purposes, you can use the provided [translations](/material-ui/guides/localization/).
214 * @param {string} type The link or button type to format ('first' | 'last' | 'next' | 'previous').
215 * @returns {string}
216 * @default function defaultGetAriaLabel(type: ItemAriaLabelType) {
217 * return `Go to ${type} page`;
218 * }
219 */
220 getItemAriaLabel: PropTypes.func,
221 /**
222 * Customize the displayed rows label. Invoked with a `{ from, to, count, page }`
223 * object.
224 *
225 * For localization purposes, you can use the provided [translations](/material-ui/guides/localization/).
226 * @default function defaultLabelDisplayedRows({ from, to, count }: LabelDisplayedRowsArgs) {
227 * return `${from}–${to} of ${count !== -1 ? count : `more than ${to}`}`;
228 * }
229 */
230 labelDisplayedRows: PropTypes.func,
231 /**
232 * Id of the label element within the pagination.
233 */
234 labelId: PropTypes.string,
235 /**
236 * Customize the rows per page label.
237 *
238 * For localization purposes, you can use the provided [translations](/material-ui/guides/localization/).
239 * @default 'Rows per page:'
240 */
241 labelRowsPerPage: PropTypes.node,
242 /**
243 * Callback fired when the page is changed.
244 *
245 * @param {React.MouseEvent<HTMLButtonElement> | null} event The event source of the callback.
246 * @param {number} page The page selected.
247 */
248 onPageChange: PropTypes.func.isRequired,
249 /**
250 * Callback fired when the number of rows per page is changed.
251 *
252 * @param {React.ChangeEvent<HTMLTextAreaElement | HTMLInputElement>} event The event source of the callback.
253 */
254 onRowsPerPageChange: PropTypes.func,
255 /**
256 * The zero-based index of the current page.
257 */
258 page: chainPropTypes(integerPropType.isRequired, function (props) {
259 var count = props.count,
260 page = props.page,
261 rowsPerPage = props.rowsPerPage;
262 if (count === -1) {
263 return null;
264 }
265 var newLastPage = Math.max(0, Math.ceil(count / rowsPerPage) - 1);
266 if (page < 0 || page > newLastPage) {
267 return new Error('MUI: The page prop of a TablePagination is out of range ' + "(0 to ".concat(newLastPage, ", but page is ").concat(page, ")."));
268 }
269 return null;
270 }),
271 /**
272 * The number of rows per page.
273 *
274 * Set -1 to display all the rows.
275 */
276 rowsPerPage: integerPropType.isRequired,
277 /**
278 * Customizes the options of the rows per page select field. If less than two options are
279 * available, no select field will be displayed.
280 * Use -1 for the value with a custom label to show all the rows.
281 * @default [10, 25, 50, 100]
282 */
283 rowsPerPageOptions: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.number, PropTypes.shape({
284 label: PropTypes.string.isRequired,
285 value: PropTypes.number.isRequired
286 })]).isRequired),
287 /**
288 * Id of the select element within the pagination.
289 */
290 selectId: PropTypes.string,
291 /**
292 * The props used for each slot inside the TablePagination.
293 * @default {}
294 */
295 slotProps: PropTypes /* @typescript-to-proptypes-ignore */.shape({
296 actions: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
297 displayedRows: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
298 menuItem: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
299 root: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
300 select: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
301 selectLabel: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
302 spacer: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
303 toolbar: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
304 }),
305 /**
306 * The components used for each slot inside the TablePagination.
307 * Either a string to use a HTML element or a component.
308 * @default {}
309 */
310 slots: PropTypes.shape({
311 actions: PropTypes.elementType,
312 displayedRows: PropTypes.elementType,
313 menuItem: PropTypes.elementType,
314 root: PropTypes.elementType,
315 select: PropTypes.elementType,
316 selectLabel: PropTypes.elementType,
317 spacer: PropTypes.elementType,
318 toolbar: PropTypes.elementType
319 })
320} : void 0;
321export default TablePagination;
\No newline at end of file