项目作者: makotot

项目描述 :
⚛️ React render props component & custom hook for pagination.
高级语言: TypeScript
项目地址: git://github.com/makotot/paginated.git
创建时间: 2021-01-12T17:56:33Z
项目社区:https://github.com/makotot/paginated

开源协议:MIT License

下载


DEPRECATED - no longer actively maintained.

Please use GhostUI instead.

" class="reference-link">Paginated CI

@makotot/paginated?style=for-the-badge" alt="npm">
@makotot/paginated?style=for-the-badge" alt="NPM">
@makotot/paginated?style=for-the-badge" alt="npm bundle size">
@makotot/paginated?style=for-the-badge" alt="npm bundle size">
GitHub contributors
semantic-release

React render props & custom hook for pagination.

Features

  • Headless component -
    There are no restrictions on the ui expression.
  • Style free - This lib only care about functionality of pagination.
  • Tested with RTL
  • Small size - @makotot/paginated"">https://bundlephobia.com/result?p=@makotot/paginated

Install

  1. npm i @makotot/paginated

Example

Usage

Render Props

  1. import { Paginated } from '@makotot/paginated'
  2. ...
  3. return (
  4. <Paginated currentPage={1} totalPage={10} siblingsSize={2} boundarySize={2}>
  5. {({ pages, currentPage, hasPrev, hasNext, getFirstBoundary, getLastBoundary, isPrevTruncated, isNextTruncated }) => (
  6. <div>
  7. {hasPrev() && <a href="#">prev</a>}
  8. {getFirstBoundary().map(boundary => <a href="#" key={boundary}>{boundary}</a>)}
  9. {isPrevTruncated && <span>...</span>}
  10. {pages.map(page => {
  11. return page === currentPage ? (
  12. <span key={page}>{page}</span>
  13. ) : (
  14. <a href="#" key={page}>{page}</a>
  15. );
  16. })}
  17. {isNextTruncated && <span>...</span>}
  18. {getLastBoundary().map(boundary => <a href="#" key={boundary}>{boundary}</a>)}
  19. {hasNext() && <a href="#">next</a>}
  20. </div>
  21. )}
  22. </Paginated>
  23. )

Hooks

  1. import { usePaginated } from '@makotot/paginated'
  2. ...
  3. const { pages, currentPage, hasPrev, hasNext, getFirstBoundary, getLastBoundary, isPrevTruncated, isNextTruncated } = usePaginated({ currentPage: 1, totalPage: 10, siblingsSize: 2, boundarySize: 2 });
  4. return (
  5. <div>
  6. {hasPrev() && <a href="#">prev</a>}
  7. {getFirstBoundary().map(boundary => <a href="#" key={boundary}>{boundary}</a>)}
  8. {isPrevTruncated && <span>...</span>}
  9. {pages.map(page => {
  10. return page === currentPage ? (
  11. <span key={page}>{page}</span>
  12. ) : (
  13. <a href="#" key={page}>{page}</a>
  14. );
  15. })}
  16. {isNextTruncated && <span>...</span>}
  17. {getLastBoundary().map(boundary => <a href="#" key={boundary}>{boundary}</a>)}
  18. {hasNext() && <a href="#">next</a>}
  19. </div>
  20. )

Options

currentPage

Type: number

The value of current page. Required.

totalPage

Type: number

The value of total page. Required.

siblingsSize

Type: number

The items size of one side of the middle of pagination.

boundarySize

Type: number

The items size of one side of the edge of pagination.

Returns (Hooks) | Props (Render Props)

pages

Type: number[]

The page items of the middle of pagination.

currentPage

Type: number

The value of current page.

hasPrev

Type: boolean

Returns true if previous page of the current page is exist.

hasNext

Type: boolean

Returns true if next page of the current page is exist.

getFirstBoundary

Type: () => number[]

Returns page items of first boundary.

getLastBoundary

Type: () => number[]

Returns page items of last boundary.

isPrevTruncated

Type: boolean

Returns true if pages before the current page is ellipsized.

isNextTruncated

Type: boolean

Returns true if pages after the current page is ellipsized.

Authors