• Stars
    star
    142
  • Rank 250,264 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple pagination for React (MIT)

build status

react-pagify - Simple pagination for React

react-pagify provides a simple API for building your own custom paginator.

If you want to learn more about React, read SurviveJS - Webpack and React.

Usage

react-pagify consists of four React components: Context, Segment, Button, and Ellipsis.

Pagination logic can be handled through a package, such as segmentize. react-pagify doesn't tie you to any particular solution by design, though.

Context and Segment

Context accepts two props: segments and onSelect. A segment in turn consists of an object (segmentName -> [pageNumbers]). Segment accepts matching segmentName through a prop and then constructs divs based on that. It also binds onSelect automatically so that when you click an individual page div, the page number will be passed to it as a first parameter. The second one matches with DOM event.

The example below binds centerPage through Context and Segment:

...

import Paginator from 'react-pagify';

...

<Paginator.Context className="pagify-pagination"
  segments={{
    centerPage: [4]
  }} onSelect={(page) => console.log(page)}>
  <Paginator.Segment field="centerPage" />
</Paginator.Context>

Both Context and Segment accept custom props so you can customize className and attach custom behavior as needed. In the latter case the custom props are applied per each page div generated.

Usage with segmentize

react-pagify doesn't deal with pagination logic itself. Instead, you can use another package, such as segmentize, to deal with it. The following example demonstrates basic integration:

...

import Paginator from 'react-pagify';
import segmentize from 'segmentize';

...

<Paginator.Context className="pagify-pagination"
  segments={segmentize({
    page: 1,
    pages: 4,
    sidePages: 2
  })} onSelect={(page) => console.log(page)}>
  <Paginator.Segment field="previousPages" />
  <Paginator.Segment field="centerPage" />
  <Paginator.Segment field="nextPages" />
</Paginator.Context>

The idea is the same as earlier. In this case we bind fields that segmentize outputs. Each of those fields contains an array of data. Refer to segmentize documentation for available options.

Button

Given it's handy to be able to implement previous and next buttons, there's a little shortcut just for that:

...

import Paginator from 'react-pagify';

...

<Paginator.Context className="pagify-pagination"
  segments={{
    centerPage: [currentPage]
  }} onSelect={(page) => console.log(page)}>
  <Paginator.Button page={currentPage - 1}>Previous</Paginator.Button>
  <Paginator.Button page={currentPage + 1}>Next</Paginator.Button>
</Paginator.Context>

Ellipsis

Given it can be handy to be able to display ellipsis between pages of a paginator, there's a small utility known as Ellipsis just for this. Internally it uses comparison logic based on given previousField and nextField props. If there is room between these fields (say the values are [1, 2] and [4, 5]), it will render ellipsis. You can customize the outlook by passing custom children to it. Consider the example below:

...

import Paginator from 'react-pagify';
import segmentize from 'segmentize';

...

<Paginator.Context className="pagify-pagination"
  segments={segmentize({
    page: 1,
    pages: 4,
    sidePages: 2
  })} onSelect={(page) => console.log(page)}>
  <Paginator.Segment field="beginPages" />

  <Paginator.Ellipsis className="ellipsis"
    previousField="beginPages" nextField="previousPages">
    ***
  </Paginator.Ellipsis>

  <Paginator.Segment field="previousPages" />
  <Paginator.Segment field="centerPage" />
  <Paginator.Segment field="nextPages" />

  <Paginator.Ellipsis previousField="nextPages" nextField="endPages" />

  <Paginator.Segment field="endPages" />
</Paginator.Context>

See demo/ for a full implementation of the ideas discussed.

Customize Tags

By default react-pagify uses divs for container, segments and ellipses, and spans for links. You can customize these tags and define custom props for htme:

...

import Paginator from 'react-pagify';

...

<Paginator.Context className="pagination"
  tags={{
    container: {
      tag: 'ul'
    },
    segment: {
      tag: 'li'
    },
    ellipsis: {
      tag: 'li'
    },
    link: {
      tag: 'a',
      props: {
        href: '#'
      }
    }
  }}
  segments={{
    centerPage: [4]
  }} onSelect={(page) => console.log(page)}>
  <Paginator.Segment field="centerPage" />
</Paginator.Context>

Related Projects

Contributors

  • rowbare - Allowed usage in Bootstrap by making className customizable.
  • Nadav Spiegelman - Added optional ellipsesClassName prop, showPrevNext prop.
  • Nick Zarczynski - Added configuration to always show prev/next buttons and allowed inactive buttons to be styled.
  • Nimi Wariboko Jr. - Added support for activeClassName.
  • Artem Sapegin - Added Added ellipsisButton and sidePages props. Allowed paginator tags to be customized (important for Bootstrap).
  • Frederic Heem - Moved lodash to peerDependencies.
  • Alexander Ryzhikov - Fixed global lodash import.

License

