UNPKG

@navikt/ds-react

Version:

React components from the Norwegian Labour and Welfare Administration.

152 lines 8.18 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Pagination = exports.getSteps = void 0; const clsx_1 = __importDefault(require("clsx")); const react_1 = __importStar(require("react")); const aksel_icons_1 = require("@navikt/aksel-icons"); const Theme_1 = require("../theme/Theme"); const typography_1 = require("../typography"); const util_1 = require("../util"); const i18n_hooks_1 = require("../util/i18n/i18n.hooks"); const PaginationItem_1 = __importDefault(require("./PaginationItem")); const getSteps = ({ page, count, boundaryCount = 1, siblingCount = 1, }) => { var _a, _b; const range = (start, end) => Array.from({ length: end - start + 1 }, (_, i) => start + i); if (count <= (boundaryCount + siblingCount) * 2 + 3) return range(1, count); const startPages = range(1, boundaryCount); const endPages = range(count - boundaryCount + 1, count); const siblingsStart = Math.max(Math.min(page - siblingCount, count - boundaryCount - siblingCount * 2 - 1), boundaryCount + 2); const siblingsEnd = siblingsStart + siblingCount * 2; return [ ...startPages, siblingsStart - ((_a = startPages[startPages.length - 1]) !== null && _a !== void 0 ? _a : 0) === 2 ? siblingsStart - 1 : "ellipsis", ...range(siblingsStart, siblingsEnd), ((_b = endPages[0]) !== null && _b !== void 0 ? _b : count + 1) - siblingsEnd === 2 ? siblingsEnd + 1 : "ellipsis", ...endPages, ]; }; exports.getSteps = getSteps; /** * TODO: These classes can be removed in darkside update * - navds-pagination--prev-next--with-text * - navds-pagination__prev-next */ /** * A component that displays pagination controls. * * @see [📝 Documentation](https://aksel.nav.no/komponenter/core/pagination) * @see 🏷️ {@link PaginationProps} * * @example * ```jsx * <Pagination * page={pageState} * onPageChange={setPageState} * count={9} * boundaryCount={1} * siblingCount={1} * /> * ``` */ exports.Pagination = (0, react_1.forwardRef)((_a, ref) => { var { page, onPageChange, count, boundaryCount = 1, siblingCount = 1, className, size = "medium", prevNextTexts = false, srHeading, "aria-labelledby": ariaLabelledBy, renderItem: Item = PaginationItem_1.default, "data-color": color } = _a, rest = __rest(_a, ["page", "onPageChange", "count", "boundaryCount", "siblingCount", "className", "size", "prevNextTexts", "srHeading", "aria-labelledby", "renderItem", "data-color"]); const { cn } = (0, Theme_1.useRenameCSS)(); const headingId = (0, util_1.useId)(); const translate = (0, i18n_hooks_1.useI18n)("Pagination"); if (page < 1) { console.error("page cannot be less than 1"); return null; } if (count < 1) { console.error("count cannot be less than 1"); return null; } if (boundaryCount < 0) { console.error("boundaryCount cannot be less than 0"); return null; } if (siblingCount < 0) { console.error("siblingCount cannot be less than 0"); return null; } return (react_1.default.createElement("nav", Object.assign({ ref: ref }, rest, { "data-color": color, "aria-labelledby": srHeading ? (0, clsx_1.default)(headingId, ariaLabelledBy) : ariaLabelledBy, className: cn("navds-pagination", `navds-pagination--${size}`, className) }), srHeading && (react_1.default.createElement(typography_1.Heading, { size: "xsmall", visuallyHidden: true, as: srHeading.tag, id: headingId }, srHeading.text)), react_1.default.createElement("ul", { className: cn("navds-pagination__list") }, react_1.default.createElement("li", null, react_1.default.createElement(Item, { "data-color": color, className: cn("navds-pagination__prev-next", { "navds-pagination--invisible": page === 1, "navds-pagination--prev-next--with-text": prevNextTexts, }), disabled: page === 1, onClick: () => onPageChange === null || onPageChange === void 0 ? void 0 : onPageChange(page - 1), page: page - 1, size: size, icon: react_1.default.createElement(aksel_icons_1.ChevronLeftIcon, Object.assign({}, (prevNextTexts ? { "aria-hidden": true } : { title: translate("previous") }))) }, prevNextTexts && translate("previous"))), (0, exports.getSteps)({ page, count, siblingCount, boundaryCount }).map((step, i) => { const n = Number(step); return Number.isNaN(n) ? (react_1.default.createElement("li", { className: cn("navds-pagination__ellipsis"), key: `${step}${i}` }, react_1.default.createElement(typography_1.BodyShort, { size: size === "xsmall" ? "small" : size, as: "span" }, "..."))) : (react_1.default.createElement("li", { key: step }, react_1.default.createElement(Item, { "data-color": color, /* Remember to update RenderItemProps if you make changes to props sent into Item */ onClick: () => onPageChange === null || onPageChange === void 0 ? void 0 : onPageChange(n), selected: page === n, page: n, size: size }, n))); }), react_1.default.createElement("li", null, react_1.default.createElement(Item, { "data-color": color, className: cn("navds-pagination__prev-next", { "navds-pagination--invisible": page === count, "navds-pagination--prev-next--with-text": prevNextTexts, }), disabled: page === count, onClick: () => onPageChange === null || onPageChange === void 0 ? void 0 : onPageChange(page + 1), page: page + 1, size: size, icon: react_1.default.createElement(aksel_icons_1.ChevronRightIcon, Object.assign({}, (prevNextTexts ? { "aria-hidden": true } : { title: translate("next") }))), iconPosition: "right" }, prevNextTexts && translate("next")))))); }); exports.Pagination.Item = PaginationItem_1.default; exports.default = exports.Pagination; //# sourceMappingURL=Pagination.js.map