Files
imphnen-frontend-service/libs/ui/src/organisms/datatable/datatable.tsx
T

87 lines
2.3 KiB
TypeScript
Raw Normal View History

import {
2025-03-28 18:17:13 +07:00
PaginationState,
useReactTable,
getCoreRowModel,
getPaginationRowModel,
2025-03-28 18:17:13 +07:00
flexRender,
ColumnDef,
2025-03-28 21:46:10 +07:00
Table,
} from '@tanstack/react-table';
import { Pagination } from '../../molecules';
2025-03-28 18:17:13 +07:00
import React from 'react';
2025-03-27 15:17:04 +07:00
interface DataTableProps<T> {
data: T[];
2025-03-28 18:17:13 +07:00
columns: ColumnDef<T>[];
2025-03-28 21:46:10 +07:00
table: Table<T>;
2025-03-28 18:17:13 +07:00
pageSize?: number;
2025-03-27 15:17:04 +07:00
}
2025-03-28 18:17:13 +07:00
export const DataTable = <T,>({
2025-03-27 15:17:04 +07:00
data,
2025-03-28 18:17:13 +07:00
columns,
pageSize = 9,
}: DataTableProps<T>) => {
const [pagination, setPagination] = React.useState<PaginationState>({
pageIndex: 0,
pageSize,
});
const table = useReactTable({
data,
2025-03-28 18:17:13 +07:00
columns,
state: {
pagination,
},
getCoreRowModel: getCoreRowModel(),
getPaginationRowModel: getPaginationRowModel(),
2025-03-28 18:17:13 +07:00
onPaginationChange: setPagination,
});
2025-03-28 18:17:13 +07:00
2025-03-27 15:17:04 +07:00
return (
<div className="flex flex-col gap-8">
<div className="w-full overflow-x-auto">
<table className="w-full min-w-full text-base">
<thead className="bg-primary-50 mb-3 text-left text-nowrap">
{table.getHeaderGroups().map((headerGroup) => (
<tr key={headerGroup.id}>
{headerGroup.headers.map((header) => (
<th
key={header.id}
2025-03-28 22:41:55 +07:00
className="py-4 px-5 font-normal first:rounded-l-lg last:rounded-r-lg"
>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext()
)}
</th>
))}
</tr>
))}
</thead>
2025-03-28 22:41:55 +07:00
<tbody>
{table.getRowModel().rows.map((row) => (
<tr key={row.id} className="bg-primary-100 odd:bg-white">
{row.getVisibleCells().map((cell, index) => (
<td
key={cell.id}
className="py-3 px-5 first:rounded-l-lg last:rounded-r-lg"
>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</td>
))}
</tr>
))}
</tbody>
</table>
</div>
<Pagination table={table} />
2025-03-27 15:17:04 +07:00
</div>
);
};
2025-03-27 16:29:09 +07:00
export default DataTable;