Skip to content

Commit f6b0579

Browse files
committed
Add the LinkButton and Pagination components.
1 parent 3361ead commit f6b0579

File tree

9 files changed

+146
-2
lines changed

9 files changed

+146
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ The components in this library will be described below.
2626
- [Form](https://github.com/macroing/simple-react-components/tree/master/documentation/form) provides a component that defines a form.
2727
- [Input](https://github.com/macroing/simple-react-components/tree/master/documentation/input) provides a component that defines an input.
2828
- [Jumbotron](https://github.com/macroing/simple-react-components/tree/master/documentation/jumbotron) provides a component that defines a jumbotron.
29+
- [LinkButton](https://github.com/macroing/simple-react-components/tree/master/documentation/link-button) provides a component that defines a link button.
2930
- [MenuBar](https://github.com/macroing/simple-react-components/tree/master/documentation/menu-bar) provides a component that defines a menu bar.
31+
- [Pagination](https://github.com/macroing/simple-react-components/tree/master/documentation/pagination) provides a component that defines a pagination.
3032
- [Select](https://github.com/macroing/simple-react-components/tree/master/documentation/select) provides a component that defines a select.
3133
- [TabPane](https://github.com/macroing/simple-react-components/tree/master/documentation/tab-pane) provides a component that defines a tab pane.
3234
- [TextArea](https://github.com/macroing/simple-react-components/tree/master/documentation/text-area) provides a component that defines a text area.

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# `LinkButton`

documentation/pagination/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# `Pagination`

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import Footer from "./src/Footer";
55
import Form from "./src/Form";
66
import Input from "./src/Input";
77
import Jumbotron from "./src/Jumbotron";
8+
import LinkButton from "./src/LinkButton";
89
import MenuBar from "./src/MenuBar";
910
import MobileMenuBar from "./src/MobileMenuBar";
11+
import Pagination from "./src/Pagination";
1012
import Select from "./src/Select";
1113
import TabPane from "./src/TabPane";
1214
import TextArea from "./src/TextArea";
1315

14-
export { Button, DesktopMenuBar, Dialog, Footer, Form, Input, Jumbotron, MenuBar, MobileMenuBar, Select, TabPane, TextArea };
16+
export { Button, DesktopMenuBar, Dialog, Footer, Form, Input, Jumbotron, LinkButton, MenuBar, MobileMenuBar, Pagination, Select, TabPane, TextArea };

src/LinkButton.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import importedStyles from "./LinkButton.module.css";
2+
3+
export default function LinkButton(props) {
4+
let { children, styles, ...rest } = props;
5+
6+
//If no styles property has been assigned, the imported CSS module will be used for styling.
7+
if (styles === null || styles === undefined) {
8+
styles = importedStyles;
9+
}
10+
11+
return (
12+
<button className={styles.link_button} {...rest}>
13+
{children}
14+
</button>
15+
);
16+
}

src/LinkButton.module.css

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
.link_button {
2+
--src-link-button-color: #333333;
3+
--src-link-button-color-disabled: #dddddd;
4+
--src-link-button-color-hover: #1877f2;
5+
6+
background-color: transparent;
7+
border: none;
8+
color: var(--src-link-button-color);
9+
cursor: pointer;
10+
font-family: inherit;
11+
text-decoration: none;
12+
}
13+
14+
.link_button:hover {
15+
color: var(--src-link-button-color-hover);
16+
text-decoration: underline;
17+
}
18+
19+
.link_button:disabled {
20+
background-color: transparent;
21+
border: none;
22+
color: var(--src-link-button-color-disabled);
23+
cursor: default;
24+
}
25+
26+
.link_button:disabled:hover {
27+
text-decoration: none;
28+
}

src/Pagination.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import LinkButton from "./LinkButton";
2+
3+
import importedStyles from "./Pagination.module.css";
4+
5+
export default function Pagination(props) {
6+
const items = Math.max(parseInt(props.items) || 1, 1);
7+
const itemsPerPage = Math.max(parseInt(props.itemsPerPage) || 1, 1);
8+
const maximumPagesPerSide = Math.max(parseInt(props.maximumPagesPerSide) || 0, 0);
9+
const onClick = props.onClick;
10+
const page = Math.max(parseInt(props.page) || 1, 1);
11+
const pages = Math.ceil(items / itemsPerPage);
12+
const styles = props.styles || importedStyles;
13+
14+
const isEnablingPreviousPage = page > 1;
15+
const isEnablingNextPage = page < pages;
16+
17+
const previousPage = isEnablingPreviousPage ? page - 1 : page;
18+
const nextPage = isEnablingNextPage ? page + 1 : page;
19+
20+
let pageMinimum = page - maximumPagesPerSide;
21+
let pageMaximum = page + maximumPagesPerSide;
22+
23+
if (pageMinimum < 1 && pageMaximum > pages) {
24+
pageMinimum = 1;
25+
pageMaximum = pages;
26+
} else if (pageMinimum < 1) {
27+
pageMaximum = Math.min(pageMaximum + (1 - pageMinimum), pages);
28+
pageMinimum = 1;
29+
} else if (pageMaximum > pages) {
30+
pageMinimum = Math.max(pageMinimum - (pageMaximum - pages), 1);
31+
pageMaximum = pages;
32+
}
33+
34+
const array = [];
35+
36+
for (let i = pageMinimum; i <= pageMaximum; i++) {
37+
array.push(i);
38+
}
39+
40+
function handleClick(currentPage) {
41+
if (currentPage !== page && onClick) {
42+
onClick(currentPage);
43+
}
44+
}
45+
46+
return (
47+
<div className={styles.pagination}>
48+
<ul>
49+
<li>
50+
<LinkButton disabled={!isEnablingPreviousPage} onClick={() => handleClick(previousPage)}>
51+
&laquo;
52+
</LinkButton>
53+
</li>
54+
{array.map((currentPage) => (
55+
<li key={"page-" + currentPage}>
56+
<LinkButton disabled={currentPage === page} onClick={() => handleClick(currentPage)}>
57+
{currentPage}
58+
</LinkButton>
59+
</li>
60+
))}
61+
<li>
62+
<LinkButton disabled={!isEnablingNextPage} onClick={() => handleClick(nextPage)}>
63+
&raquo;
64+
</LinkButton>
65+
</li>
66+
</ul>
67+
</div>
68+
);
69+
}

src/Pagination.module.css

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
.pagination {
2+
align-items: center;
3+
display: flex;
4+
flex-direction: row;
5+
flex-wrap: wrap;
6+
justify-content: center;
7+
width: 100%;
8+
}
9+
10+
.pagination > ul {
11+
display: flex;
12+
flex-direction: row;
13+
flex-wrap: wrap;
14+
gap: 10px;
15+
justify-content: center;
16+
list-style-type: none;
17+
margin: 0px;
18+
padding: 0px;
19+
width: 100%;
20+
}
21+
22+
.pagination > ul > li {
23+
margin: 0px;
24+
padding: 0px;
25+
}

0 commit comments

Comments
 (0)