2025-03-28 00:19:01 +07:00
|
|
|
import {
|
2025-03-28 18:17:13 +07:00
|
|
|
PaginationState,
|
2025-03-28 00:19:01 +07:00
|
|
|
useReactTable,
|
|
|
|
|
getCoreRowModel,
|
|
|
|
|
getPaginationRowModel,
|
2025-03-28 18:17:13 +07:00
|
|
|
flexRender,
|
|
|
|
|
ColumnDef,
|
2025-03-28 00:19:01 +07:00
|
|
|
} from '@tanstack/react-table';
|
2025-03-28 18:50:17 +07:00
|
|
|
import { Pagination } from '../../molecules';
|
|
|
|
|
|
2025-03-28 18:17:13 +07:00
|
|
|
import React from 'react';
|
2025-03-27 15:17:04 +07:00
|
|
|
|
2025-03-27 17:22:04 +07:00
|
|
|
interface DataTableProps<T> {
|
|
|
|
|
data: T[];
|
2025-03-28 18:17:13 +07:00
|
|
|
columns: ColumnDef<T>[];
|
|
|
|
|
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,
|
|
|
|
|
});
|
|
|
|
|
|
2025-03-28 00:19:01 +07:00
|
|
|
const table = useReactTable({
|
|
|
|
|
data,
|
2025-03-28 18:17:13 +07:00
|
|
|
columns,
|
|
|
|
|
state: {
|
|
|
|
|
pagination,
|
|
|
|
|
},
|
2025-03-28 00:19:01 +07:00
|
|
|
getCoreRowModel: getCoreRowModel(),
|
|
|
|
|
getPaginationRowModel: getPaginationRowModel(),
|
2025-03-28 18:17:13 +07:00
|
|
|
onPaginationChange: setPagination,
|
2025-03-28 00:19:01 +07:00
|
|
|
});
|
2025-03-28 18:17:13 +07:00
|
|
|
|
2025-03-27 15:17:04 +07:00
|
|
|
return (
|
2025-03-28 18:50:17 +07:00
|
|
|
<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">
|
|
|
|
|
{table.getHeaderGroups().map((headerGroup) => (
|
|
|
|
|
<tr key={headerGroup.id}>
|
|
|
|
|
{headerGroup.headers.map((header) => (
|
|
|
|
|
<th
|
|
|
|
|
key={header.id}
|
|
|
|
|
className="py-3 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>
|
|
|
|
|
<tbody className="mt-3">
|
|
|
|
|
{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;
|