UNPKG

12.7 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
4
5var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
6
7Object.defineProperty(exports, "__esModule", {
8 value: true
9});
10exports.default = exports.styles = void 0;
11
12var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
13
14var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
15
16var React = _interopRequireWildcard(require("react"));
17
18var _propTypes = _interopRequireDefault(require("prop-types"));
19
20var _utils = require("@material-ui/utils");
21
22var _clsx = _interopRequireDefault(require("clsx"));
23
24var _deprecatedPropType = _interopRequireDefault(require("../utils/deprecatedPropType"));
25
26var _withStyles = _interopRequireDefault(require("../styles/withStyles"));
27
28var _InputBase = _interopRequireDefault(require("../InputBase"));
29
30var _MenuItem = _interopRequireDefault(require("../MenuItem"));
31
32var _Select = _interopRequireDefault(require("../Select"));
33
34var _TableCell = _interopRequireDefault(require("../TableCell"));
35
36var _Toolbar = _interopRequireDefault(require("../Toolbar"));
37
38var _Typography = _interopRequireDefault(require("../Typography"));
39
40var _TablePaginationActions = _interopRequireDefault(require("./TablePaginationActions"));
41
42var _unstable_useId = _interopRequireDefault(require("../utils/unstable_useId"));
43
44var styles = function styles(theme) {
45 return {
46 /* Styles applied to the root element. */
47 root: {
48 color: theme.palette.text.primary,
49 fontSize: theme.typography.pxToRem(14),
50 overflow: 'auto',
51 // Increase the specificity to override TableCell.
52 '&:last-child': {
53 padding: 0
54 }
55 },
56
57 /* Styles applied to the Toolbar component. */
58 toolbar: {
59 minHeight: 52,
60 paddingRight: 2
61 },
62
63 /* Styles applied to the spacer element. */
64 spacer: {
65 flex: '1 1 100%'
66 },
67
68 /* Styles applied to the caption Typography components if `variant="caption"`. */
69 caption: {
70 flexShrink: 0
71 },
72 // TODO v5: `.selectRoot` should be merged with `.input`
73
74 /* Styles applied to the Select component root element. */
75 selectRoot: {
76 marginRight: 32,
77 marginLeft: 8
78 },
79
80 /* Styles applied to the Select component `select` class. */
81 select: {
82 paddingLeft: 8,
83 paddingRight: 24,
84 textAlign: 'right',
85 textAlignLast: 'right' // Align <select> on Chrome.
86
87 },
88 // TODO v5: remove
89
90 /* Styles applied to the Select component `icon` class. */
91 selectIcon: {},
92
93 /* Styles applied to the `InputBase` component. */
94 input: {
95 color: 'inherit',
96 fontSize: 'inherit',
97 flexShrink: 0
98 },
99
100 /* Styles applied to the MenuItem component. */
101 menuItem: {},
102
103 /* Styles applied to the internal `TablePaginationActions` component. */
104 actions: {
105 flexShrink: 0,
106 marginLeft: 20
107 }
108 };
109};
110
111exports.styles = styles;
112
113var defaultLabelDisplayedRows = function defaultLabelDisplayedRows(_ref) {
114 var from = _ref.from,
115 to = _ref.to,
116 count = _ref.count;
117 return "".concat(from, "-").concat(to, " of ").concat(count !== -1 ? count : "more than ".concat(to));
118};
119
120var defaultRowsPerPageOptions = [10, 25, 50, 100];
121/**
122 * A `TableCell` based component for placing inside `TableFooter` for pagination.
123 */
124
125var TablePagination = /*#__PURE__*/React.forwardRef(function TablePagination(props, ref) {
126 var _props$ActionsCompone = props.ActionsComponent,
127 ActionsComponent = _props$ActionsCompone === void 0 ? _TablePaginationActions.default : _props$ActionsCompone,
128 backIconButtonProps = props.backIconButtonProps,
129 _props$backIconButton = props.backIconButtonText,
130 backIconButtonText = _props$backIconButton === void 0 ? 'Previous page' : _props$backIconButton,
131 classes = props.classes,
132 className = props.className,
133 colSpanProp = props.colSpan,
134 _props$component = props.component,
135 Component = _props$component === void 0 ? _TableCell.default : _props$component,
136 count = props.count,
137 _props$labelDisplayed = props.labelDisplayedRows,
138 labelDisplayedRows = _props$labelDisplayed === void 0 ? defaultLabelDisplayedRows : _props$labelDisplayed,
139 _props$labelRowsPerPa = props.labelRowsPerPage,
140 labelRowsPerPage = _props$labelRowsPerPa === void 0 ? 'Rows per page:' : _props$labelRowsPerPa,
141 nextIconButtonProps = props.nextIconButtonProps,
142 _props$nextIconButton = props.nextIconButtonText,
143 nextIconButtonText = _props$nextIconButton === void 0 ? 'Next page' : _props$nextIconButton,
144 onChangePage = props.onChangePage,
145 onPageChange = props.onPageChange,
146 onChangeRowsPerPageProp = props.onChangeRowsPerPage,
147 onRowsPerPageChangeProp = props.onRowsPerPageChange,
148 page = props.page,
149 rowsPerPage = props.rowsPerPage,
150 _props$rowsPerPageOpt = props.rowsPerPageOptions,
151 rowsPerPageOptions = _props$rowsPerPageOpt === void 0 ? defaultRowsPerPageOptions : _props$rowsPerPageOpt,
152 _props$SelectProps = props.SelectProps,
153 SelectProps = _props$SelectProps === void 0 ? {} : _props$SelectProps,
154 other = (0, _objectWithoutProperties2.default)(props, ["ActionsComponent", "backIconButtonProps", "backIconButtonText", "classes", "className", "colSpan", "component", "count", "labelDisplayedRows", "labelRowsPerPage", "nextIconButtonProps", "nextIconButtonText", "onChangePage", "onPageChange", "onChangeRowsPerPage", "onRowsPerPageChange", "page", "rowsPerPage", "rowsPerPageOptions", "SelectProps"]);
155 var onChangeRowsPerPage = onChangeRowsPerPageProp || onRowsPerPageChangeProp;
156 var colSpan;
157
158 if (Component === _TableCell.default || Component === 'td') {
159 colSpan = colSpanProp || 1000; // col-span over everything
160 }
161
162 var selectId = (0, _unstable_useId.default)();
163 var labelId = (0, _unstable_useId.default)();
164 var MenuItemComponent = SelectProps.native ? 'option' : _MenuItem.default;
165 return /*#__PURE__*/React.createElement(Component, (0, _extends2.default)({
166 className: (0, _clsx.default)(classes.root, className),
167 colSpan: colSpan,
168 ref: ref
169 }, other), /*#__PURE__*/React.createElement(_Toolbar.default, {
170 className: classes.toolbar
171 }, /*#__PURE__*/React.createElement("div", {
172 className: classes.spacer
173 }), rowsPerPageOptions.length > 1 && /*#__PURE__*/React.createElement(_Typography.default, {
174 color: "inherit",
175 variant: "body2",
176 className: classes.caption,
177 id: labelId
178 }, labelRowsPerPage), rowsPerPageOptions.length > 1 && /*#__PURE__*/React.createElement(_Select.default, (0, _extends2.default)({
179 classes: {
180 select: classes.select,
181 icon: classes.selectIcon
182 },
183 input: /*#__PURE__*/React.createElement(_InputBase.default, {
184 className: (0, _clsx.default)(classes.input, classes.selectRoot)
185 }),
186 value: rowsPerPage,
187 onChange: onChangeRowsPerPage,
188 id: selectId,
189 labelId: labelId
190 }, SelectProps), rowsPerPageOptions.map(function (rowsPerPageOption) {
191 return /*#__PURE__*/React.createElement(MenuItemComponent, {
192 className: classes.menuItem,
193 key: rowsPerPageOption.value ? rowsPerPageOption.value : rowsPerPageOption,
194 value: rowsPerPageOption.value ? rowsPerPageOption.value : rowsPerPageOption
195 }, rowsPerPageOption.label ? rowsPerPageOption.label : rowsPerPageOption);
196 })), /*#__PURE__*/React.createElement(_Typography.default, {
197 color: "inherit",
198 variant: "body2",
199 className: classes.caption
200 }, labelDisplayedRows({
201 from: count === 0 ? 0 : page * rowsPerPage + 1,
202 to: count !== -1 ? Math.min(count, (page + 1) * rowsPerPage) : (page + 1) * rowsPerPage,
203 count: count === -1 ? -1 : count,
204 page: page
205 })), /*#__PURE__*/React.createElement(ActionsComponent, {
206 className: classes.actions,
207 backIconButtonProps: (0, _extends2.default)({
208 title: backIconButtonText,
209 'aria-label': backIconButtonText
210 }, backIconButtonProps),
211 count: count,
212 nextIconButtonProps: (0, _extends2.default)({
213 title: nextIconButtonText,
214 'aria-label': nextIconButtonText
215 }, nextIconButtonProps),
216 onChangePage: onChangePage,
217 onPageChange: onPageChange,
218 page: page,
219 rowsPerPage: rowsPerPage
220 })));
221});
222process.env.NODE_ENV !== "production" ? TablePagination.propTypes = {
223 /**
224 * The component used for displaying the actions.
225 * Either a string to use a HTML element or a component.
226 */
227 ActionsComponent: _propTypes.default.elementType,
228
229 /**
230 * Props applied to the back arrow [`IconButton`](/api/icon-button/) component.
231 */
232 backIconButtonProps: _propTypes.default.object,
233
234 /**
235 * Text label for the back arrow icon button.
236 *
237 * For localization purposes, you can use the provided [translations](/guides/localization/).
238 */
239 backIconButtonText: _propTypes.default.string,
240
241 /**
242 * Override or extend the styles applied to the component.
243 * See [CSS API](#css) below for more details.
244 */
245 classes: _propTypes.default.object.isRequired,
246
247 /**
248 * @ignore
249 */
250 className: _propTypes.default.string,
251
252 /**
253 * @ignore
254 */
255 colSpan: _propTypes.default.number,
256
257 /**
258 * The component used for the root node.
259 * Either a string to use a HTML element or a component.
260 */
261 component: _propTypes.default
262 /* @typescript-to-proptypes-ignore */
263 .elementType,
264
265 /**
266 * The total number of rows.
267 *
268 * To enable server side pagination for an unknown number of items, provide -1.
269 */
270 count: _propTypes.default.number.isRequired,
271
272 /**
273 * Customize the displayed rows label. Invoked with a `{ from, to, count, page }`
274 * object.
275 *
276 * For localization purposes, you can use the provided [translations](/guides/localization/).
277 */
278 labelDisplayedRows: _propTypes.default.func,
279
280 /**
281 * Customize the rows per page label.
282 *
283 * For localization purposes, you can use the provided [translations](/guides/localization/).
284 */
285 labelRowsPerPage: _propTypes.default.node,
286
287 /**
288 * Props applied to the next arrow [`IconButton`](/api/icon-button/) element.
289 */
290 nextIconButtonProps: _propTypes.default.object,
291
292 /**
293 * Text label for the next arrow icon button.
294 *
295 * For localization purposes, you can use the provided [translations](/guides/localization/).
296 */
297 nextIconButtonText: _propTypes.default.string,
298
299 /**
300 * Callback fired when the page is changed.
301 *
302 * @param {object} event The event source of the callback.
303 * @param {number} page The page selected.
304 * @deprecated Use the onPageChange prop instead.
305 */
306 onChangePage: (0, _deprecatedPropType.default)(_propTypes.default.func, 'Use the `onPageChange` prop instead.'),
307
308 /**
309 * Callback fired when the number of rows per page is changed.
310 *
311 * @param {object} event The event source of the callback.
312 * @deprecated Use the onRowsPerPageChange prop instead.
313 */
314 onChangeRowsPerPage: (0, _deprecatedPropType.default)(_propTypes.default.func, 'Use the `onRowsPerPageChange` prop instead.'),
315
316 /**
317 * Callback fired when the page is changed.
318 *
319 * @param {object} event The event source of the callback.
320 * @param {number} page The page selected.
321 */
322 onPageChange: _propTypes.default.func.isRequired,
323
324 /**
325 * Callback fired when the number of rows per page is changed.
326 *
327 * @param {object} event The event source of the callback.
328 */
329 onRowsPerPageChange: _propTypes.default.func,
330
331 /**
332 * The zero-based index of the current page.
333 */
334 page: (0, _utils.chainPropTypes)(_propTypes.default.number.isRequired, function (props) {
335 var count = props.count,
336 page = props.page,
337 rowsPerPage = props.rowsPerPage;
338
339 if (count === -1) {
340 return null;
341 }
342
343 var newLastPage = Math.max(0, Math.ceil(count / rowsPerPage) - 1);
344
345 if (page < 0 || page > newLastPage) {
346 return new Error('Material-UI: The page prop of a TablePagination is out of range ' + "(0 to ".concat(newLastPage, ", but page is ").concat(page, ")."));
347 }
348
349 return null;
350 }),
351
352 /**
353 * The number of rows per page.
354 */
355 rowsPerPage: _propTypes.default.number.isRequired,
356
357 /**
358 * Customizes the options of the rows per page select field. If less than two options are
359 * available, no select field will be displayed.
360 */
361 rowsPerPageOptions: _propTypes.default.array,
362
363 /**
364 * Props applied to the rows per page [`Select`](/api/select/) element.
365 */
366 SelectProps: _propTypes.default.object
367} : void 0;
368
369var _default = (0, _withStyles.default)(styles, {
370 name: 'MuiTablePagination'
371})(TablePagination);
372
373exports.default = _default;
\No newline at end of file