UNPKG

@payfit/unity-components

Version:

125 lines (124 loc) • 3.99 kB
import { PaginationJumpDialog as e } from "./parts/PaginationJumpDialog.js"; import { usePagination as t } from "./hooks/use-pagination.js"; import { PaginationEllipsis as n } from "./parts/PaginationEllipsis.js"; import { PaginationNavButton as r } from "./parts/PaginationNavButton.js"; import { RawPaginationLink as i } from "./parts/RawPaginationLink.js"; import { generatePaginationWithWindow as a } from "./utils/pagination-window.js"; import { forwardRef as o, useMemo as s, useRef as c } from "react"; import { uyTv as l } from "@payfit/unity-themes"; import { jsx as u, jsxs as d } from "react/jsx-runtime"; import { useTranslation as f } from "react-i18next"; import { useId as p } from "react-aria/useId"; import { useOverlayTriggerState as m } from "react-stately/useOverlayTriggerState"; //#region src/core/components/client-side-pagination/ClientSidePagination.tsx var h = l({ slots: { base: "uy:flex uy:gap-100 uy:relative", pagesList: "uy:flex uy:gap-100 uy:list-none uy:m-0 uy:p-0" } }), g = 7, _ = o(({ pageCount: o, defaultPage: l, currentPage: _, onPageHover: v, onPageChange: y, onNextPress: b, onPreviousPress: x, LinkComponent: S = i, ...C }, w) => { let T = p(), { i18n: E, t: D } = f("unity"), O = _ !== void 0 && y !== void 0 && l === void 0, { currentPage: k, goToPage: A } = t({ pageCount: o, initialPage: O ? _ : l, onPageChange: y, isControlled: O }), j = m({}), M = c(/* @__PURE__ */ new Map()), N = s(() => a(o, k, g), [o, k]), P = new Intl.NumberFormat(E.language, { notation: "standard" }), F = (e) => { A(e), j.close(), requestAnimationFrame(() => { let t = M.current.get(e); t && t.focus(); }); }, I = (e, t) => { switch (e.key) { case "ArrowLeft": case "ArrowUp": e.preventDefault(), t > 1 && (A(t - 1), requestAnimationFrame(() => { M.current.get(t - 1)?.focus(); })); break; case "ArrowRight": case "ArrowDown": e.preventDefault(), t < o && (A(t + 1), requestAnimationFrame(() => { M.current.get(t + 1)?.focus(); })); break; case "Home": e.preventDefault(), A(1), requestAnimationFrame(() => { M.current.get(1)?.focus(); }); break; case "End": e.preventDefault(), A(o), requestAnimationFrame(() => { M.current.get(o)?.focus(); }); break; } }, L = { paginationLabel: D((e) => e.unity.component.pagination.label), pageListLabel: D((e) => e.unity.component.pagination.pageList.label, { current: k, total: o }) }, { base: R, pagesList: z } = h(); return /* @__PURE__ */ d("nav", { "data-dd-privacy": "allow", ...C, ref: w, className: R(), "aria-labelledby": L.paginationLabel, children: [ /* @__PURE__ */ u(e, { pageCount: o, onJumpToPage: F, state: j }), /* @__PURE__ */ u(r, { variant: "previous", isDisabled: k === 1, onClick: () => { A("previous"), x?.(); } }), /* @__PURE__ */ u("ul", { className: z(), "aria-label": L.pageListLabel, children: N.map((e, t) => e.type === "page" ? /* @__PURE__ */ u("li", { "aria-setsize": o, "aria-posinset": e.value, children: /* @__PURE__ */ u(S, { ref: (t) => { t ? M.current.set(e.value, t) : M.current.delete(e.value); }, onPress: () => { A(e.value); }, onHoverChange: (t) => { t && v?.(e.value); }, onKeyDown: (t) => { I(t, e.value); }, isActive: e.value === k, value: e.value, children: P.format(e.value) }) }, `pagination-${T}-item-${t}`) : /* @__PURE__ */ u("li", { children: /* @__PURE__ */ u(n, { value: e.value, onKeyDown: (e) => { I(e, -1); }, onPress: () => { j.open(); } }) }, `pagination-${T}-item-${t}`)) }), /* @__PURE__ */ u(r, { variant: "next", isDisabled: k === o, onClick: () => { A("next"), b?.(); } }) ] }); }); _.displayName = "ClientSidePagination"; //#endregion export { _ as ClientSidePagination };