Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@bazhe/with-pagination

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@bazhe/with-pagination

React Higher-Order Component for handling your pagination

  • 1.2.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5
increased by150%
Maintainers
1
Weekly downloads
 
Created
Source

@bazhe/with-pagination

React Higher-Order Component for handling your pagination

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/with-pagination

yarn

yarn add @bazhe/with-pagination

Usage

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

The example includes the implementation of withPagination for both class based and function based components

Import

import { withPagination } from '@bazhe/with-pagination';

Initialization

Syntax: withPagination(WrappedComponent, paginationOptions)
Returns: PaginationResult as props to the wrapped component

Function based components:

const fakeData = [1, 2, 3]


const FunctionalBasedComponent = ({
  paginationResult,
  nextPage,
  previousPage,
  setItems,
  setCurrentPage,
}: WithPaginationProps<typeof fakeData[0]>) => {
  // pass the typeof the element in this case items have number

  return (
    <div>
        The pagination result:
        <pre>{JSON.stringify(paginationResult)}</pre>
    </div>
    );
};

export const PaginationHocHookDemo = withPagination(FunctionalBasedComponent, {
  items: fakeData,
  limit: 2,
});

Class based components:

const fakeData = [1, 2, 3]

class ClassBasedComponent extends React.Component<
  WithPaginationProps<typeof fakeData[0]>
> {
  constructor(props) {
    super(props);
  }

  render() {
    const {
      nextPage,
      paginationResult,
      previousPage,
      setCurrentPage,
      setItems,
    } = this.props;

  return (
    <div>
        The pagination result:
        <pre>{JSON.stringify(paginationResult)}</pre>
    </div>
    );
  }
}



export const PaginationHocHookDemo = withPagination(ClassBasedComponent, {
  items: fakeData,
  limit: 2,
});

API

Parameters

withPagination(WrappedComponent, paginationOptions) accepts 2 parameters:

Wrapper Component
NameTypeDescription
WrappedComponentReact.ComponentType<T>The wrapped component that will recieve the pagination result as props
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

Expected pagination props to the wrapped component:

  • 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 - items[first_result] (depends on link and limit)
last_resultnumberLast result is the index of the item that's last for the current page - items[last_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

Keywords

FAQs

Package last updated on 16 May 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc