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

feat: new paginated table components #25

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
191 changes: 191 additions & 0 deletions src/components/ui/PaginationHeader/PaginationHeader.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
import React from "react";
import { ComponentStory, ComponentMeta } from "@storybook/react";
import { PaginationHeader } from "./PaginationHeader";

export default {
title: "Components/UI/PaginationHeader",
component: PaginationHeader,
parameters: {
docs: {
description: {
component:
"A pagination component that combines Ant Design Pagination with selection summary and controls.",
},
},
},
} as ComponentMeta<typeof PaginationHeader>;

const Template: ComponentStory<typeof PaginationHeader> = (args) => {
return <PaginationHeader {...args} />;
};

// Common handlers for all stories
const commonHandlers = {
onRequestPageChange: (page: number, pageSize?: number) => {
console.log(`Page changed to ${page} with size ${pageSize}`);
},
onPageSizeChange: (pageSize: number) => {
console.log(`Page size changed to ${pageSize}`);
},
onSelectAllGlobalRecords: async () => {
console.log("Select all records clicked");
await new Promise((resolve) => setTimeout(resolve, 1000));
},
};

export const NoResults = Template.bind({});
NoResults.args = {
initialPage: 1,
initialPageSize: 10,
total: 0,
currentPageSelectedCount: 0,
totalSelectedCount: 0,
...commonHandlers,
};
NoResults.parameters = {
docs: {
description: {
story: "Shows the pagination state when there are no results.",
},
},
};

export const SinglePage = Template.bind({});
SinglePage.args = {
initialPage: 1,
initialPageSize: 10,
total: 5,
currentPageSelectedCount: 0,
totalSelectedCount: 0,
...commonHandlers,
};
SinglePage.parameters = {
docs: {
description: {
story: "Shows the pagination when all items fit in a single page.",
},
},
};

export const MultiplePages = Template.bind({});
MultiplePages.args = {
initialPage: 2,
initialPageSize: 10,
total: 25,
currentPageSelectedCount: 0,
totalSelectedCount: 0,
...commonHandlers,
};
MultiplePages.parameters = {
docs: {
description: {
story: "Shows pagination with multiple pages, currently on page 2.",
},
},
};

export const LargeDataset = Template.bind({});
LargeDataset.args = {
initialPage: 5,
initialPageSize: 20,
total: 1000,
currentPageSelectedCount: 0,
totalSelectedCount: 0,
...commonHandlers,
};
LargeDataset.parameters = {
docs: {
description: {
story: "Shows pagination with a large dataset and larger page size.",
},
},
};

export const WithPartialPageSelected = Template.bind({});
WithPartialPageSelected.args = {
initialPage: 1,
initialPageSize: 10,
total: 100,
currentPageSelectedCount: 10,
totalSelectedCount: 10,
...commonHandlers,
};
WithPartialPageSelected.parameters = {
docs: {
description: {
story:
"Shows when current page is fully selected but there are more records available to select globally.",
},
},
};

export const WithAllPagesSelected = Template.bind({});
WithAllPagesSelected.args = {
initialPage: 1,
initialPageSize: 10,
total: 100,
currentPageSelectedCount: 10,
totalSelectedCount: 100,
...commonHandlers,
};
WithAllPagesSelected.parameters = {
docs: {
description: {
story: "Shows when all records across all pages are selected.",
},
},
};

export const CustomPageSize = Template.bind({});
CustomPageSize.args = {
initialPage: 1,
initialPageSize: 50,
total: 200,
currentPageSelectedCount: 0,
totalSelectedCount: 0,
...commonHandlers,
};
CustomPageSize.parameters = {
docs: {
description: {
story: "Shows pagination with a custom page size of 50 items.",
},
},
};

export const LastPage = Template.bind({});
LastPage.args = {
initialPage: 10,
initialPageSize: 10,
total: 100,
currentPageSelectedCount: 0,
totalSelectedCount: 0,
...commonHandlers,
};
LastPage.parameters = {
docs: {
description: {
story: "Shows pagination when viewing the last page of results.",
},
},
};

export const WithoutSelectionControls = Template.bind({});
WithoutSelectionControls.args = {
initialPage: 1,
initialPageSize: 10,
total: 100,
currentPageSelectedCount: 0,
totalSelectedCount: 0,
onRequestPageChange: commonHandlers.onRequestPageChange,
onPageSizeChange: commonHandlers.onPageSizeChange,
// Omitting onSelectAllGlobalRecords to hide selection controls
};
WithoutSelectionControls.parameters = {
docs: {
description: {
story:
"Shows pagination without the selection controls by omitting the onSelectAllGlobalRecords handler.",
},
},
};
133 changes: 133 additions & 0 deletions src/components/ui/PaginationHeader/PaginationHeader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { useLocale } from "@/context";
import { Col, Pagination, Row, Spin } from "antd";
import type { PaginationProps } from "antd";
import { useMemo, useState, useCallback, memo, useEffect } from "react";
import {
SelectAllRecordsRow,
shouldShowSelectionRow,
} from "../SelectAllRecordsRow/SelectAllRecordsRow";
import type { PaginationHeaderProps } from "./PaginationHeader.types";
import type { SelectAllRecordsRowProps } from "../SelectAllRecordsRow/SelectAllRecordsRow.types";

