UNPKG

@stainless-code/react-paginate

Version:
124 lines (122 loc) 3.69 kB
"use strict"; "use client"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { getPageCount: () => getPageCount, paginate: () => paginate, usePaginate: () => usePaginate }); module.exports = __toCommonJS(src_exports); var import_react = require("react"); function usePaginate(items, options = {}) { const { initialPageSize = 25, initialPageIndex = 0, threshold = 0 } = options; const [pageSize, setPageSize] = (0, import_react.useState)(initialPageSize); const [pageIndex, setPageIndex] = (0, import_react.useState)(initialPageIndex); const pageCount = getPageCount(items, pageSize); const shouldPaginate = items.length > threshold; const pages = (0, import_react.useMemo)(() => { const pages2 = []; if (shouldPaginate) { for (let index = 0; index < pageCount; index++) { pages2.push(paginate(items, pageSize, index)); } } return pages2; }, [items, pageCount, pageSize, shouldPaginate]); const page = shouldPaginate ? pages[pageIndex] ?? [] : items; const firstPageIndex = 0; const lastPageIndex = safeIndex(pageCount - 1); const nextPageIndex = safeIndex(pageIndex + 1); const prevPageIndex = safeIndex(pageIndex - 1); const canFirstPage = pageIndex !== 0; const canLastPage = pageIndex !== lastPageIndex; const canNextPage = pageIndex < lastPageIndex; const canPrevPage = pageIndex > 0; (0, import_react.useEffect)(() => { if (pageIndex > lastPageIndex) { setPageIndex(lastPageIndex); } }, [pageIndex, lastPageIndex]); function safeIndex(index) { return Math.min(Math.max(index, 0), pageCount - 1); } function handleChangePageSize(size) { if (size !== pageSize) { setPageSize(size); resetPage(); } } function handleChangePage(index) { if (index !== pageIndex) { setPageIndex(safeIndex(index)); } } function firstPage() { setPageIndex(firstPageIndex); } function lastPage() { setPageIndex(lastPageIndex); } function nextPage() { setPageIndex(nextPageIndex); } function prevPage() { setPageIndex(prevPageIndex); } function resetPage() { setPageIndex(firstPageIndex); } return { canFirstPage, canLastPage, canNextPage, canPrevPage, changePage: handleChangePage, changePageSize: handleChangePageSize, firstPage, firstPageIndex, lastPage, lastPageIndex, nextPage, nextPageIndex, page, pageCount, pageIndex, pages, pageSize, prevPage, prevPageIndex, resetPage, shouldPaginate }; } function paginate(items, pageSize, pageIndex) { return items.slice(pageIndex * pageSize, (pageIndex + 1) * pageSize); } function getPageCount(items, pageSize) { return Math.ceil(items.length / pageSize); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { getPageCount, paginate, usePaginate });