'use client'; import { useState } from 'react'; import { ColumnDef, flexRender, getCoreRowModel, getPaginationRowModel, useReactTable, } from '@tanstack/react-table'; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from '@workspace/ui/components/table'; import { Student } from './columns'; import { StudentDetailsModal } from './student-details-modal.tsx'; import { columns } from './columns'; interface DataTableProps { data: Student[]; } export function DataTable({ data }: DataTableProps) { const [selectedStudent, setSelectedStudent] = useState(null); const [isModalOpen, setIsModalOpen] = useState(false); const table = useReactTable({ data, columns, getCoreRowModel: getCoreRowModel(), getPaginationRowModel: getPaginationRowModel(), }); const handleRowClick = (student: Student) => { setSelectedStudent(student); setIsModalOpen(true); }; const handleCloseModal = () => { setIsModalOpen(false); setSelectedStudent(null); }; return ( <>
{table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => { return ( {header.isPlaceholder ? null : flexRender(header.column.columnDef.header, header.getContext())} ); })} ))} {table.getRowModel().rows?.length ? ( table.getRowModel().rows.map((row) => ( handleRowClick(row.original as Student)} > {row.getVisibleCells().map((cell) => ( {flexRender(cell.column.columnDef.cell, cell.getContext())} ))} )) ) : ( No students found. )}
{/* Pagination */}
Showing {table.getFilteredRowModel().rows.length} of{' '} {table.getFilteredRowModel().rows.length} results
{/* Student Details Modal */} {selectedStudent && ( )} ); }