-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from artsiom-voitas/feat/shadcn-pagination
feat: added shadcn-pagination
- Loading branch information
Showing
7 changed files
with
903 additions
and
392 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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 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,95 @@ | ||
import { | ||
Pagination, | ||
PaginationContent, | ||
PaginationEllipsis, | ||
PaginationItem, | ||
PaginationLink, | ||
PaginationNext, | ||
PaginationPrevious | ||
} from '@/components/ui/pagination'; | ||
|
||
interface ImagesCardPaginationProps { | ||
currentCollection: string; | ||
currentOrder: string; | ||
pagesAmount: number; | ||
currentPage: number; | ||
} | ||
|
||
export function ImagesCardPagination({ | ||
pagesAmount, | ||
currentPage, | ||
currentCollection, | ||
currentOrder | ||
}: ImagesCardPaginationProps) { | ||
const pageNumbers = []; | ||
for (let i = 1; i <= pagesAmount; i++) { | ||
pageNumbers.push(i); | ||
} | ||
const previousPage: number = currentPage > 1 ? currentPage - 1 : 1; | ||
const nextPage: number = currentPage < pagesAmount ? currentPage + 1 : pagesAmount; | ||
|
||
const maxPageNum = 3; | ||
const pageNumLimit = Math.floor(maxPageNum / 2); | ||
|
||
let activePages = pageNumbers.slice( | ||
Math.max(0, currentPage - 1 - pageNumLimit), | ||
Math.min(currentPage - 1 + pageNumLimit + 1, pageNumbers.length) | ||
); | ||
|
||
const renderPages = () => { | ||
const renderedPages = activePages.map((page, idx) => ( | ||
<PaginationItem key={idx}> | ||
<PaginationLink | ||
isActive={currentPage === page} | ||
href={`/?collection=${currentCollection}&page=${page}&order_by=${currentOrder}`}> | ||
{page} | ||
</PaginationLink> | ||
</PaginationItem> | ||
)); | ||
|
||
if (activePages[0] > 1) { | ||
if (activePages[0] !== 2) { | ||
renderedPages.unshift(<PaginationEllipsis key="ellipsis-start" />); | ||
} | ||
renderedPages.unshift( | ||
<PaginationLink | ||
href={`/?collection=${currentCollection}&page=${1}&order_by=${currentOrder}`}> | ||
{1} | ||
</PaginationLink> | ||
); | ||
} | ||
|
||
if (activePages[activePages.length - 1] < pageNumbers.length) { | ||
if (activePages[activePages.length - 1] !== pageNumbers.length - 1) | ||
renderedPages.push(<PaginationEllipsis key="ellipsis-end" />); | ||
renderedPages.push( | ||
<PaginationLink | ||
href={`/?collection=${currentCollection}&page=${pagesAmount}&order_by=${currentOrder}`}> | ||
{pagesAmount} | ||
</PaginationLink> | ||
); | ||
} | ||
|
||
return renderedPages; | ||
}; | ||
|
||
return ( | ||
<Pagination className="my-8"> | ||
<PaginationContent> | ||
<PaginationItem> | ||
<PaginationPrevious | ||
href={`/?collection=${currentCollection}&page=${previousPage}&order_by=${currentOrder}`} | ||
/> | ||
</PaginationItem> | ||
|
||
{renderPages()} | ||
|
||
<PaginationItem> | ||
<PaginationNext | ||
href={`/?collection=${currentCollection}&page=${nextPage}&order_by=${currentOrder}`} | ||
/> | ||
</PaginationItem> | ||
</PaginationContent> | ||
</Pagination> | ||
); | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1,49 +1,104 @@ | ||
'use client'; | ||
|
||
import * as React from 'react'; | ||
import { ChevronLeft, ChevronRight, MoreHorizontal } from 'lucide-react'; | ||
import { useRouter } from 'next/navigation'; | ||
import ReactPaginate from 'react-paginate'; | ||
|
||
interface PaginationProps { | ||
currentCollection: string; | ||
currentOrder: string; | ||
pagesAmount: number; | ||
currentPage: number; | ||
} | ||
|
||
export default function Pagination({ | ||
pagesAmount, | ||
currentPage, | ||
currentCollection, | ||
currentOrder | ||
}: PaginationProps) { | ||
const { push } = useRouter(); | ||
const page: number = currentPage - 1; | ||
|
||
const handlePageClick = (event: any) => { | ||
const newPage = event.selected + 1; | ||
push(`/?collection=${currentCollection}&page=${newPage}&order_by=${currentOrder}`); | ||
}; | ||
|
||
return ( | ||
<ReactPaginate | ||
className="my-8 flex items-center justify-center gap-1 sm:gap-4" | ||
breakLabel={<MoreHorizontal size={18} />} | ||
nextLabel={<ChevronRight size={18} />} | ||
onPageChange={handlePageClick} | ||
marginPagesDisplayed={1} | ||
pageRangeDisplayed={5} | ||
pageCount={pagesAmount} | ||
previousLabel={<ChevronLeft size={18} />} | ||
forcePage={page} | ||
renderOnZeroPageCount={null} | ||
pageClassName={ | ||
'w-6 h-6 sm:w-8 sm:h-8 p-4 flex items-center justify-center hover:opacity-[70%] bg-black dark:bg-white text-white dark:text-black rounded-xl cursor-pointer' | ||
} | ||
activeClassName={'opacity-[50%] hover:cursor-default hover:opacity-[50%]'} | ||
activeLinkClassName={'hover:cursor-default'} | ||
disabledClassName={'opacity-[50%] hover:cursor-default'} | ||
disabledLinkClassName={'hover:cursor-default'} | ||
|
||
import { cn } from '@/lib/utils'; | ||
import Link from 'next/link'; | ||
|
||
import { ButtonProps, buttonVariants } from '@/components/ui/button'; | ||
|
||
const Pagination = ({ className, ...props }: React.ComponentProps<'nav'>) => ( | ||
<nav | ||
role="navigation" | ||
aria-label="pagination" | ||
className={cn('mx-auto flex w-full justify-center', className)} | ||
{...props} | ||
/> | ||
); | ||
|
||
const PaginationContent = React.forwardRef<HTMLUListElement, React.ComponentProps<'ul'>>( | ||
({ className, ...props }, ref) => ( | ||
<ul | ||
ref={ref} | ||
className={cn('flex flex-row items-center gap-1', className)} | ||
{...props} | ||
/> | ||
) | ||
); | ||
PaginationContent.displayName = 'PaginationContent'; | ||
|
||
const PaginationItem = React.forwardRef<HTMLLIElement, React.ComponentProps<'li'>>( | ||
({ className, ...props }, ref) => ( | ||
<li | ||
ref={ref} | ||
className={cn('', className)} | ||
{...props} | ||
/> | ||
); | ||
} | ||
) | ||
); | ||
PaginationItem.displayName = 'PaginationItem'; | ||
|
||
type PaginationLinkProps = { | ||
isActive?: boolean; | ||
} & Pick<ButtonProps, 'size'> & | ||
React.ComponentProps<typeof Link>; | ||
|
||
const PaginationLink = ({ className, isActive, size = 'sm', ...props }: PaginationLinkProps) => ( | ||
<PaginationItem> | ||
<Link | ||
aria-current={isActive ? 'page' : undefined} | ||
className={cn( | ||
buttonVariants({ | ||
variant: isActive ? 'outline' : 'ghost', | ||
size | ||
}), | ||
className | ||
)} | ||
{...props} | ||
/> | ||
</PaginationItem> | ||
); | ||
PaginationLink.displayName = 'PaginationLink'; | ||
|
||
const PaginationPrevious = ({ | ||
className, | ||
...props | ||
}: React.ComponentProps<typeof PaginationLink>) => ( | ||
<PaginationLink | ||
aria-label="Go to previous page" | ||
size="sm" | ||
className={cn('gap-1', className)} | ||
{...props}> | ||
<ChevronLeft className="h-4 w-4" /> | ||
</PaginationLink> | ||
); | ||
PaginationPrevious.displayName = 'PaginationPrevious'; | ||
|
||
const PaginationNext = ({ className, ...props }: React.ComponentProps<typeof PaginationLink>) => ( | ||
<PaginationLink | ||
aria-label="Go to next page" | ||
size="sm" | ||
className={cn('gap-1', className)} | ||
{...props}> | ||
<ChevronRight className="h-4 w-4" /> | ||
</PaginationLink> | ||
); | ||
|
||
const PaginationEllipsis = ({ className, ...props }: React.ComponentProps<'span'>) => ( | ||
<span | ||
aria-hidden | ||
className={cn('flex h-9 w-9 items-center justify-center', className)} | ||
{...props}> | ||
<MoreHorizontal className="h-4 w-4" /> | ||
<span className="sr-only">More pages</span> | ||
</span> | ||
); | ||
|
||
export { | ||
Pagination, | ||
PaginationContent, | ||
PaginationEllipsis, | ||
PaginationItem, | ||
PaginationLink, | ||
PaginationNext, | ||
PaginationPrevious | ||
}; |