react-pagify is available under MIT. See LICENSE for more details.

More Repositories

1

jswiki

JavaScript wiki. Focuses mainly on JS/HTML5/WebGL related tech.
JavaScript
3,913
star
2

colorjoe

Scaleable color picker with touch and AMD support (MIT)
JavaScript
561
star
3

Chart.js.legend

**DEPRECATED** Legend plugin for Chart.js (MIT)
JavaScript
114
star
4

grunt-umd

Surrounds code with the universal module definition (MIT)
JavaScript
96
star
5

ghw

Converts your GitHub wiki to gh-pages (MIT)
CSS
63
star
6

invoice-frontend

Invoicing application (MIT)
JavaScript
50
star
7

setjs

Set data structure for JavaScript (MIT)
JavaScript
47
star
8

dragjs

Simple utility to make it easier to implement drag based things (ie. sliders and such) (MIT)
TypeScript
45
star
9

generate-invoice

Generates PDF invoices (MIT)
JavaScript
36
star
10

node-configuration-patterns

Node.js configuration patterns (MIT)
JavaScript
30
star
11

segmentize

Simple segmentation useful for pagination (MIT)
JavaScript
28
star
12

react-ellipsify

Ellipsify content (MIT)
JavaScript
27
star
13

highlight-loader

Applies highlight.js to given HTML (MIT)
JavaScript
25
star
14

swagger-todo

Minimal Todo API example using Swagger on top of Node.js (Express+Sequelize+swagger-tools+jwt) (MIT)
JavaScript
24
star
15

colorjs

Color lib for JS (supports rgba, hsva, hsla via a simple API) (MIT)
JavaScript
20
star
16

yeswejekyll

Yes We Jekyll. Or if we don't yet, we will after reading this guide. Read on.
JavaScript
19
star
17

blogger2ghost

Blogspot JSON migrator plugin for Ghost (MIT)
JavaScript
18
star
18

pingdom-api

Pingdom API client for Node.js (MIT)
JavaScript
17
star
19

react-crm-backend

Backend for CRM (MIT)
JavaScript
15
star
20

react-crm-frontend

Frontend for CRM (MIT)
JavaScript
14
star
21

jquery.ellipsis

Ellipsis plugin for jQuery (MIT)
JavaScript
14
star
22

mocss

CSS preprocessor written in Node. Beta quality, wrote this originally for a blogpost. (MIT)
JavaScript
13
star
23

png2ico

Converts pngs to icos (MIT)
JavaScript
13
star
24

libumd

Wraps given JavaScript code with UMD (MIT)
JavaScript
10
star
25

jquery.caro

Simple, scaleable carousels for jQuery (MIT)
JavaScript
9
star
26

suite.js

Minimalistic testing tool for Node. Supports generative tests (aka QuickCheck). (MIT)
JavaScript
8
star
27

funkit

Various utilities (MIT)
JavaScript
7
star
28

bunit.js

Simple testing lib for JS (MIT)
JavaScript
7
star
29

Placidity

Placidity is an interactive interpreter with simple plugin architecture developed as a part of tutorial series focusing on testing related concepts.
Python
7
star
30

canifont

Figure out what fonts to use (MIT)
JavaScript
6
star
31

yabox.js

Yet another lightbox clone. Very light one this time. (MIT)
JavaScript
6
star
32

reactabular

Redirect to reactabular.js.org
HTML
5
star
33

react-ghfork

GitHub fork image for React (MIT)
CSS
5
star
34

iterplus

Utilities that complement Python's itertools
Python
5
star
35

blog-utils

Misc. utils making it easier to write blog posts (MIT)
JavaScript
5
star
36

replace-project-meta

Replaces project meta from package.json/README.md
JavaScript
5
star
37

speccer

Specification based test runner for Python (MIT)
Python
5
star
38

webpack-add-dependency-plugin

Add dependencies to webpack dev process to watch
TypeScript
4
star
39

jsassert

Chaining assert for JavaScript. Left here more as an example. Currently developed as a part of https://github.com/bebraw/bunit.js .
JavaScript
4
star
40

canvas-dataview-upload

Canvas DataView upload demo (MIT)
JavaScript
4
star
41

readme2gh

Updates your gh sites pages based on README.md (MIT)
JavaScript
4
star
42

styleguidist-ts-sc

Styleguidist demos with TypeScript and Styled Components with Cypress for acceptance testing
JavaScript
4
star
43

schema2object

Generate random objects based on JSON Schema (MIT)
JavaScript
4
star
44

lte-scraper

Scrapes LTE data from Apple and Wikipedia and then joins them (MIT)
JavaScript
4
star
45

qdemo

Demonstration generator for jQuery plugins (MIT)
JavaScript
4
star
46

swagger2client

JavaScript client for Swagger 2.0 (MIT)
JavaScript
4
star
47

turtles.js

Implementation of Turtle Graphics in JavaScript
JavaScript
3
star
48

