UNPKG

7.17 kBMarkdownView Raw
1
2# React Paginating
3
4[![Build Status](https://travis-ci.org/ChoTotOSS/react-paginating.svg?branch=master)](https://travis-ci.org/ChoTotOSS/react-paginating) [![CircleCI](https://circleci.com/gh/davidnguyen179/react-paginating.svg?style=svg)](https://circleci.com/gh/davidnguyen179/react-paginating)
5
6
7[![CircleCI](https://img.shields.io/npm/dm/react-paginating.svg)](https://img.shields.io/npm/dm/react-paginating.svg) [![codecov](https://codecov.io/gh/ChoTotOSS/react-paginating/branch/master/graph/badge.svg)](https://codecov.io/gh/ChoTotOSS/react-paginating) [![npm version](https://img.shields.io/npm/v/react-paginating.svg?style=flat-square)](https://badge.fury.io/js/react-paginating) [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/ChoTotOSS/react-paginating/blob/master/LICENSE) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
8
9[![module formats](http://img.badgesize.io/https://unpkg.com/react-paginating@1.2.1/dist/react-paginating.umd.min.js?compression=gzip&label=gzip%20size&style=flat-square)](https://unpkg.com/react-paginating@1.2.1/dist/) [![module formats](https://img.shields.io/badge/module%20formats-umd,%20cjs,%20es-green.svg?style=flat-square)](https://unpkg.com/react-paginating@1.2.1/dist/)
10
11
12Simple lightweight pagination component.
13
14<div align="center">
15 <img src="https://user-images.githubusercontent.com/6290720/33229010-d65daf4a-d1f8-11e7-851a-7d3e0d454b48.gif" />
16 <br />
17 <br />
18</div>
19
20The component applied `Function as Child Components` pattern. (You can read more
21about this pattern
22[here](https://medium.com/merrickchristensen/function-as-child-components-5f3920a9ace9)).
23
24This approach allows you to fully control UI component and behaviours.
25
26> See [the intro blog post](https://medium.com/chotot/introducing-react-paginating-9128f30f1f6b)
27
28## Table content
29
30* [Installation](https://github.com/ChoTotOSS/react-paginating#installation)
31* [Usage](https://github.com/ChoTotOSS/react-paginating#usage)
32* [Examples](https://github.com/ChoTotOSS/react-paginating#examples)
33* [Input Props](https://github.com/ChoTotOSS/react-paginating#input-props)
34* [Child callback functions](https://github.com/ChoTotOSS/react-paginating#child-callback-functions)
35* [Controlled Props](https://github.com/ChoTotOSS/react-paginating#controlled-props)
36* [Alternatives](https://github.com/ChoTotOSS/react-paginating#alternatives)
37
38## Installation
39
40```bash
41npm install --save react-paginating
42```
43
44or
45
46```bash
47yarn add react-paginating
48```
49
50## Usage
51
52You can check out the basic demo here:
53[https://codesandbox.io/s/z2rr7z23ol](https://codesandbox.io/s/z2rr7z23ol)
54
55```js
56import React from 'react';
57import { render } from 'react-dom';
58import Pagination from 'react-paginating';
59
60const fruits = [
61 ['apple', 'orange'],
62 ['banana', 'avocado'],
63 ['coconut', 'blueberry'],
64 ['payaya', 'peach'],
65 ['pear', 'plum']
66];
67const limit = 2;
68const pageCount = 3;
69const total = fruits.length * limit;
70
71class App extends React.Component {
72 constructor() {
73 super();
74 this.state = {
75 currentPage: 1
76 };
77 }
78
79 handlePageChange = (page, e) => {
80 this.setState({
81 currentPage: page
82 });
83 };
84
85 render() {
86 const { currentPage } = this.state;
87 return (
88 <div>
89 <ul>
90 {fruits[currentPage - 1].map(item => <li key={item}>{item}</li>)}
91 </ul>
92 <Pagination
93 total={total}
94 limit={limit}
95 pageCount={pageCount}
96 currentPage={currentPage}
97 >
98 {({
99 pages,
100 currentPage,
101 hasNextPage,
102 hasPreviousPage,
103 previousPage,
104 nextPage,
105 totalPages,
106 getPageItemProps
107 }) => (
108 <div>
109 <button
110 {...getPageItemProps({
111 pageValue: 1,
112 onPageChange: this.handlePageChange
113 })}
114 >
115 first
116 </button>
117
118 {hasPreviousPage && (
119 <button
120 {...getPageItemProps({
121 pageValue: previousPage,
122 onPageChange: this.handlePageChange
123 })}
124 >
125 {'<'}
126 </button>
127 )}
128
129 {pages.map(page => {
130 let activePage = null;
131 if (currentPage === page) {
132 activePage = { backgroundColor: '#fdce09' };
133 }
134 return (
135 <button
136 {...getPageItemProps({
137 pageValue: page,
138 key: page,
139 style: activePage,
140 onPageChange: this.handlePageChange
141 })}
142 >
143 {page}
144 </button>
145 );
146 })}
147
148 {hasNextPage && (
149 <button
150 {...getPageItemProps({
151 pageValue: nextPage,
152 onPageChange: this.handlePageChange
153 })}
154 >
155 {'>'}
156 </button>
157 )}
158
159 <button
160 {...getPageItemProps({
161 pageValue: totalPages,
162 onPageChange: this.handlePageChange
163 })}
164 >
165 last
166 </button>
167 </div>
168 )}
169 </Pagination>
170 </div>
171 );
172 }
173}
174
175render(<App />, document.getElementById('root'));
176```
177
178## Examples
179
180* [with simple basic data](https://github.com/ChoTotOSS/react-paginating/tree/master/examples/withBasic)
181* [with redux & query param from url](https://github.com/ChoTotOSS/react-paginating/tree/master/examples/withNextJSRedux)
182
183## Input Props
184
185### total
186
187> `number`
188
189Total results
190
191### limit
192
193> `number`
194
195Number of results per page
196
197### pageCount
198
199> `number`
200
201How many pages number you want to display in pagination zone.
202
203### currentPage
204
205> `number`
206
207Current page number
208
209## Child callback functions
210
211### getPageItemProps
212
213> `function({ pageValue: number, onPageChange: func })`
214
215Allow to pass props and event to page item. When page is clicked, `onPageChange`
216will be executed with param value `pageValue`.
217
218**Note:** This callback function should only use for paging with state change.
219If you prefer parsing page value from `query` url (**Please don't use this
220callback function**).
221
222## Controlled Props
223
224### pages
225
226> `array: [number]`
227
228List of pages number will be displayed. E.g: [1, 2, 3, 4, 5]
229
230### currentPage
231
232> `number`
233
234### previousPage
235
236> `number`
237
238### nextPage
239
240> `number`
241
242### totalPages
243
244> `number`
245
246### hasNextPage
247
248> `boolean`
249
250Check if it has `next page` or not.
251
252### hasPreviousPage
253
254> `boolean`
255
256Check if it has `previous page` or not.
257
258## Alternatives
259
260If you don’t agree with the choices made in this project, you might want to
261explore alternatives with different tradeoffs. Some of the more popular and
262actively maintained ones are:
263
264* [react-paginate](https://github.com/AdeleD/react-paginate)