@catho/quantum
Version:
Catho react components
160 lines (159 loc) • 10.2 kB
JavaScript
;
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _react = require("react");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _styledComponents = _interopRequireDefault(require("styled-components"));
var _Grid = require("../Grid");
var _Desktop = _interopRequireDefault(require("./sub-components/Desktop"));
var _Mobile = _interopRequireDefault(require("./sub-components/Mobile"));
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["ariaLabel", "activePage", "activePageAriaLabel", "nextButtonText", "pageAriaLabel", "pageHref", "prevButtonText", "onPageClick", "totalPages", "infoFormatter", "tabIndex", "followOnlyFirstPage", "hideLastPagination"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], t.indexOf(o) >= 0 || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.indexOf(n) >= 0) continue; t[n] = r[n]; } return t; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
var Wrapper = _styledComponents["default"].nav.withConfig({
displayName: "Pagination__Wrapper",
componentId: "sc-1lcjvm4-0"
})(["align-items:center;display:flex;justify-content:center;"]);
var Pagination = /*#__PURE__*/function (_Component) {
function Pagination(props) {
var _this;
_classCallCheck(this, Pagination);
_this = _callSuper(this, Pagination, [props]);
_this.props = props;
return _this;
}
_inherits(Pagination, _Component);
return _createClass(Pagination, [{
key: "render",
value: function render() {
var _this$props = this.props,
ariaLabel = _this$props.ariaLabel,
activePage = _this$props.activePage,
activePageAriaLabel = _this$props.activePageAriaLabel,
nextButtonText = _this$props.nextButtonText,
pageAriaLabel = _this$props.pageAriaLabel,
pageHref = _this$props.pageHref,
prevButtonText = _this$props.prevButtonText,
onPageClick = _this$props.onPageClick,
totalPages = _this$props.totalPages,
infoFormatter = _this$props.infoFormatter,
tabIndex = _this$props.tabIndex,
followOnlyFirstPage = _this$props.followOnlyFirstPage,
hideLastPagination = _this$props.hideLastPagination,
props = _objectWithoutProperties(_this$props, _excluded);
var handlePageClick = function handlePageClick(page) {
return function (e) {
if (!onPageClick) {
return undefined;
}
e.preventDefault();
if (page <= 0 || page > totalPages) {
return false;
}
return onPageClick(page, e);
};
};
var handleHref = function handleHref(page) {
if (!pageHref || onPageClick || page <= 0 || page > totalPages) {
return undefined;
}
return pageHref(page);
};
var attributes = {
tabIndex: tabIndex,
activePage: activePage,
handlePageClick: handlePageClick,
handleHref: handleHref,
prevButtonText: prevButtonText,
nextButtonText: nextButtonText,
totalPages: totalPages,
activePageAriaLabel: activePageAriaLabel,
pageAriaLabel: pageAriaLabel,
infoFormatter: infoFormatter,
followOnlyFirstPage: followOnlyFirstPage,
hideLastPagination: hideLastPagination
};
return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Grid.Hide, {
xsmall: true,
small: true,
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Wrapper, _objectSpread(_objectSpread({
"aria-label": ariaLabel
}, props), {}, {
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Desktop["default"], _objectSpread({}, attributes))
}))
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_Grid.Hide, {
medium: true,
large: true,
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Wrapper, _objectSpread(_objectSpread({
"aria-label": ariaLabel
}, props), {}, {
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Mobile["default"], _objectSpread({}, attributes))
}))
})]
});
}
}]);
}(_react.Component);
Pagination.propTypes = {
tabIndex: _propTypes["default"].number,
/** Set the aria-label html attribute to the root element of pagination */
ariaLabel: _propTypes["default"].string,
/** Set the current page */
activePage: _propTypes["default"].number,
/** aria-label attribute to active page button. */
activePageAriaLabel: _propTypes["default"].string,
/** Set the number of pages to be displayed. */
totalPages: _propTypes["default"].number.isRequired,
/** Set next page button text */
nextButtonText: _propTypes["default"].string,
/** Page number button aria-label html attribute. */
pageAriaLabel: _propTypes["default"].string,
/** Set the page anchor element href value. */
pageHref: _propTypes["default"].func,
/** Set previous page button text. */
prevButtonText: _propTypes["default"].string,
/** Function to be called when prev, next or page button is clicked, it receives the next page number as an argument. */
onPageClick: _propTypes["default"].func,
/** When viewport is less than 600px (small breakpoint) the pagination will show the mobile layout, this function can be used to format the current and last page information. */
infoFormatter: _propTypes["default"].func,
/** If true, add rel="nofollow" property to all pagination buttons except page 1. */
followOnlyFirstPage: _propTypes["default"].bool,
/** If false, total pages will disappear. */
hideLastPagination: _propTypes["default"].bool
};
Pagination.defaultProps = {
tabIndex: undefined,
ariaLabel: 'pagination',
activePage: 1,
activePageAriaLabel: 'Current Page',
nextButtonText: 'Next',
pageAriaLabel: 'Page',
pageHref: undefined,
prevButtonText: 'Previous',
onPageClick: undefined,
infoFormatter: undefined,
followOnlyFirstPage: false,
hideLastPagination: false
};
var _default = exports["default"] = Pagination;