UNPKG

@material-ui/core

Version:

React components that implement Google's Material Design.

300 lines (248 loc) 9.56 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.styles = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties")); var _react = _interopRequireDefault(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _utils = require("@material-ui/utils"); var _clsx = _interopRequireDefault(require("clsx")); var _withStyles = _interopRequireDefault(require("../styles/withStyles")); var _InputBase = _interopRequireDefault(require("../InputBase")); var _MenuItem = _interopRequireDefault(require("../MenuItem")); var _Select = _interopRequireDefault(require("../Select")); var _TableCell = _interopRequireDefault(require("../TableCell")); var _Toolbar = _interopRequireDefault(require("../Toolbar")); var _Typography = _interopRequireDefault(require("../Typography")); var _TablePaginationActions = _interopRequireDefault(require("./TablePaginationActions")); var styles = function styles(theme) { return { /* Styles applied to the root element. */ root: { color: theme.palette.text.secondary, fontSize: theme.typography.pxToRem(12), // Increase the specificity to override TableCell. '&:last-child': { padding: 0 } }, /* Styles applied to the Toolbar component. */ toolbar: { height: 56, minHeight: 56, paddingRight: 2 }, /* Styles applied to the spacer element. */ spacer: { flex: '1 1 100%' }, /* Styles applied to the caption Typography components if `variant="caption"`. */ caption: { flexShrink: 0 }, /* Styles applied to the Select component root element. */ selectRoot: { // `.selectRoot` should be merged with `.input` in v5. marginRight: 32, marginLeft: 8 }, /* Styles applied to the Select component `select` class. */ select: { paddingLeft: 8, paddingRight: 24, textAlign: 'right', textAlignLast: 'right' // Align <select> on Chrome. }, /* Styles applied to the Select component `icon` class. */ selectIcon: { top: 1 }, /* Styles applied to the `InputBase` component. */ input: { color: 'inherit', fontSize: 'inherit', flexShrink: 0 }, /* Styles applied to the MenuItem component. */ menuItem: {}, /* Styles applied to the internal `TablePaginationActions` component. */ actions: { flexShrink: 0, marginLeft: 20 } }; }; exports.styles = styles; var defaultLabelDisplayedRows = function defaultLabelDisplayedRows(_ref) { var from = _ref.from, to = _ref.to, count = _ref.count; return "".concat(from, "-").concat(to, " of ").concat(count); }; var defaultRowsPerPageOptions = [10, 25, 50, 100]; /** * A `TableCell` based component for placing inside `TableFooter` for pagination. */ var TablePagination = _react.default.forwardRef(function TablePagination(props, ref) { var _props$ActionsCompone = props.ActionsComponent, ActionsComponent = _props$ActionsCompone === void 0 ? _TablePaginationActions.default : _props$ActionsCompone, backIconButtonProps = props.backIconButtonProps, classes = props.classes, colSpanProp = props.colSpan, _props$component = props.component, Component = _props$component === void 0 ? _TableCell.default : _props$component, count = props.count, _props$labelDisplayed = props.labelDisplayedRows, labelDisplayedRows = _props$labelDisplayed === void 0 ? defaultLabelDisplayedRows : _props$labelDisplayed, _props$labelRowsPerPa = props.labelRowsPerPage, labelRowsPerPage = _props$labelRowsPerPa === void 0 ? 'Rows per page:' : _props$labelRowsPerPa, nextIconButtonProps = props.nextIconButtonProps, onChangePage = props.onChangePage, onChangeRowsPerPage = props.onChangeRowsPerPage, page = props.page, rowsPerPage = props.rowsPerPage, _props$rowsPerPageOpt = props.rowsPerPageOptions, rowsPerPageOptions = _props$rowsPerPageOpt === void 0 ? defaultRowsPerPageOptions : _props$rowsPerPageOpt, _props$SelectProps = props.SelectProps, SelectProps = _props$SelectProps === void 0 ? {} : _props$SelectProps, other = (0, _objectWithoutProperties2.default)(props, ["ActionsComponent", "backIconButtonProps", "classes", "colSpan", "component", "count", "labelDisplayedRows", "labelRowsPerPage", "nextIconButtonProps", "onChangePage", "onChangeRowsPerPage", "page", "rowsPerPage", "rowsPerPageOptions", "SelectProps"]); var colSpan; if (Component === _TableCell.default || Component === 'td') { colSpan = colSpanProp || 1000; // col-span over everything } var MenuItemComponent = SelectProps.native ? 'option' : _MenuItem.default; return _react.default.createElement(Component, (0, _extends2.default)({ className: classes.root, colSpan: colSpan, ref: ref }, other), _react.default.createElement(_Toolbar.default, { className: classes.toolbar }, _react.default.createElement("div", { className: classes.spacer }), rowsPerPageOptions.length > 1 && _react.default.createElement(_Typography.default, { color: "inherit", variant: "caption", className: classes.caption }, labelRowsPerPage), rowsPerPageOptions.length > 1 && _react.default.createElement(_Select.default, (0, _extends2.default)({ classes: { select: classes.select, icon: classes.selectIcon }, input: _react.default.createElement(_InputBase.default, { className: (0, _clsx.default)(classes.input, classes.selectRoot) }), value: rowsPerPage, onChange: onChangeRowsPerPage }, SelectProps), rowsPerPageOptions.map(function (rowsPerPageOption) { return _react.default.createElement(MenuItemComponent, { className: classes.menuItem, key: rowsPerPageOption, value: rowsPerPageOption }, rowsPerPageOption); })), _react.default.createElement(_Typography.default, { color: "inherit", variant: "caption", className: classes.caption }, labelDisplayedRows({ from: count === 0 ? 0 : page * rowsPerPage + 1, to: Math.min(count, (page + 1) * rowsPerPage), count: count, page: page })), _react.default.createElement(ActionsComponent, { className: classes.actions, backIconButtonProps: backIconButtonProps, count: count, nextIconButtonProps: nextIconButtonProps, onChangePage: onChangePage, page: page, rowsPerPage: rowsPerPage }))); }); process.env.NODE_ENV !== "production" ? TablePagination.propTypes = { /** * The component used for displaying the actions. * Either a string to use a DOM element or a component. */ ActionsComponent: _propTypes.default.elementType, /** * Props applied to the back arrow [`IconButton`](/api/icon-button/) component. */ backIconButtonProps: _propTypes.default.object, /** * Override or extend the styles applied to the component. * See [CSS API](#css) below for more details. */ classes: _propTypes.default.object.isRequired, /** * @ignore */ colSpan: _propTypes.default.number, /** * The component used for the root node. * Either a string to use a DOM element or a component. */ component: _propTypes.default.elementType, /** * The total number of rows. */ count: _propTypes.default.number.isRequired, /** * Customize the displayed rows label. */ labelDisplayedRows: _propTypes.default.func, /** * Customize the rows per page label. Invoked with a `{ from, to, count, page }` * object. */ labelRowsPerPage: _propTypes.default.node, /** * Props applied to the next arrow [`IconButton`](/api/icon-button/) element. */ nextIconButtonProps: _propTypes.default.object, /** * Callback fired when the page is changed. * * @param {object} event The event source of the callback * @param {number} page The page selected */ onChangePage: _propTypes.default.func.isRequired, /** * Callback fired when the number of rows per page is changed. * * @param {object} event The event source of the callback */ onChangeRowsPerPage: _propTypes.default.func, /** * The zero-based index of the current page. */ page: (0, _utils.chainPropTypes)(_propTypes.default.number.isRequired, function (props) { var count = props.count, page = props.page, rowsPerPage = props.rowsPerPage; var newLastPage = Math.max(0, Math.ceil(count / rowsPerPage) - 1); if (page < 0 || page > newLastPage) { return new Error('Material-UI: the page prop of a TablePagination is out of range ' + "(0 to ".concat(newLastPage, ", but page is ").concat(page, ").")); } return null; }), /** * The number of rows per page. */ rowsPerPage: _propTypes.default.number.isRequired, /** * Customizes the options of the rows per page select field. If less than two options are * available, no select field will be displayed. */ rowsPerPageOptions: _propTypes.default.array, /** * Props applied to the rows per page [`Select`](/api/select/) element. */ SelectProps: _propTypes.default.object } : void 0; var _default = (0, _withStyles.default)(styles, { name: 'MuiTablePagination' })(TablePagination); exports.default = _default;