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

94 lines
3.4 KiB
TypeScript
Raw Normal View History

import { Table } from '@tanstack/react-table';
2025-03-27 16:41:42 +07:00
import { ArrowLeftOutlined, ArrowRightOutlined } from '@ant-design/icons';
2025-03-27 15:17:04 +07:00
interface PaginationProps<T> {
table: Table<T>;
2025-03-27 15:17:04 +07:00
}
export const Pagination = <T,>({ table }: PaginationProps<T>) => {
2025-03-27 15:17:04 +07:00
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
2025-03-27 16:41:42 +07:00
className="disabled:opacity-50 cursor-pointer"
onClick={() => table.previousPage()}
disabled={!table.getCanPreviousPage()}
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">
{table.getPageCount() <= 8 ? (
Array.from({ length: table.getPageCount() }, (_, index) => (
2025-03-27 16:41:42 +07:00
<button
key={index}
className={`size-[30px] py-[8px] flex items-center justify-center rounded-md cursor-pointer ${
table.getState().pagination.pageIndex === index
? 'bg-primary-500 text-white'
: 'bg-primary-100 hover:bg-primary-200'
}`}
onClick={() => table.setPageIndex(index)}
>
{index + 1}
</button>
))
) : (
<>
<button
onClick={() => table.setPageIndex(0)}
2025-03-27 16:41:42 +07:00
className={`size-[30px] py-[8px] flex items-center justify-center rounded-md cursor-pointer ${
table.getState().pagination.pageIndex === 0
2025-03-27 16:41:42 +07:00
? '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
}`}
>
1
2025-03-27 16:41:42 +07:00
</button>
{table.getState().pagination.pageIndex > 3 && <span>...</span>}
{Array.from(
{ length: 5 },
(_, index) => table.getState().pagination.pageIndex - 2 + index
)
.filter((page) => page > 0 && page < table.getPageCount() - 1)
.map((page) => (
<button
key={page}
onClick={() => table.setPageIndex(page)}
className={`size-[30px] py-[8px] flex items-center justify-center rounded-md cursor-pointer ${
table.getState().pagination.pageIndex === page
? 'bg-primary-500 text-white'
: 'bg-primary-100 hover:bg-primary-200'
}`}
>
{page + 1}
</button>
))}
{table.getState().pagination.pageIndex <
table.getPageCount() - 4 && <span>...</span>}
<button
onClick={() => table.setPageIndex(table.getPageCount() - 1)}
className={`size-[30px] py-[8px] flex items-center justify-center rounded-md cursor-pointer ${
table.getState().pagination.pageIndex ===
table.getPageCount() - 1
? 'bg-primary-500 text-white'
: 'bg-primary-100 hover:bg-primary-200'
}`}
>
{table.getPageCount()}
</button>
</>
2025-03-27 16:41:42 +07:00
)}
</div>
2025-03-27 15:17:04 +07:00
<button
2025-03-27 16:41:42 +07:00
className="disabled:opacity-50 cursor-pointer"
onClick={() => table.nextPage()}
disabled={!table.getCanNextPage()}
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>
);
};