UNPKG

wix-style-react

Version:
128 lines (105 loc) 5 kB
import _extends from "@babel/runtime/helpers/extends"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } import React from 'react'; import PropTypes from 'prop-types'; import { Pagination as CorePagination } from "wix-ui-core/dist/es/src/components/pagination"; import { withFocusable } from "wix-ui-core/dist/es/src/hocs/Focusable/FocusableHOC"; import ChevronLeft from 'wix-ui-icons-common/ChevronLeft'; import ChevronRight from 'wix-ui-icons-common/ChevronRight'; import { st, classes } from './Pagination.st.css'; var coreComponentDefaults = { showFirstPage: true, showLastPage: true, responsive: false, showFirstLastNavButtons: false, showInputModeTotalPages: false, paginationMode: 'pages', nextLabel: /*#__PURE__*/React.createElement(ChevronRight, { className: classes.arrow }), previousLabel: /*#__PURE__*/React.createElement(ChevronLeft, { className: classes.arrow }) }; /** Component for pagination */ var Pagination = /*#__PURE__*/function (_React$PureComponent) { _inherits(Pagination, _React$PureComponent); var _super = _createSuper(Pagination); function Pagination() { var _this; _classCallCheck(this, Pagination); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _super.call.apply(_super, [this].concat(args)); _defineProperty(_assertThisInitialized(_this), "_getMaxPagesToShow", function () { var _this$props = _this.props, currentPage = _this$props.currentPage, totalPages = _this$props.totalPages; var absoluteNumDistance = Math.min(Math.abs(1 - currentPage), Math.abs(currentPage - totalPages)); if (absoluteNumDistance >= 4) { return 9; } else if (absoluteNumDistance === 3) { return 8; } return 7; }); return _this; } _createClass(Pagination, [{ key: "render", value: function render() { var _this$props2 = this.props, dataHook = _this$props2.dataHook, currentPage = _this$props2.currentPage, totalPages = _this$props2.totalPages, onChange = _this$props2.onChange, nextLabel = _this$props2.nextLabel, previousLabel = _this$props2.previousLabel, className = _this$props2.className; return /*#__PURE__*/React.createElement("div", { className: st(classes.root, className), "data-hook": dataHook, onFocus: this.props.focusableOnFocus, onBlur: this.props.focusableOnBlur }, /*#__PURE__*/React.createElement(CorePagination, _extends({ className: classes.pagination }, coreComponentDefaults, { previousLabel: previousLabel || coreComponentDefaults.previousLabel, nextLabel: nextLabel || coreComponentDefaults.nextLabel, onChange: onChange, totalPages: totalPages, currentPage: currentPage, maxPagesToShow: this._getMaxPagesToShow(), showNextLabel: currentPage !== totalPages, showPreviousLabel: currentPage !== 1 }))); } }]); return Pagination; }(React.PureComponent); _defineProperty(Pagination, "displayName", 'Pagination'); _defineProperty(Pagination, "propTypes", { /** Applied as data-hook HTML attribute that can be used in the tests */ dataHook: PropTypes.string, /** A css class to be applied to the component's root element */ className: PropTypes.string, /** Total available pages to show */ totalPages: PropTypes.number, /** Currently selected page */ currentPage: PropTypes.number, /** Returns selected page or arrow ({event,page}) */ onChange: PropTypes.func }); _defineProperty(Pagination, "defaultProps", { currentPage: 1 }); export default withFocusable(Pagination);