1.2.1 • Published 3 years ago

@bazhe/use-pagination v1.2.1

Weekly downloads
-
License
-
Repository
github
Last release
3 years ago

@bazhe/use-pagination

React hook for handling your pagination. Depends on: @bazhe/paginator

Playground/Demo: https://blagoj5.github.io/pagination/#playground
Full documentation: https://blagoj5.github.io/pagination

Getting Started

Prerequisites

You need to have react-dom and react above version 16

"react": ">=16.0.0",
"react-dom": ">=16.0.0"

Installing

npm

npm install @bazhe/use-pagination

yarn

yarn add @bazhe/use-pagination

Usage

Typescript usage example in: https://github.com/Blagoj5/pagination/blob/main/example/components/pagination-hook-demo.tsx

Import

import { usePagination } from '@bazhe/use-pagination';

Initialization

Syntax: usePagination(paginationOptions)
Returns: PaginationResult

export const PaginationHookDemo: React.FC = () => {
  const {
    paginationResult,
    nextPage,
    previousPage,
    setCurrentPage,
    setItems,
  } = usePagination({
    items: defaultItems,
    limit: 2,
  });

  return (
    <div>
      Pagination data:
      <pre>{JSON.stringify(paginationResult, null, 2)}</pre>
    </div>
  );
};

API

Parameters

usePagination(paginationOptions) accepts 1 parameter:

Pagination Options

NameTypeDefaultDescription
itemsT[] | undefined[]Initial items for the pagination
limitnumber | undefined4The limit of items per page
currentPagenumber | undefined1The current page
linksnumber | undefined10Links is the number of pages/links/buttons to display. Example: How many buttons you want to show in the pagination bar?
totalResultsnumber | undefineditems.lengthTotal results is the maximum number of items. Usually refers to items.length (so there's no need to pass it)

Pagination Result/Return

The result from usePagination hook:

  • paginationResult
NameTypeDescription
itemsT[] | undefinedThe items that will change depending on the pagination (currentPage, limit ...)
all_itemsT[] | undefinedInitial/All items for the pagination
total_pagesnumber | undefinedThe total pages for the provided items. Example: 12 items with limit 2 -> 6 total pages
pagesnumberThe number of pages between range(first_page, last_page)
current_pagenumberCurrent page
first_pagenumberFirst page, depends on the link (buttons/links to display in the pagination bar) and limit (items per page)
last_pagenumberLast page, depends on the link (buttons/links to display in the pagination bar) and limit (items per page)
previous_pagenumberPrevious page, current_page - 1
next_pagenumberNext page, current_page + 1
has_previous_pageboolean
has_next_pageboolean
total_resultsnumberThe length of the initial items
resultsnumberResults per page
first_resultnumberFirst result is the index of the item that's first for the current page - itemsfirst_result (depends on link and limit)
last_resultnumberLast result is the index of the item that's last for the current page - itemslast_result (depends on link and limit)
limitnumberThe limit of items per page
  • Pagination handlers for managing state
NameTypeDescription
setCurrentPage(n: number) => voidFunction handler for changing the current page
setItems(items: T[]) => voidFunction handler for changing the items
setPageAndItems(payload: { items: I[]; currentPage: number }) => voidFunction handler for changing current page and the items themselves
nextPage() => voidFunction handler for changing to next page
previousPage() => voidFunction handler for changing to previous page

Built With

Depends on:

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

  • Blagoj Petrov

Security

This project doesn’t have any security concerns.

License

This project is licensed under the MIT License - see the LICENSE.md file for details