"use client"; import type { Row } from "@tanstack/react-table"; import { flexRender } from "@tanstack/react-table"; import type { Table as ReactTableType } from "@tanstack/react-table"; import { useMemo } from "react"; import { useVirtual } from "react-virtual"; import classNames from "@calcom/lib/classNames"; import { Icon } from "../icon"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "../table/TableNew"; export interface DataTableProps { table: ReactTableType; tableContainerRef: React.RefObject; isPending?: boolean; onRowMouseclick?: (row: Row) => void; onScroll?: (e: React.UIEvent) => void; tableOverlay?: React.ReactNode; variant?: "default" | "compact"; "data-testid"?: string; children?: React.ReactNode; } export function DataTable({ table, tableContainerRef, isPending, variant, onRowMouseclick, onScroll, children, ...rest }: DataTableProps & React.ComponentPropsWithoutRef<"div">) { const { rows } = table.getRowModel(); const rowVirtualizer = useVirtual({ parentRef: tableContainerRef, size: rows.length, overscan: 10, }); const { virtualItems: virtualRows, totalSize } = rowVirtualizer; const paddingTop = virtualRows.length > 0 ? virtualRows?.[0]?.start || 0 : 0; const paddingBottom = virtualRows.length > 0 ? totalSize - (virtualRows?.[virtualRows.length - 1]?.end || 0) : 0; const columnSizeVars = useMemo(() => { const headers = table.getFlatHeaders(); const colSizes: { [key: string]: number } = {}; for (let i = 0; i < headers.length; i++) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const header = headers[i]!; colSizes[`--header-${header.id}-size`] = header.getSize(); colSizes[`--col-${header.column.id}-size`] = header.column.getSize(); } return colSizes; }, [table.getState().columnSizingInfo, table.getState().columnSizing]); return (
{table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => { const meta = header.column.columnDef.meta; return (
{header.isPlaceholder ? null : flexRender(header.column.columnDef.header, header.getContext())} {header.column.getIsSorted() && ( )}
); })}
))}
{paddingTop > 0 && ( )} {virtualRows && !isPending ? ( virtualRows.map((virtualRow) => { const row = rows[virtualRow.index] as Row; return ( onRowMouseclick && onRowMouseclick(row)} className={classNames( onRowMouseclick && "hover:cursor-pointer", variant === "compact" && "!border-0", "group" )}> {row.getVisibleCells().map((cell) => { const column = table.getColumn(cell.column.id); const meta = column?.columnDef.meta; return ( {flexRender(cell.column.columnDef.cell, cell.getContext())} ); })} ); }) ) : ( No results. )} {paddingBottom > 0 && ( )}
{children}
); }