├── .babelrc ├── .gitignore ├── .npmignore ├── LICENSE ├── README.md ├── demo ├── .babelrc ├── LICENSE ├── package-lock.json ├── package.json ├── src │ ├── App.jsx │ ├── index.html │ └── index.js └── webpack.config.js ├── package-lock.json ├── package.json ├── src └── JwPagination.jsx └── webpack.config.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | "react", 4 | "env", 5 | "stage-0" 6 | ] 7 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Component lib directory 2 | lib 3 | 4 | # Logs 5 | logs 6 | *.log 7 | npm-debug.log* 8 | 9 | # Runtime data 10 | pids 11 | *.pid 12 | *.seed 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # node-waf configuration 27 | .lock-wscript 28 | 29 | # Compiled binary addons (http://nodejs.org/api/addons.html) 30 | build/Release 31 | 32 | # Dependency directories 33 | node_modules 34 | jspm_packages 35 | typings 36 | 37 | # Optional npm cache directory 38 | .npm 39 | 40 | # Optional REPL history 41 | .node_repl_history -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | src 2 | .babelrc 3 | webpack.config.js 4 | demo -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Jason Watmore 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # jw-react-pagination 2 | 3 | React Pagination Component 4 | 5 | For documentation and demo go to http://jasonwatmore.com/post/2018/04/10/npm-jw-react-pagination-component -------------------------------------------------------------------------------- /demo/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | "react", 4 | "env", 5 | "stage-0" 6 | ] 7 | } -------------------------------------------------------------------------------- /demo/LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Jason Watmore 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /demo/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jw-react-pagination-demo", 3 | "license": "MIT", 4 | "scripts": { 5 | "start": "webpack-dev-server --open" 6 | }, 7 | "dependencies": { 8 | "jw-react-pagination": "^1.0.6", 9 | "prop-types": "^15.6.1", 10 | "react": "^16.3.1", 11 | "react-dom": "^16.3.1" 12 | }, 13 | "devDependencies": { 14 | "babel-core": "^6.26.0", 15 | "babel-loader": "^7.1.4", 16 | "babel-preset-env": "^1.6.1", 17 | "babel-preset-react": "^6.24.1", 18 | "babel-preset-stage-0": "^6.24.1", 19 | "html-webpack-plugin": "^3.2.0", 20 | "webpack": "^4.5.0", 21 | "webpack-cli": "^3.3.0", 22 | "webpack-dev-server": "^3.1.2" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /demo/src/App.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import Pagination from 'jw-react-pagination'; 3 | 4 | class App extends React.Component { 5 | constructor() { 6 | super(); 7 | 8 | // an example array of items to be paged 9 | var exampleItems = [...Array(150).keys()].map(i => ({ id: (i+1), name: 'Item ' + (i+1) })); 10 | 11 | this.state = { 12 | exampleItems: exampleItems, 13 | pageOfItems: [] 14 | }; 15 | 16 | // bind function in constructor instead of render (https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-no-bind.md) 17 | this.onChangePage = this.onChangePage.bind(this); 18 | } 19 | 20 | onChangePage(pageOfItems) { 21 | // update state with new page of items 22 | this.setState({ pageOfItems: pageOfItems }); 23 | } 24 | 25 | render() { 26 | return ( 27 |
28 |
29 |
30 |

React Pagination Component Demo

31 | {this.state.pageOfItems.map(item => 32 |
{item.name}
33 | )} 34 | 35 |
36 |
37 |
38 |
39 |

40 | npm - JW React Pagination Component 41 |

42 |

43 | JasonWatmore.com 44 |

