@navikt/ds-react
Version:
React components from the Norwegian Labour and Welfare Administration.
103 lines • 5.81 kB
JavaScript
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;
};
import React, { forwardRef } from "react";
import { ChevronLeftIcon, ChevronRightIcon } from "@navikt/aksel-icons";
import { BodyShort, Heading } from "../typography/index.js";
import { useId } from "../utils-external/index.js";
import { cl } from "../utils/helpers/index.js";
import { useI18n } from "../utils/i18n/i18n.hooks.js";
import PaginationItem from "./PaginationItem.js";
export 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,
];
};
/**
* 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}
* />
* ```
*/
export const Pagination = forwardRef((_a, ref) => {
var { page, onPageChange, count, boundaryCount = 1, siblingCount = 1, className, size = "medium", prevNextTexts = false, srHeading, "aria-labelledby": ariaLabelledBy, renderItem: Item = PaginationItem, "data-color": color = "neutral" } = _a, rest = __rest(_a, ["page", "onPageChange", "count", "boundaryCount", "siblingCount", "className", "size", "prevNextTexts", "srHeading", "aria-labelledby", "renderItem", "data-color"]);
const headingId = useId();
const translate = 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.createElement("nav", Object.assign({ ref: ref }, rest, { "data-color": color, "aria-labelledby": srHeading ? cl(headingId, ariaLabelledBy) : ariaLabelledBy, className: cl("aksel-pagination", `aksel-pagination--${size}`, className) }),
srHeading && (React.createElement(Heading, { size: "xsmall", visuallyHidden: true, as: srHeading.tag, id: headingId }, srHeading.text)),
React.createElement("ul", { className: "aksel-pagination__list" },
React.createElement("li", null,
React.createElement(Item, { "data-color": color, className: cl({
"aksel-pagination--invisible": page === 1,
}), disabled: page === 1, onClick: () => onPageChange === null || onPageChange === void 0 ? void 0 : onPageChange(page - 1), page: page - 1, size: size, icon: React.createElement(ChevronLeftIcon, Object.assign({}, (prevNextTexts
? { "aria-hidden": true }
: { title: translate("previous") }))) }, prevNextTexts && translate("previous"))),
getSteps({ page, count, siblingCount, boundaryCount }).map((step, i) => {
const n = Number(step);
return Number.isNaN(n) ? (React.createElement("li", { className: "aksel-pagination__ellipsis", key: `${step}${i}` },
React.createElement(BodyShort, { size: size === "xsmall" ? "small" : size, as: "span" }, "..."))) : (React.createElement("li", { key: step },
React.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.createElement("li", null,
React.createElement(Item, { "data-color": color, className: cl({
"aksel-pagination--invisible": page === count,
}), disabled: page === count, onClick: () => onPageChange === null || onPageChange === void 0 ? void 0 : onPageChange(page + 1), page: page + 1, size: size, icon: React.createElement(ChevronRightIcon, Object.assign({}, (prevNextTexts
? { "aria-hidden": true }
: { title: translate("next") }))), iconPosition: "right" }, prevNextTexts && translate("next"))))));
});
Pagination.Item = PaginationItem;
export default Pagination;
//# sourceMappingURL=Pagination.js.map