-
-
Notifications
You must be signed in to change notification settings - Fork 454
feat/TablePagination - Table with Pagination #1353
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
Open
dhavalveera
wants to merge
10
commits into
themesberg:main
Choose a base branch
from
dhavalveera:feat/table-page
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8d319aa
feat/TablePagination
dhavalveera 84246cc
fix: missed web components
dhavalveera 12c00f5
use client forgot to add in example
dhavalveera 0c076a7
feat: made Table Pagination with 2 options => Numbers & Prev/Next Button
dhavalveera 708b363
fix: small pagination part issue fix
dhavalveera 63774cf
small fix
dhavalveera 2a2af89
fix: removed redundant duplication of Pagination for Table & updated …
dhavalveera c9ec929
fix: removed redundant duplication of Pagination for Table & updated …
dhavalveera 915d52c
fix: based on AI Review
dhavalveera cecd43a
fix: made the Pagiantion COmponent centered
dhavalveera File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
"use client"; | ||
|
||
import { forwardRef, type ComponentPropsWithRef } from "react"; | ||
import { twMerge } from "tailwind-merge"; | ||
import { mergeDeep } from "../../helpers/merge-deep"; | ||
import { getTheme } from "../../theme-store"; | ||
import type { DeepPartial } from "../../types"; | ||
|
||
export interface FlowbiteTablePaginationTheme { | ||
base: string; | ||
totalPages: { | ||
base: string; | ||
pageRange: string; | ||
outOf: string; | ||
}; | ||
page: { | ||
base: string; | ||
previous: string; | ||
pageNo: string; | ||
next: string; | ||
}; | ||
} | ||
|
||
export interface TablePaginationProps extends ComponentPropsWithRef<"tfoot"> { | ||
count: number; | ||
onPageChange: (newPage: number) => void; | ||
page: number; | ||
rowsPerPage: number; | ||
theme?: DeepPartial<FlowbiteTablePaginationTheme>; | ||
} | ||
|
||
export const TablePagination = forwardRef<HTMLDivElement, TablePaginationProps>( | ||
({ count, onPageChange, page, rowsPerPage, className, theme: customTheme = {}, ...props }, ref) => { | ||
const theme = mergeDeep(getTheme().table.pagination, customTheme); | ||
|
||
const nPages = Math.ceil(count / rowsPerPage); | ||
const pageNumbers = [...Array(nPages + 1).keys()].slice(1); | ||
|
||
const goToPrevPage = (): void => { | ||
if (page !== 1) { | ||
onPageChange(page - 1); | ||
} | ||
}; | ||
|
||
const goToNextPage = (): void => { | ||
if (page !== nPages) { | ||
onPageChange(page + 1); | ||
} | ||
}; | ||
|
||
const directPageChange = (customPageNo: number): void => { | ||
if (page !== customPageNo) { | ||
onPageChange(customPageNo); | ||
} | ||
}; | ||
|
||
const getLabelDisplayedRowsTo = (): number => { | ||
if (count === -1) { | ||
return (page + 1) * rowsPerPage; | ||
} | ||
|
||
return rowsPerPage === -1 ? count : Math.min(count, (page + 1) * rowsPerPage); | ||
}; | ||
|
||
return ( | ||
<div className={twMerge(theme.base, className)} aria-label="Table pagination" {...props} ref={ref}> | ||
<span className={theme.totalPages.base}> | ||
Showing{" "} | ||
<span className={theme.totalPages.pageRange}> | ||
{`${count === 0 ? 0 : page * rowsPerPage + 1}-${getLabelDisplayedRowsTo()}`} | ||
</span> | ||
of | ||
<span className={theme.totalPages.outOf}>{count === -1 ? -1 : count}</span> | ||
</span> | ||
|
||
<ul className={theme.page.base}> | ||
<button onClick={goToPrevPage} className={theme.page.previous} disabled={page === 1}> | ||
Previous | ||
</button> | ||
{pageNumbers.map((pgNumber, index) => { | ||
return ( | ||
<button | ||
onClick={() => { | ||
directPageChange(index + 1); | ||
}} | ||
key={index + 1} | ||
className={twMerge(theme.page.pageNo, index + 1 === page ? "bg-blue-50 text-blue-600" : "")} | ||
> | ||
{pgNumber} | ||
</button> | ||
); | ||
})} | ||
<button | ||
role="button" | ||
onClick={goToNextPage} | ||
className={theme.page.next} | ||
disabled={count !== -1 ? page >= Math.ceil(count / rowsPerPage) - 1 : false} | ||
> | ||
Next | ||
</button> | ||
</ul> | ||
</div> | ||
); | ||
}, | ||
); | ||
|
||
TablePagination.displayName = "Table.Pagination"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.