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

99 lines
2.7 KiB
TypeScript
Raw Normal View History

2025-03-27 22:52:35 +07:00
import { ReactElement, ReactNode } from 'react';
import {
useReactTable,
getCoreRowModel,
getPaginationRowModel,
getFilteredRowModel,
} from '@tanstack/react-table';
2025-03-27 15:17:04 +07:00
interface DataTableProps<T> {
data: T[];
headers: {
accessorKey?: keyof T;
header: string;
cell?: (info: any) => ReactNode;
}[];
onRowClick?: (item: T) => void;
2025-03-27 15:17:04 +07:00
}
export const DataTable = <T extends Record<string, any>>({
2025-03-27 15:17:04 +07:00
data,
headers,
onRowClick,
}: DataTableProps<T>): ReactElement => {
const table = useReactTable({
data,
columns: headers.map((header) => ({
accessorKey: header.accessorKey,
header: header.header,
cell: header.cell,
})),
getCoreRowModel: getCoreRowModel(),
getPaginationRowModel: getPaginationRowModel(),
getFilteredRowModel: getFilteredRowModel(),
});
2025-03-27 15:17:04 +07:00
return (
<div className="w-full overflow-x-auto">
2025-03-27 16:29:09 +07:00
<table className="w-full min-w-full text-base">
<thead className="bg-primary-50 mb-3">
{table.getHeaderGroups().map((headerGroup) => (
<tr key={headerGroup.id}>
{headerGroup.headers.map((header) => (
<th key={header.id}>
{header.isPlaceholder ? null : (
<div>
{typeof header.column.columnDef.header === 'function'
? header.column.columnDef.header(header.getContext())
: header.column.columnDef.header}
</div>
)}
</th>
))}
</tr>
))}
2025-03-27 15:17:04 +07:00
</thead>
2025-03-27 16:29:09 +07:00
<tbody className="mt-3">
{table.getRowModel().rows.map((row) => (
2025-03-27 15:17:04 +07:00
<tr
key={row.id}
onClick={() => onRowClick && onRowClick(row.original)}
2025-03-27 15:17:04 +07:00
>
{row.getVisibleCells().map((cell) => (
<td key={cell.id}>{cell.getValue() as ReactNode}</td>
))}
2025-03-27 15:17:04 +07:00
</tr>
))}
</tbody>
</table>
<div>
<button
onClick={() => table.setPageIndex(0)}
disabled={!table.getCanPreviousPage()}
>
{'<<'}
</button>
<button
onClick={() => table.previousPage()}
disabled={!table.getCanPreviousPage()}
>
{'<'}
</button>
<button
onClick={() => table.nextPage()}
disabled={!table.getCanNextPage()}
>
{'>'}
</button>
<button
onClick={() => table.setPageIndex(table.getPageCount() - 1)}
disabled={!table.getCanNextPage()}
>
{'>>'}
</button>
</div>
2025-03-27 15:17:04 +07:00
</div>
);
};
2025-03-27 16:29:09 +07:00
export default DataTable;