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

Pagination component #313

Merged
merged 5 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 16 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@
"dependencies": {
"copy-to-clipboard": "^3.3.3",
"inline-style-expand-shorthand": "^1.6.0",
"styletron-standard": "^3.1.0"
"styletron-standard": "^3.1.0",
"ts-essentials": "^10.0.1"
},
"pre-commit": [
"tsc",
Expand Down
44 changes: 30 additions & 14 deletions src/components/button/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AriaRole } from "react";
import { ButtonProps as BaseButtonProps } from "baseui/button";
import type { XOR } from "ts-xor";
import { Merge } from "ts-essentials";

export enum BUTTON_KIND {
primary = "primary",
Expand All @@ -18,21 +19,36 @@ export enum BUTTON_SIZE {
large = "large",
}

export type ButtonCommonProps = Omit<BaseButtonProps, "kind" | "shape" | "size"> & {
size?: BUTTON_SIZE;
disabled?: boolean;
isLoading?: boolean;
className?: string;
role?: AriaRole;
};
export type ButtonCommonProps = Omit<
Merge<
React.ComponentProps<"button">,
Merge<
BaseButtonProps,
{
size?: BUTTON_SIZE;
disabled?: boolean;
isLoading?: boolean;
className?: string;
role?: AriaRole;
}
>
>,
"ref"
>;

export type ToggleButtonProps = {
kind: BUTTON_KIND.toggle;
checked?: boolean;
} & ButtonCommonProps;
export type ToggleButtonProps = Merge<
ButtonCommonProps,
{
kind: BUTTON_KIND.toggle;
checked?: boolean;
}
>;

export type RegularButtonProps = {
kind?: Exclude<BUTTON_KIND, BUTTON_KIND.toggle>;
} & ButtonCommonProps;
export type RegularButtonProps = Merge<
ButtonCommonProps,
{
kind?: Exclude<BUTTON_KIND, BUTTON_KIND.toggle>;
}
>;

export type ButtonProps = XOR<ToggleButtonProps, RegularButtonProps>;
2 changes: 1 addition & 1 deletion src/components/copy-button/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CopyToClipboardComponentProps } from "../../shared/ui/copy-to-clipboard-component";
import { ButtonIconProps } from "../button-icon";

export type CopyButtonProps = Omit<CopyToClipboardComponentProps, "render"> & Omit<ButtonIconProps, "icon">;
export type CopyButtonProps = Omit<CopyToClipboardComponentProps, "render"> & Omit<ButtonIconProps, "icon" | "onCopy">;
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ export * from "./toggle";
export * from "./button-icon";
export * from "./copy-button";
export * from "./menu";
export * from "./pagination";
56 changes: 56 additions & 0 deletions src/components/pagination/Pagination.stories.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Canvas, Meta, Story, ArgsTable, Source } from "@storybook/blocks";
import { Pagination } from "./Pagination";
import { useState } from "react";
import { BUTTON_SIZE } from "../button/types";

<Meta title="Navigation/Pagination" component={Pagination} />

export const Template = ({ ...args }) => {
const [page, setPage] = useState(args.currentPage);
return (
<Pagination
{...args}
currentPage={page}
pageHandler={(p) => {
setPage(p);
}}
/>
);
};

# Pagination

<Canvas isColumn>
<Story name="Default" args={{ currentPage: 1, totalPages: 12 }}>
{Template.bind({})}
</Story>
<Story name="Mini" args={{ currentPage: 1, totalPages: 12, buttonSize: BUTTON_SIZE.mini }}>
{Template.bind({})}
</Story>
<Story name="Compact" args={{ currentPage: 1, totalPages: 12, buttonSize: BUTTON_SIZE.compact }}>
{Template.bind({})}
</Story>
<Story
name="Link"
args={{ currentPage: 1, totalPages: 12, buttonSize: BUTTON_SIZE.compact, linkMapper: (p) => `/page/${p}` }}
>
{Template.bind({})}
</Story>
</Canvas>

<ArgsTable of={Pagination} />

### Usage:

To use, import the component `Pagination` from `@nilfoundation/ui-kit`.

<Source
language="tsx"
dark
format={true}
code={`
import {Pagination} from "@nilfoundation/ui-kit";
...
<Pagination currentPage={1} totalPages={5} />
`}
/>
159 changes: 159 additions & 0 deletions src/components/pagination/Pagination.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import { useMemo } from "react";
import { BUTTON_KIND, BUTTON_SIZE, Button, ButtonProps } from "../button";
import { ChevronLeftIcon, ChevronRightIcon } from "../icons";
import { PageElementProps, PaginationProps } from "./types";
import { useStyletron } from "baseui";

