react-instantsearch
Version:
⚡ Lightning-fast search for React, by Algolia
127 lines (126 loc) • 7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Pagination = Pagination;
var _instantsearchUiComponents = require("instantsearch-ui-components");
var _react = _interopRequireDefault(require("react"));
var _isModifierClick = require("./lib/isModifierClick");
var _excluded = ["pages", "currentPage", "nbPages", "isFirstPage", "isLastPage", "showFirst", "showPrevious", "showNext", "showLast", "createURL", "onNavigate", "translations", "classNames"],
_excluded2 = ["isDisabled", "className", "classNames", "href", "onClick"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function Pagination(_ref) {
var pages = _ref.pages,
currentPage = _ref.currentPage,
nbPages = _ref.nbPages,
isFirstPage = _ref.isFirstPage,
isLastPage = _ref.isLastPage,
_ref$showFirst = _ref.showFirst,
showFirst = _ref$showFirst === void 0 ? true : _ref$showFirst,
_ref$showPrevious = _ref.showPrevious,
showPrevious = _ref$showPrevious === void 0 ? true : _ref$showPrevious,
_ref$showNext = _ref.showNext,
showNext = _ref$showNext === void 0 ? true : _ref$showNext,
_ref$showLast = _ref.showLast,
showLast = _ref$showLast === void 0 ? true : _ref$showLast,
createURL = _ref.createURL,
onNavigate = _ref.onNavigate,
translations = _ref.translations,
_ref$classNames = _ref.classNames,
classNames = _ref$classNames === void 0 ? {} : _ref$classNames,
props = _objectWithoutProperties(_ref, _excluded);
var firstPageIndex = 0;
var previousPageIndex = currentPage - 1;
var nextPageIndex = currentPage + 1;
var lastPageIndex = nbPages - 1;
return /*#__PURE__*/_react.default.createElement("div", _extends({}, props, {
className: (0, _instantsearchUiComponents.cx)('ais-Pagination', classNames.root, nbPages <= 1 && (0, _instantsearchUiComponents.cx)('ais-Pagination--noRefinement', classNames.noRefinementRoot), props.className)
}), /*#__PURE__*/_react.default.createElement("ul", {
className: (0, _instantsearchUiComponents.cx)('ais-Pagination-list', classNames.list)
}, showFirst && /*#__PURE__*/_react.default.createElement(PaginationItem, {
isDisabled: isFirstPage,
className: (0, _instantsearchUiComponents.cx)('ais-Pagination-item--firstPage', classNames.firstPageItem),
classNames: classNames,
"aria-label": translations.firstPageItemAriaLabel,
href: createURL(firstPageIndex),
onClick: function onClick() {
return onNavigate(firstPageIndex);
}
}, translations.firstPageItemText), showPrevious && /*#__PURE__*/_react.default.createElement(PaginationItem, {
isDisabled: isFirstPage,
className: (0, _instantsearchUiComponents.cx)('ais-Pagination-item--previousPage', classNames.previousPageItem),
classNames: classNames,
"aria-label": translations.previousPageItemAriaLabel,
href: createURL(previousPageIndex),
onClick: function onClick() {
return onNavigate(previousPageIndex);
}
}, translations.previousPageItemText), pages.map(function (page) {
return /*#__PURE__*/_react.default.createElement(PaginationItem, {
key: page,
isDisabled: false,
className: (0, _instantsearchUiComponents.cx)('ais-Pagination-item--page', classNames.pageItem, page === currentPage && (0, _instantsearchUiComponents.cx)('ais-Pagination-item--selected', classNames.selectedItem)),
classNames: classNames,
"aria-label": translations.pageItemAriaLabel({
currentPage: page + 1,
nbPages: nbPages
}),
href: createURL(page),
onClick: function onClick() {
return onNavigate(page);
}
}, translations.pageItemText({
currentPage: page + 1,
nbPages: nbPages
}));
}), showNext && /*#__PURE__*/_react.default.createElement(PaginationItem, {
isDisabled: isLastPage,
className: (0, _instantsearchUiComponents.cx)('ais-Pagination-item--nextPage', classNames.nextPageItem),
classNames: classNames,
"aria-label": translations.nextPageItemAriaLabel,
href: createURL(nextPageIndex),
onClick: function onClick() {
return onNavigate(nextPageIndex);
}
}, translations.nextPageItemText), showLast && /*#__PURE__*/_react.default.createElement(PaginationItem, {
isDisabled: isLastPage,
className: (0, _instantsearchUiComponents.cx)('ais-Pagination-item--lastPage', classNames.lastPageItem),
classNames: classNames,
"aria-label": translations.lastPageItemAriaLabel,
href: createURL(lastPageIndex),
onClick: function onClick() {
return onNavigate(lastPageIndex);
}
}, translations.lastPageItemText)));
}
function PaginationItem(_ref2) {
var isDisabled = _ref2.isDisabled,
className = _ref2.className,
classNames = _ref2.classNames,
href = _ref2.href,
_onClick = _ref2.onClick,
props = _objectWithoutProperties(_ref2, _excluded2);
if (isDisabled) {
return /*#__PURE__*/_react.default.createElement("li", {
className: (0, _instantsearchUiComponents.cx)('ais-Pagination-item', classNames.item, 'ais-Pagination-item--disabled', classNames.disabledItem, className)
}, /*#__PURE__*/_react.default.createElement("span", _extends({
className: (0, _instantsearchUiComponents.cx)('ais-Pagination-link', classNames.link)
}, props)));
}
return /*#__PURE__*/_react.default.createElement("li", {
className: (0, _instantsearchUiComponents.cx)('ais-Pagination-item', classNames.item, className)
}, /*#__PURE__*/_react.default.createElement("a", _extends({
className: (0, _instantsearchUiComponents.cx)('ais-Pagination-link', classNames.link),
href: href,
onClick: function onClick(event) {
if ((0, _isModifierClick.isModifierClick)(event)) {
return;
}
event.preventDefault();
_onClick(event);
}
}, props)));
}