Files
imphnen-frontend-service/libs/ui/src/molecules/pagination/pagination.tsx
T

89 lines
2.3 KiB
TypeScript
Raw Normal View History

2025-03-27 15:17:04 +07:00
import { FC, ReactElement } from 'react';
2025-03-27 16:41:42 +07:00
import { ArrowLeftOutlined, ArrowRightOutlined } from '@ant-design/icons';
2025-03-27 15:17:04 +07:00
export interface PaginationProps {
2025-03-27 15:17:04 +07:00
currentPage: number;
totalPages: number;
onPageChange: (page: number) => void;
}
export const Pagination: FC<PaginationProps> = ({
currentPage,
totalPages,
onPageChange,
}): ReactElement => {
const getPageNumbers = () => {
const pages = [];
if (currentPage > 3) {
pages.push(1);
if (currentPage > 4) {
pages.push('...');
}
}
const startPage = Math.max(1, currentPage - 1);
const endPage = Math.min(totalPages, currentPage + 1);
for (let i = startPage; i <= endPage; i++) {
pages.push(i);
}
if (currentPage < totalPages - 2) {
if (currentPage < totalPages - 3) {
pages.push('...');
}
pages.push(totalPages);
}
return pages;
};
return (
2025-03-27 16:55:54 +07:00
<div className="flex items-center justify-center gap-[40px]">
2025-03-27 15:17:04 +07:00
<button
onClick={() => currentPage > 1 && onPageChange(currentPage - 1)}
disabled={currentPage === 1}
2025-03-27 16:41:42 +07:00
className="disabled:opacity-50 cursor-pointer"
2025-03-27 15:17:04 +07:00
aria-label="Previous page"
>
2025-03-27 16:41:42 +07:00
<ArrowLeftOutlined className="text-[16px] text-neutral-800" />
2025-03-27 15:17:04 +07:00
</button>
2025-03-27 16:41:42 +07:00
<div className="flex gap-4 items-baseline">
{getPageNumbers().map((page, index) =>
typeof page === 'number' ? (
<button
key={index}
onClick={() => onPageChange(page)}
className={`size-[30px] py-[8px] flex items-center justify-center rounded-md cursor-pointer ${
currentPage === page
? 'bg-primary-500 text-white'
2025-03-27 22:28:40 +07:00
: 'bg-primary-100 hover:bg-primary-200'
2025-03-27 16:41:42 +07:00
}`}
>
{page}
</button>
) : (
<span key={index} className="px-1">
{page}
</span>
)
)}
</div>
2025-03-27 15:17:04 +07:00
<button
onClick={() =>
currentPage < totalPages && onPageChange(currentPage + 1)
}
disabled={currentPage === totalPages}
2025-03-27 16:41:42 +07:00
className="disabled:opacity-50 cursor-pointer"
2025-03-27 15:17:04 +07:00
aria-label="Next page"
>
2025-03-27 16:41:42 +07:00
<ArrowRightOutlined className="text-[16px] text-neutral-800" />
2025-03-27 15:17:04 +07:00
</button>
</div>
);
};
export default Pagination;