const PageElement = ({ page, linkMapper, pageHandler, label, disabled, active, buttonSize }: PageElementProps) => {
const buttonProps: ButtonProps = {
disabled,
kind: active ? BUTTON_KIND.primary : BUTTON_KIND.secondary,
size: buttonSize,
};
if (pageHandler && !linkMapper) {
buttonProps.onClick = (e) => {
e.preventDefault();
pageHandler(page);
};
}
if (linkMapper && !disabled) {
return (
<li>
<a
href={linkMapper(page)}
onClick={(e) => {
e.preventDefault();
if (pageHandler) pageHandler(page);
KlonD90 marked this conversation as resolved.
Show resolved Hide resolved
}}
aria-label={`${active ? "Current Page, " : ""}Page ${page}`}
aria-current={active}
>
<Button tabIndex={-1} {...buttonProps}>
{label ?? page}
</Button>
</a>
</li>
);
}
return (
<li>
<Button {...buttonProps} aria-label={`${active ? "Current Page, " : ""}Page ${page}`} aria-current={active}>
{label ?? page}
</Button>
</li>
);
};

export const Pagination = ({
currentPage,
totalPages,
visiblePages = 7,
pageHandler,
linkMapper,
buttonSize = BUTTON_SIZE.default,
}: PaginationProps) => {
const [css] = useStyletron();

const pages = useMemo(() => {
if (visiblePages >= totalPages) {
return Array.from({ length: totalPages }, (_, i) => i + 1);
}
const pages = [];
let leftPages = visiblePages - 1; // minus current
let startPage = currentPage;
let endPage = currentPage;
while (leftPages > 0) {
if (startPage > 1) {
startPage--;
leftPages--;
}
if (endPage < totalPages) {
endPage++;
leftPages--;
}
}

if (startPage > 1) {
startPage += 2;
pages.push(1);
}

if (endPage < totalPages) {
endPage -= 2;
}
for (let i = startPage; i <= endPage; i++) {
pages.push(i);
}
if (endPage < totalPages) {
pages.push(totalPages);
}
return pages;
}, [currentPage, totalPages, visiblePages]);

const elements = useMemo(() => {
const elements = [];
let prevPage = 1;
for (const page of pages) {
if (page - prevPage > 1) {
elements.push(
<li>
<Button kind={BUTTON_KIND.tertiary} size={buttonSize} disabled>
...
</Button>
</li>
);
}
elements.push(
<PageElement
page={page}
linkMapper={linkMapper}
pageHandler={pageHandler}
active={page === currentPage}
buttonSize={buttonSize}
/>
);
prevPage = page;
}
return elements;
}, [pages, pageHandler, linkMapper, currentPage, buttonSize]);

return (
<nav
role="navigation"
aria-label="Pagination navigation"
className={css({
display: "flex",
flexDirection: "row",
})}
>
<ul
className={css({
display: "flex",
flexDirection: "row",
listStyle: "none",
gap: "8px",
})}
>
<PageElement
key="prev"
ukorvl marked this conversation as resolved.
Show resolved Hide resolved
disabled={currentPage === 1}
page={currentPage - 1}
label={<ChevronLeftIcon />}
linkMapper={linkMapper}
pageHandler={pageHandler}
buttonSize={buttonSize}
/>
{elements}
<PageElement
key="next"
disabled={currentPage === totalPages}
page={currentPage + 1}
label={<ChevronRightIcon />}
linkMapper={linkMapper}
pageHandler={pageHandler}
buttonSize={buttonSize}
/>
</ul>
</nav>
);
};
2 changes: 2 additions & 0 deletions src/components/pagination/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./Pagination";
export * from "./types";
21 changes: 21 additions & 0 deletions src/components/pagination/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ReactNode } from "react";
import { BUTTON_SIZE } from "../button";

export type PaginationProps = {
currentPage: number;
totalPages: number;
visiblePages?: number;
linkMapper?: (page: number) => string;
pageHandler?: (page: number) => void;
buttonSize?: BUTTON_SIZE;
};

export type PageElementProps = {
page: number;
label?: ReactNode;
linkMapper?: (page: number) => string;
pageHandler?: (page: number) => void;
disabled?: boolean;
active?: boolean;
buttonSize: BUTTON_SIZE;
};
Loading