`;
+
+exports[`Pagination renders correctly with function as children 1`] = `
+
+
+
+
+
+
+
+`;
+
+exports[`Pagination uses default number formatter correctly 1`] = `
+
+
+
+
+
+
+
+`;
diff --git a/src/Pagination/Pagination.tsx b/src/Pagination/Pagination.tsx
index 48ad38f..4252e7c 100644
--- a/src/Pagination/Pagination.tsx
+++ b/src/Pagination/Pagination.tsx
@@ -102,17 +102,31 @@ const TruncableElement = ({ prev }: ITruncableElementProps) => {
) : null;
};
+const pageFormatter = (page: number) => Intl.NumberFormat().format(page);
+
export const PageButton = ({
as =
,
+ children,
className,
dataTestIdActive,
dataTestIdInactive,
+ formatter = pageFormatter,
activeClassName,
inactiveClassName,
renderExtraProps,
}: PageButtonProps) => {
const pagination: IPagination = React.useContext(PaginationContext);
+ const getPageContent = (page: number): React.ReactNode => {
+ if (!children) {
+ return formatter(page);
+ }
+
+ return typeof children === "function"
+ ? (children as (page: number) => React.ReactNode)(page)
+ : children;
+ };
+
const renderPageButton = (page: number) => (
- {page}
+ {getPageContent(page)}
);
diff --git a/src/types/types.ts b/src/types/types.ts
index c61f797..f0be549 100644
--- a/src/types/types.ts
+++ b/src/types/types.ts
@@ -32,8 +32,6 @@ type IPagination = IUsePagination & {
type ButtonProps = ButtonHTMLAttributes
& {
as?: React.ReactElement;
- children?: string | React.ReactNode;
- className?: string;
dataTestId?: string;
};
@@ -44,9 +42,11 @@ type PageButtonProps = ButtonProps & {
as?: React.ReactElement;
activeClassName?: string;
inactiveClassName?: string;
+ children?: React.ReactNode | ((page: number) => React.ReactNode);
dataTestIdActive?: string;
dataTestIdInactive?: string;
- renderExtraProps?: (pageNum: number) => {};
+ formatter?: (page: number) => string;
+ renderExtraProps?: (pageNum: number) => Record;
};
export {
diff --git a/stories/PaginationCustomChildren.stories.tsx b/stories/PaginationCustomChildren.stories.tsx
new file mode 100644
index 0000000..28b6d11
--- /dev/null
+++ b/stories/PaginationCustomChildren.stories.tsx
@@ -0,0 +1,87 @@
+import React from "react";
+import clsx from "clsx";
+import { Meta, Story } from "@storybook/react";
+import { FiArrowLeft, FiArrowRight } from "react-icons/fi";
+import { Pagination, IPaginationProps } from "../src";
+
+import "./tailwind_output.css";
+
+const meta: Meta = {
+ title: "Pagination Custom Children",
+ component: Pagination,
+ parameters: {
+ controls: { expanded: true },
+ },
+};
+
+export default meta;
+
+const PaginationStory: Story = (args) => {
+ const [page, setPage] = React.useState(0);
+
+ const handlePageChange = (page: number) => {
+ setPage(page);
+ };
+
+ return (
+
+ }
+ className={clsx(
+ "flex items-center mr-2 text-gray-500 hover:text-gray-600 dark:hover:text-gray-200",
+ {
+ "cursor-pointer": page !== 0,
+ "opacity-50": page === 0,
+ },
+ )}
+ >
+
+ Previous
+
+
+
+
+
+ Next
+
+
+
+ );
+};
+
+export const Default = PaginationStory.bind({});
+
+Default.args = {
+ totalPages: 1000,
+ edgePageCount: 2,
+ middlePagesSiblingCount: 1,
+ truncableText: "...",
+ truncableClassName: "w-10 px-0.5",
+};
diff --git a/stories/PaginationTailwind.stories.tsx b/stories/PaginationTailwind.stories.tsx
index 4946835..396c51d 100644
--- a/stories/PaginationTailwind.stories.tsx
+++ b/stories/PaginationTailwind.stories.tsx
@@ -83,3 +83,25 @@ Default.args = {
truncableText: "...",
truncableClassName: "w-10 px-0.5",
};
+
+export const BigNumbers = PaginationStory.bind({});
+
+BigNumbers.args = {
+ totalPages: 1200,
+ currentPage: 1000,
+ edgePageCount: 2,
+ middlePagesSiblingCount: 1,
+ truncableText: "...",
+ truncableClassName: "w-10 px-0.5",
+};
+
+export const CustomFormatter = PaginationStory.bind({});
+
+CustomFormatter.args = {
+ totalPages: 10,
+ edgePageCount: 2,
+ middlePagesSiblingCount: 1,
+ truncableText: "...",
+ truncableClassName: "",
+ formatter: (page: number) => `${page}.0`,
+};