├── .eslintcache
├── .gitignore
├── README.md
├── package-lock.json
├── package.json
├── public
├── favicon.ico
├── index.html
├── logo192.png
├── logo512.png
├── manifest.json
└── robots.txt
├── screenshot-localhost_3000-2021.01.05-12_01_43.png
├── screenshot-localhost_3000-2021.01.05-12_02_22 (1).png
├── screenshot-localhost_3000-2021.01.05-12_02_22.png
└── src
├── App.css
├── App.js
├── App.test.js
├── components
├── PaginationComponent.js
└── style.css
├── index.css
├── index.js
├── logo.svg
├── reportWebVitals.js
└── setupTests.js
/.eslintcache:
--------------------------------------------------------------------------------
1 | [{"C:\\Users\\khush\\Desktop\\Pagination\\react-pagination-component\\src\\index.js":"1","C:\\Users\\khush\\Desktop\\Pagination\\react-pagination-component\\src\\reportWebVitals.js":"2","C:\\Users\\khush\\Desktop\\Pagination\\react-pagination-component\\src\\App.js":"3","C:\\Users\\khush\\Desktop\\Pagination\\react-pagination-component\\src\\components\\PaginationComponent.js":"4"},{"size":500,"mtime":499162500000,"results":"5","hashOfConfig":"6"},{"size":362,"mtime":499162500000,"results":"7","hashOfConfig":"6"},{"size":293,"mtime":1609666028785,"results":"8","hashOfConfig":"6"},{"size":3335,"mtime":1609668159518,"results":"9","hashOfConfig":"6"},{"filePath":"10","messages":"11","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"12"},"yafc53",{"filePath":"13","messages":"14","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"usedDeprecatedRules":"12"},{"filePath":"15","messages":"16","errorCount":0,"warningCount":1,"fixableErrorCount":0,"fixableWarningCount":0,"source":"17","usedDeprecatedRules":"12"},{"filePath":"18","messages":"19","errorCount":0,"warningCount":5,"fixableErrorCount":0,"fixableWarningCount":0,"source":"20"},"C:\\Users\\khush\\Desktop\\Pagination\\react-pagination-component\\src\\index.js",[],["21","22"],"C:\\Users\\khush\\Desktop\\Pagination\\react-pagination-component\\src\\reportWebVitals.js",[],"C:\\Users\\khush\\Desktop\\Pagination\\react-pagination-component\\src\\App.js",["23"],"import logo from \"./logo.svg\";\nimport \"./App.css\";\nimport PaginationComponent from \"./components/PaginationComponent\";\n\nfunction App() {\n return (\n
\n );\n}\n\nexport default App;\n","C:\\Users\\khush\\Desktop\\Pagination\\react-pagination-component\\src\\components\\PaginationComponent.js",["24","25","26","27","28"],"import React, { useEffect, useState } from \"react\";\r\nimport \"./style.css\";\r\nconst renderData = (data) => {\r\n return (\r\n \r\n {data.map((todo, index) => {\r\n return - {todo.title}
;\r\n })}\r\n
\r\n );\r\n};\r\n\r\nfunction PaginationComponent() {\r\n const [data, setData] = useState([]);\r\n\r\n const [currentPage, setcurrentPage] = useState(1);\r\n const [itemsPerPage, setitemsPerPage] = useState(5);\r\n\r\n const [pageNumberLimit, setpageNumberLimit] = useState(5);\r\n const [maxPageNumberLimit, setmaxPageNumberLimit] = useState(5);\r\n const [minPageNumberLimit, setminPageNumberLimit] = useState(0);\r\n\r\n const handleClick = (event) => {\r\n setcurrentPage(Number(event.target.id));\r\n };\r\n\r\n const pages = [];\r\n for (let i = 1; i <= Math.ceil(data.length / itemsPerPage); i++) {\r\n pages.push(i);\r\n }\r\n\r\n const indexOfLastItem = currentPage * itemsPerPage;\r\n const indexOfFirstItem = indexOfLastItem - itemsPerPage;\r\n const currentItems = data.slice(indexOfFirstItem, indexOfLastItem);\r\n\r\n const renderPageNumbers = pages.map((number) => {\r\n if (number < maxPageNumberLimit + 1 && number > minPageNumberLimit) {\r\n return (\r\n \r\n {number}\r\n \r\n );\r\n } else {\r\n return null;\r\n }\r\n });\r\n\r\n useEffect(() => {\r\n fetch(\"https://jsonplaceholder.typicode.com/todos\")\r\n .then((response) => response.json())\r\n .then((json) => setData(json));\r\n }, []);\r\n\r\n const handleNextbtn = () => {\r\n setcurrentPage(currentPage + 1);\r\n\r\n if (currentPage + 1 > maxPageNumberLimit) {\r\n setmaxPageNumberLimit(maxPageNumberLimit + pageNumberLimit);\r\n setminPageNumberLimit(minPageNumberLimit + pageNumberLimit);\r\n }\r\n };\r\n\r\n const handlePrevbtn = () => {\r\n setcurrentPage(currentPage - 1);\r\n\r\n if ((currentPage - 1) % pageNumberLimit == 0) {\r\n setmaxPageNumberLimit(maxPageNumberLimit - pageNumberLimit);\r\n setminPageNumberLimit(minPageNumberLimit - pageNumberLimit);\r\n }\r\n };\r\n\r\n let pageIncrementBtn = null;\r\n if (pages.length > maxPageNumberLimit) {\r\n pageIncrementBtn = … ;\r\n }\r\n\r\n let pageDecrementBtn = null;\r\n if (minPageNumberLimit >= 1) {\r\n pageDecrementBtn = … ;\r\n }\r\n\r\n const handleLoadMore = () => {\r\n setitemsPerPage(itemsPerPage + 5);\r\n };\r\n\r\n return (\r\n <>\r\n Todo List
\r\n {renderData(currentItems)}\r\n \r\n - \r\n \r\n
\r\n {pageDecrementBtn}\r\n {renderPageNumbers}\r\n {pageIncrementBtn}\r\n\r\n - \r\n \r\n
\r\n
\r\n \r\n >\r\n );\r\n}\r\n\r\nexport default PaginationComponent;\r\n",{"ruleId":"29","replacedBy":"30"},{"ruleId":"31","replacedBy":"32"},{"ruleId":"33","severity":1,"message":"34","line":1,"column":8,"nodeType":"35","messageId":"36","endLine":1,"endColumn":12},{"ruleId":"33","severity":1,"message":"37","line":19,"column":27,"nodeType":"35","messageId":"36","endLine":19,"endColumn":45},{"ruleId":"38","severity":1,"message":"39","line":43,"column":34,"nodeType":"40","messageId":"41","endLine":43,"endColumn":36},{"ruleId":"38","severity":1,"message":"39","line":71,"column":45,"nodeType":"40","messageId":"41","endLine":71,"endColumn":47},{"ruleId":"38","severity":1,"message":"39","line":99,"column":35,"nodeType":"40","messageId":"41","endLine":99,"endColumn":37},{"ruleId":"38","severity":1,"message":"39","line":111,"column":35,"nodeType":"40","messageId":"41","endLine":111,"endColumn":37},"no-native-reassign",["42"],"no-negated-in-lhs",["43"],"no-unused-vars","'logo' is defined but never used.","Identifier","unusedVar","'setpageNumberLimit' is assigned a value but never used.","eqeqeq","Expected '===' and instead saw '=='.","BinaryExpression","unexpected","no-global-assign","no-unsafe-negation"]
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2 |
3 | # dependencies
4 | /node_modules
5 | /.pnp
6 | .pnp.js
7 |
8 | # testing
9 | /coverage
10 |
11 | # production
12 | /build
13 |
14 | # misc
15 | .DS_Store
16 | .env.local
17 | .env.development.local
18 | .env.test.local
19 | .env.production.local
20 |
21 | npm-debug.log*
22 | yarn-debug.log*
23 | yarn-error.log*
24 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Build Pagination component from scratch in React JS
2 |
3 | This repository contains code for pagination component in reactjs.
4 |
5 | If you want to learn how to create it please follow below tutorial:
6 | https://youtu.be/6DtBw3PaeHs
7 |
8 | Images:
9 |
10 | 
11 |
12 | 
13 |
14 |
15 |
16 |
17 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
18 |
19 | ## Available Scripts
20 |
21 | In the project directory, you can run:
22 |
23 | ### `npm start`
24 |
25 | Runs the app in the development mode.\
26 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
27 |
28 | The page will reload if you make edits.\
29 | You will also see any lint errors in the console.
30 |
31 | ### `npm test`
32 |
33 | Launches the test runner in the interactive watch mode.\
34 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
35 |
36 | ### `npm run build`
37 |
38 | Builds the app for production to the `build` folder.\
39 | It correctly bundles React in production mode and optimizes the build for the best performance.
40 |
41 | The build is minified and the filenames include the hashes.\
42 | Your app is ready to be deployed!
43 |
44 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
45 |
46 | ### `npm run eject`
47 |
48 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!**
49 |
50 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
51 |
52 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
53 |
54 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
55 |
56 | ## Learn More
57 |
58 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
59 |
60 | To learn React, check out the [React documentation](https://reactjs.org/).
61 |
62 | ### Code Splitting
63 |
64 | This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)
65 |
66 | ### Analyzing the Bundle Size
67 |
68 | This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
69 |
70 | ### Making a Progressive Web App
71 |
72 | This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
73 |
74 | ### Advanced Configuration
75 |
76 | This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
77 |
78 | ### Deployment
79 |
80 | This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
81 |
82 | ### `npm run build` fails to minify
83 |
84 | This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
85 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "react-pagination-component",
3 | "version": "0.1.0",
4 | "private": true,
5 | "dependencies": {
6 | "@testing-library/jest-dom": "^5.11.8",
7 | "@testing-library/react": "^11.2.2",
8 | "@testing-library/user-event": "^12.6.0",
9 | "react": "^17.0.1",
10 | "react-dom": "^17.0.1",
11 | "react-scripts": "5.0.1",
12 | "web-vitals": "^0.2.4"
13 | },
14 | "scripts": {
15 | "start": "react-scripts start",
16 | "build": "react-scripts build",
17 | "test": "react-scripts test",
18 | "eject": "react-scripts eject"
19 | },
20 | "eslintConfig": {
21 | "extends": [
22 | "react-app",
23 | "react-app/jest"
24 | ]
25 | },
26 | "browserslist": {
27 | "production": [
28 | ">0.2%",
29 | "not dead",
30 | "not op_mini all"
31 | ],
32 | "development": [
33 | "last 1 chrome version",
34 | "last 1 firefox version",
35 | "last 1 safari version"
36 | ]
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/codebucks27/react-pagination-component/4f399ee82c817405f12f448ecc575ab08771b56b/public/favicon.ico
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
12 |
13 |
17 |
18 |
27 | React App
28 |
29 |
30 |
31 |
32 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/public/logo192.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/codebucks27/react-pagination-component/4f399ee82c817405f12f448ecc575ab08771b56b/public/logo192.png
--------------------------------------------------------------------------------
/public/logo512.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/codebucks27/react-pagination-component/4f399ee82c817405f12f448ecc575ab08771b56b/public/logo512.png
--------------------------------------------------------------------------------
/public/manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "short_name": "React App",
3 | "name": "Create React App Sample",
4 | "icons": [
5 | {
6 | "src": "favicon.ico",
7 | "sizes": "64x64 32x32 24x24 16x16",
8 | "type": "image/x-icon"
9 | },
10 | {
11 | "src": "logo192.png",
12 | "type": "image/png",
13 | "sizes": "192x192"
14 | },
15 | {
16 | "src": "logo512.png",
17 | "type": "image/png",
18 | "sizes": "512x512"
19 | }
20 | ],
21 | "start_url": ".",
22 | "display": "standalone",
23 | "theme_color": "#000000",
24 | "background_color": "#ffffff"
25 | }
26 |
--------------------------------------------------------------------------------
/public/robots.txt:
--------------------------------------------------------------------------------
1 | # https://www.robotstxt.org/robotstxt.html
2 | User-agent: *
3 | Disallow:
4 |
--------------------------------------------------------------------------------
/screenshot-localhost_3000-2021.01.05-12_01_43.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/codebucks27/react-pagination-component/4f399ee82c817405f12f448ecc575ab08771b56b/screenshot-localhost_3000-2021.01.05-12_01_43.png
--------------------------------------------------------------------------------
/screenshot-localhost_3000-2021.01.05-12_02_22 (1).png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/codebucks27/react-pagination-component/4f399ee82c817405f12f448ecc575ab08771b56b/screenshot-localhost_3000-2021.01.05-12_02_22 (1).png
--------------------------------------------------------------------------------
/screenshot-localhost_3000-2021.01.05-12_02_22.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/codebucks27/react-pagination-component/4f399ee82c817405f12f448ecc575ab08771b56b/screenshot-localhost_3000-2021.01.05-12_02_22.png
--------------------------------------------------------------------------------
/src/App.css:
--------------------------------------------------------------------------------
1 | .App {
2 | text-align: center;
3 | }
4 |
5 | .App-logo {
6 | height: 40vmin;
7 | pointer-events: none;
8 | }
9 |
10 | @media (prefers-reduced-motion: no-preference) {
11 | .App-logo {
12 | animation: App-logo-spin infinite 20s linear;
13 | }
14 | }
15 |
16 | .App-header {
17 | background-color: #282c34;
18 | min-height: 100vh;
19 | display: flex;
20 | flex-direction: column;
21 | align-items: center;
22 | justify-content: center;
23 | font-size: calc(10px + 2vmin);
24 | color: white;
25 | }
26 |
27 | .App-link {
28 | color: #61dafb;
29 | }
30 |
31 | @keyframes App-logo-spin {
32 | from {
33 | transform: rotate(0deg);
34 | }
35 | to {
36 | transform: rotate(360deg);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/App.js:
--------------------------------------------------------------------------------
1 | import logo from "./logo.svg";
2 | import "./App.css";
3 | import PaginationComponent from "./components/PaginationComponent";
4 |
5 | function App() {
6 | return (
7 |
12 | );
13 | }
14 |
15 | export default App;
16 |
--------------------------------------------------------------------------------
/src/App.test.js:
--------------------------------------------------------------------------------
1 | import { render, screen } from '@testing-library/react';
2 | import App from './App';
3 |
4 | test('renders learn react link', () => {
5 | render();
6 | const linkElement = screen.getByText(/learn react/i);
7 | expect(linkElement).toBeInTheDocument();
8 | });
9 |
--------------------------------------------------------------------------------
/src/components/PaginationComponent.js:
--------------------------------------------------------------------------------
1 | import React, { useEffect, useState } from "react";
2 | import "./style.css";
3 | const renderData = (data) => {
4 | return (
5 |
6 | {data.map((todo, index) => {
7 | return - {todo.title}
;
8 | })}
9 |
10 | );
11 | };
12 |
13 | function PaginationComponent() {
14 | const [data, setData] = useState([]);
15 |
16 | const [currentPage, setcurrentPage] = useState(1);
17 | const [itemsPerPage, setitemsPerPage] = useState(5);
18 |
19 | const [pageNumberLimit, setpageNumberLimit] = useState(5);
20 | const [maxPageNumberLimit, setmaxPageNumberLimit] = useState(5);
21 | const [minPageNumberLimit, setminPageNumberLimit] = useState(0);
22 |
23 | const handleClick = (event) => {
24 | setcurrentPage(Number(event.target.id));
25 | };
26 |
27 | const pages = [];
28 | for (let i = 1; i <= Math.ceil(data.length / itemsPerPage); i++) {
29 | pages.push(i);
30 | }
31 |
32 | const indexOfLastItem = currentPage * itemsPerPage;
33 | const indexOfFirstItem = indexOfLastItem - itemsPerPage;
34 | const currentItems = data.slice(indexOfFirstItem, indexOfLastItem);
35 |
36 | const renderPageNumbers = pages.map((number) => {
37 | if (number < maxPageNumberLimit + 1 && number > minPageNumberLimit) {
38 | return (
39 |
45 | {number}
46 |
47 | );
48 | } else {
49 | return null;
50 | }
51 | });
52 |
53 | useEffect(() => {
54 | fetch("https://jsonplaceholder.typicode.com/todos")
55 | .then((response) => response.json())
56 | .then((json) => setData(json));
57 | }, []);
58 |
59 | const handleNextbtn = () => {
60 | setcurrentPage(currentPage + 1);
61 |
62 | if (currentPage + 1 > maxPageNumberLimit) {
63 | setmaxPageNumberLimit(maxPageNumberLimit + pageNumberLimit);
64 | setminPageNumberLimit(minPageNumberLimit + pageNumberLimit);
65 | }
66 | };
67 |
68 | const handlePrevbtn = () => {
69 | setcurrentPage(currentPage - 1);
70 |
71 | if ((currentPage - 1) % pageNumberLimit == 0) {
72 | setmaxPageNumberLimit(maxPageNumberLimit - pageNumberLimit);
73 | setminPageNumberLimit(minPageNumberLimit - pageNumberLimit);
74 | }
75 | };
76 |
77 | let pageIncrementBtn = null;
78 | if (pages.length > maxPageNumberLimit) {
79 | pageIncrementBtn = … ;
80 | }
81 |
82 | let pageDecrementBtn = null;
83 | if (minPageNumberLimit >= 1) {
84 | pageDecrementBtn = … ;
85 | }
86 |
87 | const handleLoadMore = () => {
88 | setitemsPerPage(itemsPerPage + 5);
89 | };
90 |
91 | return (
92 | <>
93 | Todo List
94 | {renderData(currentItems)}
95 |
96 | -
97 |
103 |
104 | {pageDecrementBtn}
105 | {renderPageNumbers}
106 | {pageIncrementBtn}
107 |
108 | -
109 |
115 |
116 |
117 |
120 | >
121 | );
122 | }
123 |
124 | export default PaginationComponent;
125 |
--------------------------------------------------------------------------------
/src/components/style.css:
--------------------------------------------------------------------------------
1 | .pageNumbers {
2 | list-style: none;
3 | display: flex;
4 | }
5 |
6 | .pageNumbers li {
7 | padding: 10px;
8 | border: 1px solid white;
9 | cursor: pointer;
10 | }
11 |
12 | .pageNumbers li.active {
13 | background-color: white;
14 | color: black;
15 | }
16 |
17 | .pageNumbers li button {
18 | background-color: transparent;
19 | border: none;
20 | color: white;
21 | font-size: 1.5rem;
22 | cursor: pointer;
23 | }
24 | .pageNumbers li button:hover {
25 | background-color: white;
26 | color: black;
27 | }
28 | .pageNumbers li button:focus {
29 | outline: none;
30 | }
31 |
32 | .loadmore {
33 | padding: 1rem;
34 | background-color: transparent;
35 |
36 | color: white;
37 | font-size: 1.2rem;
38 | border: 1px solid white;
39 | cursor: pointer;
40 | }
41 |
--------------------------------------------------------------------------------
/src/index.css:
--------------------------------------------------------------------------------
1 | body {
2 | margin: 0;
3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
5 | sans-serif;
6 | -webkit-font-smoothing: antialiased;
7 | -moz-osx-font-smoothing: grayscale;
8 | }
9 |
10 | code {
11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
12 | monospace;
13 | }
14 |
--------------------------------------------------------------------------------
/src/index.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import ReactDOM from 'react-dom';
3 | import './index.css';
4 | import App from './App';
5 | import reportWebVitals from './reportWebVitals';
6 |
7 | ReactDOM.render(
8 |
9 |
10 | ,
11 | document.getElementById('root')
12 | );
13 |
14 | // If you want to start measuring performance in your app, pass a function
15 | // to log results (for example: reportWebVitals(console.log))
16 | // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
17 | reportWebVitals();
18 |
--------------------------------------------------------------------------------
/src/logo.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/src/reportWebVitals.js:
--------------------------------------------------------------------------------
1 | const reportWebVitals = onPerfEntry => {
2 | if (onPerfEntry && onPerfEntry instanceof Function) {
3 | import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
4 | getCLS(onPerfEntry);
5 | getFID(onPerfEntry);
6 | getFCP(onPerfEntry);
7 | getLCP(onPerfEntry);
8 | getTTFB(onPerfEntry);
9 | });
10 | }
11 | };
12 |
13 | export default reportWebVitals;
14 |
--------------------------------------------------------------------------------
/src/setupTests.js:
--------------------------------------------------------------------------------
1 | // jest-dom adds custom jest matchers for asserting on DOM nodes.
2 | // allows you to do things like:
3 | // expect(element).toHaveTextContent(/react/i)
4 | // learn more: https://github.com/testing-library/jest-dom
5 | import '@testing-library/jest-dom';
6 |
--------------------------------------------------------------------------------