Skip to content

Commit

Permalink
refactor(pagination): 🚸 unify pagination navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
navin-moorthy committed Sep 10, 2020
1 parent 0982f54 commit edfa252
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 159 deletions.
106 changes: 106 additions & 0 deletions src/pagination/PaginationButton.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { callAllHandlers, isNumber } from "@chakra-ui/utils";
import React from "react";
import { createComponent, createHook } from "reakit-system";
import { ButtonHTMLProps, ButtonOptions, useButton } from "reakit";

import { PAGINATION_ITEM_KEYS } from "./__keys";
import { PaginationStateReturn } from "./PaginationState";

export type PaginationButtonOptions = ButtonOptions &
Pick<
PaginationStateReturn,
| "currentPage"
| "move"
| "next"
| "prev"
| "first"
| "last"
| "isAtMax"
| "isAtMin"
> & {
goto: string | number;
getAriaLabel?: (goto: string | number, isCurrent: boolean) => string;
};

export type PaginationButtonHTMLProps = ButtonHTMLProps;

export type PaginationButtonProps = PaginationButtonOptions &
PaginationButtonHTMLProps;

export const usePaginationButton = createHook<
PaginationButtonOptions,
PaginationButtonHTMLProps
>({
name: "PaginationButton",
compose: useButton,
keys: PAGINATION_ITEM_KEYS,

useOptions(options, { disabled: htmlDisabled }) {
const { goto, isAtMax, isAtMin } = options;
let disabled = false;

if (goto === "next" || goto === "last") {
disabled = isAtMax;
}

if (goto === "prev" || goto === "first") {
disabled = isAtMin;
}

return {
disabled: htmlDisabled || disabled,
...options,
};
},

useProps(
{ currentPage, move, last, first, next, prev, goto, getAriaLabel },
{ onClick: htmlOnClick, ...htmlProps },
) {
const isCurrent = currentPage === goto;

const onClick = React.useCallback(() => {
switch (goto) {
case "next":
next?.();
break;

case "prev":
prev?.();
break;

case "first":
first?.();
break;

case "last":
last?.();
break;

default:
if (isNumber(goto)) {
move?.(goto);
}
break;
}
}, [goto, next, prev, first, last, move]);

const ariaLabel =
getAriaLabel?.(goto, isCurrent) ?? isCurrent
? `Page ${goto}`
: `Go to ${goto} Page`;

return {
"aria-label": ariaLabel,
"aria-current": isCurrent ? true : undefined,
onClick: callAllHandlers(onClick, htmlOnClick),
...htmlProps,
};
},
});

export const PaginationButton = createComponent({
as: "button",
memo: true,
useHook: usePaginationButton,
});
58 changes: 0 additions & 58 deletions src/pagination/PaginationItem.ts

This file was deleted.

42 changes: 0 additions & 42 deletions src/pagination/PaginationNext.ts

This file was deleted.

42 changes: 0 additions & 42 deletions src/pagination/PaginationPrev.ts

This file was deleted.

17 changes: 12 additions & 5 deletions src/pagination/PaginationState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ export const usePaginationState = (props: UsePaginationProps = {}) => {
];

const next = React.useCallback(() => {
setPage(page + 1);
}, [page, setPage]);
setPage(prevPage => prevPage + 1);
}, [setPage]);

const prev = React.useCallback(() => {
setPage(page - 1);
}, [page, setPage]);
setPage(prevPage => prevPage - 1);
}, [setPage]);

const first = React.useCallback(() => {
setPage(1);
Expand All @@ -100,13 +100,20 @@ export const usePaginationState = (props: UsePaginationProps = {}) => {
setPage(count);
}, [count, setPage]);

const move = React.useCallback(
page => {
if (page >= 1 && page <= count) setPage(page);
},
[count, setPage],
);

return {
currentPage: page,
isAtMax: page >= count,
isAtMin: page <= 1,
next,
prev,
move: setPage,
move,
first,
last,
pages,
Expand Down
2 changes: 1 addition & 1 deletion src/pagination/__keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ const PAGINATION_STATE_KEYS = [
export const PAGINATION_KEYS = PAGINATION_STATE_KEYS;
export const PAGINATION_ITEM_KEYS = [
...PAGINATION_STATE_KEYS,
"page",
"goto",
"getAriaLabel",
];
4 changes: 1 addition & 3 deletions src/pagination/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export * from "./PaginationState";
export * from "./Pagination";
export * from "./PaginationItem";
export * from "./PaginationNext";
export * from "./PaginationPrev";
export * from "./PaginationButton";
29 changes: 21 additions & 8 deletions src/pagination/stories/Pagination.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import React from "react";

import { Meta } from "@storybook/react";
import { Pagination } from "../Pagination";
import { PaginationPrev } from "../PaginationPrev";
import { PaginationNext } from "../PaginationNext";
import { PaginationItem } from "../PaginationItem";
import { PaginationButton } from "../PaginationButton";
import { UsePaginationProps, usePaginationState } from "../PaginationState";

export default {
Expand All @@ -13,12 +11,20 @@ export default {

const PaginationComp: React.FC<UsePaginationProps> = props => {
const state = usePaginationState({ count: 10, ...props });
console.log("%c state", "color: #cc0088", state);

return (
<Pagination {...state}>
<ul style={{ display: "flex", listStyle: "none" }}>
<li>
<PaginationPrev {...state}>Previous</PaginationPrev>
<PaginationButton goto="first" {...state}>
First
</PaginationButton>
</li>
<li>
<PaginationButton goto="prev" {...state}>
Previous
</PaginationButton>
</li>
{state.pages.map(page => {
if (page === "start-ellipsis" || page === "end-ellipsis") {
Expand All @@ -27,20 +33,27 @@ const PaginationComp: React.FC<UsePaginationProps> = props => {

return (
<li key={page}>
<PaginationItem
page={page}
<PaginationButton
goto={page}
style={{
fontWeight: state.currentPage === page ? "bold" : undefined,
}}
{...state}
>
{page}
</PaginationItem>
</PaginationButton>
</li>
);
})}
<li>
<PaginationNext {...state}>Next</PaginationNext>
<PaginationButton goto="next" {...state}>
Next
</PaginationButton>
</li>
<li>
<PaginationButton goto="last" {...state}>
Last
</PaginationButton>
</li>
</ul>
</Pagination>
Expand Down

0 comments on commit edfa252

Please sign in to comment.