juke

Simple jukebox using MPD backend
JavaScript
3
star
49

webshotter

Capture multiple screenshots per website (MIT)
JavaScript
3
star
50

f500-scraper

Example of a scraper (MIT)
JavaScript
3
star
51

jsrunner

Simple test runner (works well with jsassert). Left here more as an example. Currently developed as a part of https://github.com/bebraw/bunit.js .
JavaScript
3
star
52

rjs-build-demo

Demo of RequireJS build (weird output)
JavaScript
3
star
53

jquery.tokko.js

Generates a table of contents and anchors based on document headers
JavaScript
3
star
54

deno-demos

Deno demos for ViennaJS coding session (2021-01-27)
TypeScript
3
star
55

reload.js

Reloads page based on given interval (handy for testing esp.) (MIT)
JavaScript
3
star
56

narrative.js

This utility makes it easier to write narrative scripts for Node (MIT)
JavaScript
3
star
57

mojs

JS preprocessor (adds some extra syntax) (MIT)
JavaScript
3
star
58

check-markdown-links

Checks links of Markdown files in the given directory
JavaScript
3
star
59

jsopo

Precompiler providing operator overloading for JavaScript (proof of concept). This project is obsolete and preserved only for learning purposes! See http://nixtu.blogspot.com/2011/05/using-jsshaper-to-provide-operator.html to see how to achieve the same using JSShaper.
Python
3
star
60

sandboxy

Minimal sandbox for JavaScript (MIT)
JavaScript
2
star
61

bebraw

2
star
62

jkl-scraper

JavaScript
2
star
63

pynu

Pynu - Python Node Utilities (MIT)
Python
2
star
64

jsdelivr-in-5mins

Intro to jsDelivr in five minutes
JavaScript
2
star
65

dotfiles

Just some of my dotfiles
Vim Script
2
star
66

django-navi

Hierarchical navigation plugin for Django
Python
2
star
67

pyqa

Configuration generator (user input -> nice conf) (MIT)
Python
2
star
68

really-brief-introduction-to-html5

Presentation slides (still WIP)
1
star
69

really-brief-introduction-to-javascript

Presentation slides
1
star
70

recipe-scraper

Scrapes Wikia food recipes (MIT)
JavaScript
1
star
71

react-hot-starter

Hot starting point for React apps (MIT)
JavaScript
1
star
72

props2query

React props to GraphQL queries prototype. Nothing to see here yet.
TypeScript
1
star
73

rekanban

Experimental Kanban app, missing UI atm (MIT)
JavaScript
1
star
74

jquery.center.js

Centering plugin for jQuery (MIT)
JavaScript
1
star
75

tests-in-modules

Jest tests in modules (poc)
JavaScript
1
star
76

t1000

Handles termination of your Node.js server tidily (MIT)
JavaScript
1
star
77

cassopi

Automatically exported from code.google.com/p/cassopi
Python
1
star
78

cv

My personal resume.
HTML
1
star
79

redirect-webpack-plugin

Write redirects using webpack
JavaScript
1
star
80

nixtu

Nixtu Blog - Source Code
Python
1
star
81

resolve-swagger-refs

Resolves Swagger 2.0 definition references (MIT)
JavaScript
1
star
82

geekprofile-frontend

Geek Profile frontend
JavaScript
1
star
83

really-brief-introduction-to-django

Presentation slides + support material
Python
1
star
84

swaggerify

Swagger definition helpers for Node.js (MIT)
JavaScript
1
star
85

react-pure-button

React wrapper for Pure buttons (MIT)
JavaScript
1
star
86

parse-env

Parses configuration from env
JavaScript
1
star
87

html-webpack-remark-plugin

Render Markdown to React through Remark (MIT)
JavaScript
1
star
88

react-pure-menu

React wrapper for Pure menus (MIT)
JavaScript
1
star
89

playwright-demo

Tiny playwright demo
JavaScript
1
star
90

grunt-adapter

Executes grunt plugins without grunt itself (MIT)
JavaScript
1
star
91

stalbum

Static album generator (MIT)
JavaScript
1
star
92

fs-demo

Webpack fs bug
JavaScript
1
star
93

react-sidecar

React wrapper for Gitter sidecar (MIT)
JavaScript
1
star
94

parsegh

Parses url/git string and returns associated GitHub user and repository (MIT)
JavaScript
1
star
95

really-brief-introduction-to-python

Presentation slides
JavaScript
1
star
96

tinytest

py.test inspired unit testing library for PHP (MIT)
PHP
1
star
97

aoc21

Advent of code 2021
TypeScript
1
star
98

ioscript.js

ioscript.js makes it easy to write IO scripts in Node
JavaScript
1
star
99

spyder

Web indexer and scraper runner (MIT)
JavaScript
1
star
100

taskist

Glues your project configuration and tasks together (MIT)
JavaScript
1
star