UNPKG

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