Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ new(react-pagination): Create package. #121

Merged
merged 15 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
695 changes: 695 additions & 0 deletions packages/react-pagination/LICENSE

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions packages/react-pagination/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[![License: GPLv3](https://img.shields.io/badge/License-GPL%20v3-blue.svg?color=green)](http://www.gnu.org/licenses/gpl-3.0)
[![npm](https://img.shields.io/npm/v/@wpmudev/react-pagination)](https://www.npmjs.com/package/@wpmudev/react-pagination)
![npm peer dependency version](https://img.shields.io/npm/dependency-version/@wpmudev/react-pagination/peer/react)

# React Pagination
[React Button](https://wpmudev.github.io/shared-ui-react/?path=/story/components-pagination--primary) allows you include a button on your project.

# `@wpmudev/react-pagination`

## Installation

```
npm i @wpmudev/react-pagination --save-dev
```

## Usage

### Javascript Instantiation

```js
import React from 'react';
import {
Pagination
} from '@wpmudev/react-pagination';

const MyApp = () => {
return (
<Pagination>
<div>Element 1 could be here</div>
<div>Element 2 could be here</div>
<div>Element 3 could be here</div>
</Pagination>
);
}
```
Gowtham369 marked this conversation as resolved.
Show resolved Hide resolved

### Props

Prop Name | Type | Description
--- | --- | ---
limit | Integer | Enter the elements limit per page.
results | Boolean | Shows results section if `true`.
skip | Boolean | Shows Skip(<<,>>) buttons if `true`.
child | Array | Pass an array here to render as elements.
Children | html or jsx | These are direct children wrapped inside Pagination Component

7 changes: 7 additions & 0 deletions packages/react-pagination/__tests__/react-pagination.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

const reactPagination = require('..');

describe('@wpmudev/react-pagination', () => {
it('needs tests');
});
82 changes: 82 additions & 0 deletions packages/react-pagination/docs/react-pagination.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import React from "react";
import { Pagination } from "../lib/react-pagination";

export default {
title: "Components/Pagination",
component: Pagination,
};

let childrenArray = new Array();
for (let i = 1; i <= 34 /* from here you can change the number of elements that can be inputed */; ++i)
childrenArray.push(<h5 key={i}>This is the sample data that I am working with. Id number is {i}</h5>);

export const primary = args => (
<Pagination {...args}>
<div>This is sample child component 1</div>
<div>This is sample child component 2</div>
<div>This is sample child component 3</div>
<div>This is sample child component 4</div>
<div>This is sample child component 5</div>
<div>This is sample child component 6</div>
<div>This is sample child component 7</div>
<div>This is sample child component 8</div>
<div>This is sample child component 10</div>
<div>This is sample child component 11</div>
<div>This is sample child component 12</div>
<div>This is sample child component 13</div>
<div>This is sample child component 14</div>
<div>This is sample child component 15</div>
</Pagination>
);
Gowtham369 marked this conversation as resolved.
Show resolved Hide resolved

primary.storyName = "Mixed";
primary.args = {
limit: 5, //elements per page
results: true, //number of results
skip: true, //skip to last and first page buttons status
Gowtham369 marked this conversation as resolved.
Show resolved Hide resolved
child: childrenArray, //Array with child elements
};
primary.argTypes = {
limit: { type: "number" },
results: { type: "boolean" },
skip: { type: "boolean" },
};

export const secondary = args => (
<Pagination {...args}>
<div>This is sample child component 1</div>
<div>This is sample child component 2</div>
<div>This is sample child component 3</div>
<div>This is sample child component 4</div>
<div>This is sample child component 5</div>
<div>This is sample child component 6</div>
<div>This is sample child component 7</div>
<div>This is sample child component 8</div>
<div>This is sample child component 10</div>
<div>This is sample child component 11</div>
<div>This is sample child component 12</div>
<div>This is sample child component 13</div>
<div>This is sample child component 14</div>
<div>This is sample child component 15</div>
</Pagination>
);

secondary.storyName = "Children Components";
secondary.args = {
...primary.args,
child: [],
};
secondary.argTypes = {
...primary.argsTypes,
};

export const thirtiary = args => <Pagination {...args} />;

thirtiary.storyName = "Child Property";
thirtiary.args = {
...primary.args,
child: childrenArray,
};
thirtiary.argTypes = {
...primary.argsTypes,
};
138 changes: 138 additions & 0 deletions packages/react-pagination/lib/react-pagination.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import React, { useState, useEffect } from "react";

export const Pagination = ({ limit, skip, results, ...args }) => {
const childElements = args.children ? [...args.children, ...args.child] : [...args.child],
elements = childElements.length,
pages = elements / limit > parseInt(elements / limit) ? parseInt(elements / limit) + 1 : elements / limit,
[pagesArray, setPagesArray] = useState([]),
[selectedPage, setSelectedPage] = useState("1"),
[startIndex, setStartIndex] = useState(0),
[endIndex, setEndIndex] = useState(pages >= 5 ? 5 : pages),
[pageClickCounter, setPageClickCounter] = useState(0),
[elementsStartIndex, setElementsStartIndex] = useState(0),
[elementsEndIndex, setElementsEndIndex] = useState(limit);

useEffect(() => {
var pagesArray = [];
for (let i = 1; i <= pages; ++i) pagesArray.push(i);
setPagesArray(pagesArray);
}, []);

useEffect(() => {
selectedPage >= endIndex && incrementIndexes();
selectedPage <= startIndex + 1 && decrementIndexes();
}, [pageClickCounter]);

useEffect(() => {
if (selectedPage !== "1") {
setElementsStartIndex(selectedPage * limit - limit);
setElementsEndIndex(selectedPage * limit);
}
}, [selectedPage]);

const handleSkipToFirstPage = () => {
setSelectedPage(1);
setStartIndex(0);
setEndIndex(pages >= 5 ? 5 : pages);
};

const handleSkipToLastPage = () => {
setSelectedPage(pages);
setStartIndex(pages >= 5 ? pages - 5 : 0);
setEndIndex(pages);
};

const handlePreviousPage = () => {
selectedPage > 1 && setSelectedPage(selectedPage - 1);
decrementIndexes();
};

const handleNextPage = () => {
selectedPage < pages && setSelectedPage(parseInt(selectedPage) + 1);
incrementIndexes();
};

const decrementIndexes = () => {
if (selectedPage - 1 <= startIndex + 1 && startIndex !== 0) {
setStartIndex(startIndex - 1);
setEndIndex(endIndex - 1);
}
};
const incrementIndexes = () => {
if (selectedPage + 1 >= endIndex && endIndex !== pages) {
setStartIndex(startIndex + 1);
setEndIndex(endIndex + 1);
}
};

const handlePreviousEllipsis = () => {
setSelectedPage(startIndex >= 5 ? endIndex - 6 : endIndex - startIndex - 1);
setStartIndex(startIndex >= 5 ? startIndex - 5 : 0);
setEndIndex(startIndex >= 5 ? endIndex - 5 : endIndex - startIndex);
};
const handleNextEllipsis = () => {
setSelectedPage(pages - endIndex >= 5 ? startIndex + 7 : pages - endIndex + startIndex + 2);
setStartIndex(pages - endIndex >= 5 ? startIndex + 5 : pages - endIndex + startIndex);
setEndIndex(pages - endIndex >= 5 ? endIndex + 5 : pages);
};

const handlePageClick = async page => {
setSelectedPage(page);
setPageClickCounter(pageClickCounter + 1);
};

return (
<>
{/* {React.Children.map(childElements, child => React.cloneElement(child)).slice(elementsStartIndex, elementsEndIndex)} */}
<div className="sui-pagination-wrap">
{results && <span className="sui-pagination-results">{elements} results</span>}
<ul className="sui-pagination">
{skip && (
<li onClick={handleSkipToFirstPage}>
<a disabled={selectedPage <= 1} title="go to first page">
Gowtham369 marked this conversation as resolved.
Show resolved Hide resolved
Gowtham369 marked this conversation as resolved.
Show resolved Hide resolved
<span className="sui-icon-arrow-skip-back"></span>
Gowtham369 marked this conversation as resolved.
Show resolved Hide resolved
</a>
</li>
)}
<li onClick={handlePreviousPage}>
<a disabled={selectedPage <= 1}>
<span className="sui-icon-chevron-left"></span>
</a>
</li>
{startIndex > 0 && (
<li onClick={handlePreviousEllipsis}>
<a>...</a>
</li>
)}
{pagesArray?.slice(startIndex, endIndex)?.map((data, index) => {
return (
<li onClick={() => handlePageClick(parseInt(data))} key={index}>
<a aria-selected={selectedPage === data} className={selectedPage === data ? "sui-active" : ""}>
{data}
</a>
</li>
);
})}
{endIndex < pages && (
<li onClick={handleNextEllipsis}>
<a>...</a>
</li>
)}
<li onClick={handleNextPage}>
<a disabled={selectedPage >= pages}>
<span className="sui-icon-chevron-right"></span>
</a>
</li>
{skip && (
<li onClick={handleSkipToLastPage}>
<a disabled={selectedPage >= pages} title="go to last page">
<span className="sui-icon-arrow-skip-forward"></span>
</a>
</li>
)}
</ul>
</div>
{React.Children.map(childElements, child => React.cloneElement(child)).slice(elementsStartIndex, elementsEndIndex)}
</>
);
};
51 changes: 51 additions & 0 deletions packages/react-pagination/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "@wpmudev/react-pagination",
"version": "0.0.0",
"description": "WPMU DEV Shared UI React Pagination Component",
"keywords": [],
"author": "WPMU DEV (https://wpmudev.com/)",
"contributors": [
{
"name": "Leighton Sapir",
"email": "2328848+iamleigh@users.noreply.github.com",
"url": "https://iamleigh.com/"
},
{
"name": "Gowtham Ravipati",
"email": "66052144+Gowtham369@users.noreply.github.com",
"url": "https://gowtham369.github.io/Gowtham_Portfolio/"
}
Gowtham369 marked this conversation as resolved.
Show resolved Hide resolved
],
"license": "GPL-3.0",
"main": "dist/react-pagination.cjs.js",
"module": "dist/react-pagination.esm.js",
"src": "lib/react-pagination.js",
"directories": {
"lib": "lib",
"test": "__tests__"
},
"files": [
"dist",
"lib"
],
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/wpmudev/shared-ui-react.git",
"directory": "packages/react-pagination"
},
"scripts": {
"test": "echo \"Error: run tests from root\" && exit 1",
"build": "sui-builder"
},
"bugs": {
"url": "https://github.com/wpmudev/shared-ui-react/issues"
},
"homepage": "https://github.com/wpmudev/shared-ui-react#readme",
"devDependencies": {
"@wpmudev/react-builder": "^0.0.0",
"react": "^17.0.1"
}
}