Skip to content

Commit c6f235c

Browse files
Improve table pagination (#1567)
* Switch to polymorphic type declaration for Pagination. - Table Pagination Layout takes totalItems and itemsPerPage instead of totalPages. - Navigation and Pagination Layouts remain the same, although they now extend BasePaginationProps. - Update Tests * Update the pagination stories. * Export DefaultPaginationProps and TablePaginationProps to clients * Check if paginationValues is defined in tablePaginationState(). * Add layout to BasePaginationProps to allow TS to handle it as a discriminant key. * Use status instead of meter for the table pagination's aria role. * Handle zero items/improve tests. * Validate pagination props (currentPage, totalPages, itemsPerPage, and totalItems). * Add aria-live="polite" to the table pagination info div * Add itemsPerPage and totalItems to the table pagination story args. --------- Co-authored-by: Joshua Mathews <[email protected]>
1 parent 2dcc282 commit c6f235c

File tree

6 files changed

+437
-24
lines changed

6 files changed

+437
-24
lines changed

apps/storybook/src/Pagination.stories.tsx

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Meta, StoryFn } from "@storybook/react";
22
import type { PaginationProps } from "flowbite-react";
3-
import { Pagination } from "flowbite-react";
3+
import { DefaultPaginationProps, Pagination, TablePaginationProps } from "flowbite-react";
44
import { useEffect, useState } from "react";
55

66
export default {
@@ -15,7 +15,9 @@ export default {
1515
],
1616
} as Meta;
1717

18-
const Template: StoryFn<PaginationProps> = ({ currentPage = 1, layout = "pagination", totalPages = 100, ...rest }) => {
18+
const Template: StoryFn<PaginationProps> = (props) => {
19+
const { currentPage = 1, layout = "pagination" } = props;
20+
1921
const [page, setPage] = useState(currentPage);
2022

2123
const onPageChange = (page: number) => {
@@ -26,6 +28,21 @@ const Template: StoryFn<PaginationProps> = ({ currentPage = 1, layout = "paginat
2628
setPage(currentPage);
2729
}, [currentPage]);
2830

31+
if (layout === "table") {
32+
const { itemsPerPage = 10, totalItems = 100, ...rest } = props as TablePaginationProps;
33+
return (
34+
<Pagination
35+
{...rest}
36+
currentPage={page}
37+
layout={layout}
38+
onPageChange={onPageChange}
39+
itemsPerPage={itemsPerPage}
40+
totalItems={totalItems}
41+
/>
42+
);
43+
}
44+
45+
const { totalPages = 100, ...rest } = props as DefaultPaginationProps;
2946
return (
3047
<Pagination {...rest} currentPage={page} layout={layout} onPageChange={onPageChange} totalPages={totalPages} />
3148
);
@@ -54,11 +71,15 @@ NavWithIcons.args = {
5471
export const Table = Template.bind({});
5572
Table.args = {
5673
layout: "table",
74+
itemsPerPage: 10,
75+
totalItems: 100,
5776
};
5877

5978
export const TableWithIcons = Template.bind({});
6079
TableWithIcons.storyName = "Table with icons";
6180
TableWithIcons.args = {
6281
layout: "table",
6382
showIcons: true,
83+
itemsPerPage: 10,
84+
totalItems: 100,
6485
};

apps/web/examples/pagination/pagination.table.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function Component() {
1717
1818
return (
1919
<div className="flex overflow-x-auto sm:justify-center">
20-
<Pagination layout="table" currentPage={currentPage} totalPages={100} onPageChange={onPageChange} />
20+
<Pagination layout="table" currentPage={currentPage} itemsPerPage={10} totalItems={100} onPageChange={onPageChange} />
2121
</div>
2222
);
2323
}
@@ -30,7 +30,13 @@ export function Component() {
3030

3131
return (
3232
<div className="flex overflow-x-auto sm:justify-center">
33-
<Pagination layout="table" currentPage={currentPage} totalPages={100} onPageChange={onPageChange} />
33+
<Pagination
34+
layout="table"
35+
currentPage={currentPage}
36+
itemsPerPage={10}
37+
totalItems={100}
38+
onPageChange={onPageChange}
39+
/>
3440
</div>
3541
);
3642
}

apps/web/examples/pagination/pagination.tableWithIcons.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function Component() {
1717
1818
return (
1919
<div className="flex overflow-x-auto sm:justify-center">
20-
<Pagination layout="table" currentPage={currentPage} totalPages={100} onPageChange={onPageChange} showIcons />
20+
<Pagination layout="table" currentPage={currentPage} itemsPerPage={10} totalItems={100} onPageChange={onPageChange} showIcons />
2121
</div>
2222
);
2323
}
@@ -30,7 +30,14 @@ export function Component() {
3030

3131
return (
3232
<div className="flex overflow-x-auto sm:justify-center">
33-
<Pagination layout="table" currentPage={currentPage} totalPages={100} onPageChange={onPageChange} showIcons />
33+
<Pagination
34+
layout="table"
35+
currentPage={currentPage}
36+
itemsPerPage={10}
37+
totalItems={100}
38+
onPageChange={onPageChange}
39+
showIcons
40+
/>
3441
</div>
3542
);
3643
}

0 commit comments

Comments
 (0)