1.0.0 • Published 8 months ago

@stainless-code/react-paginate v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

@stainless-code/react-paginate

Handle client-side pagination effortlessly. Simplifies the process of paginating data in your React applications, offering a fully type-safe and feature-rich experience.

Features

  • Customizable Pagination: Control the page size, initial page, and thresholds with ease.
  • Performance Optimized: Uses React's useMemo to optimize calculations.
  • Type-safe: Full TypeScript support for better developer experience.
  • Flexible API: Exposes powerful utilities to navigate and manipulate pagination state.

Installation

npm

npm install @stainless-code/react-paginate

yarn

yarn add @stainless-code/react-paginate

pnpm

pnpm add @stainless-code/react-paginate

bun

bun add @stainless-code/react-paginate

Usage

Here's a basic example of using usePaginate:

import { usePaginate } from "@stainless-code/react-paginate";
import React from "react";

const Example = () => {
  const items = Array.from({ length: 50 }, (_, i) => `Item ${i + 1}`);
  const {
    page,
    pageSize,
    pageIndex,
    pageCount,
    canNextPage,
    canPrevPage,
    nextPage,
    prevPage,
    changePageSize,
  } = usePaginate(items, { initialPageSize: 10, initialPageIndex: 0 });

  return (
    <div>
      <h1>Pagination Example</h1>
      <ul>
        {page.map((item, idx) => (
          <li key={idx}>{item}</li>
        ))}
      </ul>
      <div>
        <button onClick={prevPage} disabled={!canPrevPage}>
          Previous
        </button>
        <button onClick={nextPage} disabled={!canNextPage}>
          Next
        </button>
      </div>
      <p>
        Page {pageIndex + 1} of {pageCount}
      </p>
      <label>
        Items per page:{" "}
        <select
          value={pageSize}
          onChange={(e) => changePageSize(Number(e.target.value))}
        >
          <option value={5}>5</option>
          <option value={10}>10</option>
          <option value={25}>25</option>
        </select>
      </label>
    </div>
  );
};

export default Example;

Typesafety

This library is built with TypeScript and offers full type-safety. All APIs and returned values are strongly typed, ensuring a great developer experience and reducing runtime errors.

API

usePaginate(items: T[], options?: UsePaginateOptions)

Parameters

ParameterTypeDefaultDescription
itemsT[]-The list of items to paginate.
optionsobject{}Configuration options for pagination.
options.initialPageSizenumber25Initial number of items per page.
options.initialPageIndexnumber0Initial page index.
options.thresholdnumber0Minimum number of items required to enable pagination.

Returns

PropertyTypeDescription
pageT[]The current page of items.
pageSizenumberThe number of items per page.
pageIndexnumberThe current page index.
pageCountnumberTotal number of pages.
canNextPagebooleanWhether navigation to the next page is possible.
canPrevPagebooleanWhether navigation to the previous page is possible.
nextPage() => voidNavigate to the next page.
prevPage() => voidNavigate to the previous page.
firstPage() => voidNavigate to the first page.
lastPage() => voidNavigate to the last page.
changePage(index: number) => voidChange to a specific page by index.
changePageSize(size: number) => voidChange the number of items per page.
resetPage() => voidReset to the first page.
shouldPaginatebooleanWhether pagination is enabled based on the threshold.

paginate<T>(items: T[], pageSize: number, pageIndex: number): T[]

This function divides the given list of items into pages, and returns the items for the specified page.

  • items: An array of items (type T[]) that you want to paginate.
  • pageSize: The number of items per page.
  • pageIndex: The zero-based index of the page to return.

Example Usage:

const items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const pageSize = 3;
const pageIndex = 2;

const pageItems = paginate(items, pageSize, pageIndex);
// Output: [7, 8, 9]

getPageCount<T>(items: T[], pageSize: number): number

This function calculates the total number of pages required to paginate the given list of items, based on the page size.

  • items: An array of items (type T[]) to paginate.
  • pageSize: The number of items per page.

Example Usage:

const items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const pageSize = 3;

const totalPages = getPageCount(items, pageSize);
// Output: 4

Contributing

Feel free to submit issues or pull requests to improve the library. Every bit of help is appreciated. 💖

Read the contribution guidelines.

License

MIT