diff --git a/libs/ui/src/molecules/index.ts b/libs/ui/src/molecules/index.ts index 14dcfcb..dbe5a6d 100644 --- a/libs/ui/src/molecules/index.ts +++ b/libs/ui/src/molecules/index.ts @@ -1 +1,2 @@ export * from './input-form'; +export * from './pagination'; diff --git a/libs/ui/src/molecules/pagination/index.ts b/libs/ui/src/molecules/pagination/index.ts index cb0ff5c..cb72765 100644 --- a/libs/ui/src/molecules/pagination/index.ts +++ b/libs/ui/src/molecules/pagination/index.ts @@ -1 +1 @@ -export {}; +export * from './pagination'; diff --git a/libs/ui/src/molecules/pagination/pagination.tsx b/libs/ui/src/molecules/pagination/pagination.tsx index 8a02263..7518854 100644 --- a/libs/ui/src/molecules/pagination/pagination.tsx +++ b/libs/ui/src/molecules/pagination/pagination.tsx @@ -1,82 +1,89 @@ -import { FC, ReactElement } from 'react'; +import { Table } from '@tanstack/react-table'; import { ArrowLeftOutlined, ArrowRightOutlined } from '@ant-design/icons'; -export interface PaginationProps { - currentPage: number; - totalPages: number; - onPageChange: (page: number) => void; +interface PaginationProps { + table: Table; } -export const Pagination: FC = ({ - 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; - }; - +export const Pagination = ({ table }: PaginationProps) => { return (
- {getPageNumbers().map((page, index) => - typeof page === 'number' ? ( + {table.getPageCount() <= 8 ? ( + Array.from({ length: table.getPageCount() }, (_, index) => ( + )) + ) : ( + <> + - ) : ( - - {page} - - ) + {table.getState().pagination.pageIndex > 3 && ...} + {Array.from( + { length: 5 }, + (_, index) => table.getState().pagination.pageIndex - 2 + index + ) + .filter((page) => page > 0 && page < table.getPageCount() - 1) + .map((page) => ( + + ))} + {table.getState().pagination.pageIndex < + table.getPageCount() - 4 && ...} + + )}
); }; - -export default Pagination; diff --git a/libs/ui/src/organisms/datatable/datatable.tsx b/libs/ui/src/organisms/datatable/datatable.tsx index 545bea7..dddbcf5 100644 --- a/libs/ui/src/organisms/datatable/datatable.tsx +++ b/libs/ui/src/organisms/datatable/datatable.tsx @@ -6,6 +6,8 @@ import { flexRender, ColumnDef, } from '@tanstack/react-table'; +import { Pagination } from '../../molecules'; + import React from 'react'; interface DataTableProps { @@ -36,42 +38,45 @@ export const DataTable = ({ }); return ( -
- - - {table.getHeaderGroups().map((headerGroup) => ( - - {headerGroup.headers.map((header) => ( - - ))} - - ))} - - - {table.getRowModel().rows.map((row) => ( - - {row.getVisibleCells().map((cell, index) => ( - - ))} - - ))} - -
- {header.isPlaceholder - ? null - : flexRender( - header.column.columnDef.header, - header.getContext() - )} -
- {flexRender(cell.column.columnDef.cell, cell.getContext())} -
+
+
+ + + {table.getHeaderGroups().map((headerGroup) => ( + + {headerGroup.headers.map((header) => ( + + ))} + + ))} + + + {table.getRowModel().rows.map((row) => ( + + {row.getVisibleCells().map((cell, index) => ( + + ))} + + ))} + +
+ {header.isPlaceholder + ? null + : flexRender( + header.column.columnDef.header, + header.getContext() + )} +
+ {flexRender(cell.column.columnDef.cell, cell.getContext())} +
+
+
); };