45 |
46 |
47 | ); 48 | } 49 | } 50 | 51 | export default App; -------------------------------------------------------------------------------- /demo/src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | React Pagination Example with Logic Like Google 6 | 7 | 8 | 11 | 12 | 13 |
14 | 15 | -------------------------------------------------------------------------------- /demo/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | 5 | ReactDOM.render(, document.getElementById('app')); -------------------------------------------------------------------------------- /demo/webpack.config.js: -------------------------------------------------------------------------------- 1 | var HtmlWebpackPlugin = require('html-webpack-plugin'); 2 | 3 | module.exports = { 4 | resolve: { 5 | extensions: ['.js', '.jsx'] 6 | }, 7 | module: { 8 | rules: [ 9 | { 10 | test: /\.jsx?$/, 11 | exclude: /(node_modules)/, 12 | use: 'babel-loader' 13 | } 14 | ] 15 | }, 16 | plugins: [new HtmlWebpackPlugin({ 17 | template: './src/index.html', 18 | filename: 'index.html', 19 | inject: 'body' 20 | })] 21 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jw-react-pagination", 3 | "version": "1.1.0", 4 | "description": "React Pagination Component", 5 | "homepage": "http://jasonwatmore.com/post/2018/04/10/npm-jw-react-pagination-component", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/cornflourblue/jw-react-pagination.git" 9 | }, 10 | "main": "./lib/JwPagination.js", 11 | "license": "MIT", 12 | "scripts": { 13 | "build": "webpack" 14 | }, 15 | "peerDependencies": { 16 | "prop-types": "^15.6.0", 17 | "react": "^16.0.0", 18 | "react-dom": "^16.0.0" 19 | }, 20 | "devDependencies": { 21 | "babel-core": "^6.21.0", 22 | "babel-loader": "^7.1.4", 23 | "babel-preset-env": "^1.6.1", 24 | "babel-preset-react": "^6.16.0", 25 | "babel-preset-stage-0": "^6.24.1", 26 | "path": "^0.12.7", 27 | "prop-types": "^15.6.0", 28 | "react": "^16.0.0", 29 | "react-dom": "^16.0.0", 30 | "webpack": "^4.5.0", 31 | "webpack-cli": "^3.3.0" 32 | }, 33 | "dependencies": { 34 | "jw-paginate": "^1.0.2" 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/JwPagination.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import PropTypes from 'prop-types'; 3 | import paginate from 'jw-paginate'; 4 | 5 | const propTypes = { 6 | items: PropTypes.array.isRequired, 7 | onChangePage: PropTypes.func.isRequired, 8 | initialPage: PropTypes.number, 9 | pageSize: PropTypes.number, 10 | maxPages: PropTypes.number, 11 | labels: PropTypes.object, 12 | styles: PropTypes.object, 13 | disableDefaultStyles: PropTypes.bool 14 | } 15 | 16 | const defaultProps = { 17 | initialPage: 1, 18 | pageSize: 10, 19 | maxPages: 10, 20 | labels: { 21 | first: 'First', 22 | last: 'Last', 23 | previous: 'Previous', 24 | next: 'Next' 25 | } 26 | } 27 | 28 | class JwPagination extends React.Component { 29 | constructor(props) { 30 | super(props); 31 | this.state = { pager: {} }; 32 | this.styles = {}; 33 | 34 | if (!props.disableDefaultStyles) { 35 | this.styles = { 36 | ul: { 37 | margin: 0, 38 | padding: 0, 39 | display: 'inline-block' 40 | }, 41 | li: { 42 | listStyle: 'none', 43 | display: 'inline', 44 | textAlign: 'center' 45 | }, 46 | a: { 47 | cursor: 'pointer', 48 | padding: '6px 12px', 49 | display: 'block', 50 | float: 'left' 51 | } 52 | } 53 | } 54 | 55 | // merge custom styles with default styles 56 | if (props.styles) { 57 | this.styles = { 58 | ul: { ...this.styles.ul, ...props.styles.ul }, 59 | li: { ...this.styles.li, ...props.styles.li }, 60 | a: { ...this.styles.a, ...props.styles.a } 61 | }; 62 | } 63 | } 64 | 65 | componentWillMount() { 66 | // set page if items array isn't empty 67 | if (this.props.items && this.props.items.length) { 68 | this.setPage(this.props.initialPage); 69 | } 70 | } 71 | 72 | componentDidUpdate(prevProps, prevState) { 73 | // reset page if items array has changed 74 | if (this.props.items !== prevProps.items) { 75 | this.setPage(this.props.initialPage); 76 | } 77 | } 78 | 79 | setPage(page) { 80 | var { items, pageSize, maxPages } = this.props; 81 | var pager = this.state.pager; 82 | 83 | // get new pager object for specified page 84 | pager = paginate(items.length, page, pageSize, maxPages); 85 | 86 | // get new page of items from items array 87 | var pageOfItems = items.slice(pager.startIndex, pager.endIndex + 1); 88 | 89 | // update state 90 | this.setState({ pager: pager }); 91 | 92 | // call change page function in parent component 93 | this.props.onChangePage(pageOfItems); 94 | } 95 | 96 | render() { 97 | var pager = this.state.pager; 98 | var labels = this.props.labels; 99 | var styles = this.styles; 100 | 101 | if (!pager.pages || pager.pages.length <= 1) { 102 | // don't display pager if there is only 1 page 103 | return null; 104 | } 105 | 106 | return ( 107 | 126 | ); 127 | } 128 | } 129 | 130 | JwPagination.propTypes = propTypes; 131 | JwPagination.defaultProps = defaultProps; 132 | export default JwPagination; 133 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | var path = require('path'); 2 | 3 | module.exports = { 4 | entry: './src/JwPagination.jsx', 5 | output: { 6 | path: path.resolve('lib'), 7 | filename: 'JwPagination.js', 8 | libraryTarget: 'commonjs2' 9 | }, 10 | module: { 11 | rules: [ 12 | { 13 | test: /\.jsx?$/, 14 | exclude: /(node_modules)/, 15 | use: 'babel-loader' 16 | } 17 | ] 18 | } 19 | } --------------------------------------------------------------------------------