const PaginationHeaderComponent = (props: PaginationHeaderProps) => {
const {
total,
page: pageProps,
pageSize: pageSizeProps,
currentPageSelectedCount,
totalSelectedCount,
onRequestPageChange,
onSelectAllGlobalRecords,
totalRowsLoading,
} = props;

const { t } = useLocale();

const [page, setPage] = useState(pageProps);
const [pageSize, setPageSize] = useState(pageSizeProps);

useEffect(() => {
setPage(pageProps);
}, [pageProps]);

useEffect(() => {
setPageSize(pageSizeProps);
}, [pageSizeProps]);

const from = useMemo(() => (page - 1) * pageSize + 1, [page, pageSize]);
const to = useMemo(
() => Math.min(page * pageSize, total),
[page, pageSize, total],
);

const handlePageChange = useCallback(
(newPage: number, newPageSize?: number) => {
setPage(newPage);
if (newPageSize !== undefined) {
setPageSize(newPageSize);
}
onRequestPageChange(newPage, newPageSize);
},
[onRequestPageChange],
);

const summary = useMemo(() => {
if (total === undefined) return null;
if (total === 0) return t("no_results");

return t("summary")
.replace("{from}", from?.toString())
.replace("{to}", to?.toString())
.replace("{total}", total?.toString());
}, [total, from, to, t]);

const paginationProps: PaginationProps = useMemo(
() => ({
total,
pageSize,
current: page,
onChange: handlePageChange,
showSizeChanger: true,
showLessItems: true,
locale: {
items_per_page: t("items_per_page"),
},
}),
[total, pageSize, page, handlePageChange, t],
);

const shouldShowSelectAllRecordsRow = useMemo(() => {
if (!onSelectAllGlobalRecords) return false;
return shouldShowSelectionRow({
currentPageSelectedCount,
currentPageSize: pageSize,
totalRecordsCount: total,
totalSelectedCount,
currentPage: page,
});
}, [
onSelectAllGlobalRecords,
currentPageSelectedCount,
pageSize,
total,
totalSelectedCount,
page,
]);

const selectAllRecordsProps: SelectAllRecordsRowProps = useMemo(
() => ({
shouldShow: shouldShowSelectAllRecordsRow,
currentPageSelectedCount,
totalRecordsCount: total,
totalSelectedCount,
onSelectAllRecords: onSelectAllGlobalRecords!,
}),
[
shouldShowSelectAllRecordsRow,
currentPageSelectedCount,
total,
totalSelectedCount,
onSelectAllGlobalRecords,
],
);

const columnSpan = shouldShowSelectAllRecordsRow ? 8 : 12;

return (
<Row align="bottom" className="pb-4" wrap={false}>
<Col span={columnSpan}>
<Pagination {...paginationProps} />
</Col>
{shouldShowSelectAllRecordsRow && (
<Col span={8} className="text-center">
<SelectAllRecordsRow {...selectAllRecordsProps} />
</Col>
)}
<Col span={columnSpan} className="text-right">
{totalRowsLoading ? <Spin /> : summary}
</Col>
</Row>
);
};

export const PaginationHeader = memo(PaginationHeaderComponent);
18 changes: 18 additions & 0 deletions src/components/ui/PaginationHeader/PaginationHeader.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export type PaginationHeaderProps = {
/** Total number of records across all pages */
total: number;
/** Initial page number */
page: number;
/** Initial number of items per page */
pageSize: number;
/** Number of selected records in the current page */
currentPageSelectedCount: number;
/** Total number of selected records across all pages */
totalSelectedCount: number;
/** Callback when page or page size changes */
onRequestPageChange: (page: number, pageSize?: number) => void;
/** Optional callback to select all records across all pages */
onSelectAllGlobalRecords?: () => Promise<void>;
/** Whether the total number of rows is loading */
totalRowsLoading: boolean;
};
2 changes: 2 additions & 0 deletions src/components/ui/PaginationHeader/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { PaginationHeader } from "./PaginationHeader";
export type { PaginationHeaderProps } from "./PaginationHeader.types";
Loading