fix: provide fixed widths for DataTable columns (#17853)

Co-authored-by: Omar López <[email protected]>
This commit is contained in:
Eunjae LeeandOmar López authored and GitHub committed 2024-11-26 20:17:33 +00:00
1 parent f81716fbb2
commit e22fec93b1
7 files changed
+71 -43

No files matched your search

@@ -3,8 +3,8 @@
import type { Row } from "@tanstack/react-table";
import { flexRender } from "@tanstack/react-table";
import type { Table as ReactTableType } from "@tanstack/react-table";
import { useVirtualizer } from "@tanstack/react-virtual";
import { useMemo } from "react";
import { useVirtual } from "react-virtual";
import classNames from "@calcom/lib/classNames";
import { Icon, TableNew, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@calcom/ui";
@@ -32,15 +32,20 @@ export function DataTable<TData, TValue>({
}: DataTableProps<TData, TValue> & React.ComponentPropsWithoutRef<"div">) {
const { rows } = table.getRowModel();
const rowVirtualizer = useVirtual({
parentRef: tableContainerRef,
size: rows.length,
// https://stackblitz.com/github/tanstack/table/tree/main/examples/react/virtualized-infinite-scrolling
const rowVirtualizer = useVirtualizer({
count: rows.length,
estimateSize: () => 100,
getScrollElement: () => tableContainerRef.current,
// measure dynamic row height, except in firefox because it measures table border height incorrectly
measureElement:
typeof window !== "undefined" && navigator.userAgent.indexOf("Firefox") === -1
? (element) => element?.getBoundingClientRect().height
: undefined,
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 virtualRows = rowVirtualizer.getVirtualItems();
const columnSizeVars = useMemo(() => {
const headers = table.getFlatHeaders();
@@ -52,14 +57,11 @@ export function DataTable<TData, TValue>({
colSizes[`--col-${header.column.id}-size`] = header.column.getSize();
}
return colSizes;
}, [table.getState().columnSizingInfo, table.getState().columnSizing]);
}, [table.getFlatHeaders(), table.getState().columnSizingInfo, table.getState().columnSizing]);
return (
<div
className={classNames(
"grid h-[85dvh]", // Set a fixed height for the container
rest.className
)}
className={classNames("grid", rest.className)}
style={{
gridTemplateRows: "auto 1fr auto",
gridTemplateAreas: "'header' 'body' 'footer'",
@@ -70,12 +72,15 @@ export function DataTable<TData, TValue>({
<div
ref={tableContainerRef}
onScroll={onScroll}
className="scrollbar-thin border-subtle relative h-full overflow-auto rounded-md border"
className={classNames(
"relative h-[80dvh] overflow-auto", // Set a fixed height for the container
"scrollbar-thin border-subtle relative rounded-md border"
)}
style={{ gridArea: "body" }}>
<TableNew className="border-0">
<TableNew className="grid border-0">
<TableHeader className="bg-subtle sticky top-0 z-10">
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id}>
<TableRow key={headerGroup.id} className="flex w-full">
{headerGroup.headers.map((header) => {
const meta = header.column.columnDef.meta;
return (
@@ -87,8 +92,9 @@ export function DataTable<TData, TValue>({
width: `calc(var(--header-${header?.id}-size) * 1px)`,
}}
className={classNames(
"flex shrink-0 items-center",
header.column.getCanSort() ? "cursor-pointer select-none" : "",
meta?.sticky && "bg-subtle sticky top-0 z-20"
meta?.sticky && "sticky top-0 z-20"
)}>
<div className="flex items-center" onClick={header.column.getToggleSortingHandler()}>
{header.isPlaceholder
@@ -112,20 +118,23 @@ export function DataTable<TData, TValue>({
</TableRow>
))}
</TableHeader>
<TableBody>
{paddingTop > 0 && (
<tr>
<td style={{ height: `${paddingTop}px` }} />
</tr>
)}
<TableBody className="relative grid" style={{ height: `${rowVirtualizer.getTotalSize()}px` }}>
{virtualRows && !isPending ? (
virtualRows.map((virtualRow) => {
const row = rows[virtualRow.index] as Row<TData>;
return (
<TableRow
ref={(node) => rowVirtualizer.measureElement(node)} //measure dynamic row height
key={row.id}
data-index={virtualRow.index} //needed for dynamic row height measurement
data-state={row.getIsSelected() && "selected"}
onClick={() => onRowMouseclick && onRowMouseclick(row)}
style={{
display: "flex",
position: "absolute",
transform: `translateY(${virtualRow.start}px)`, //this should always be a `style` as it changes on scroll
width: "100%",
}}
className={classNames(
onRowMouseclick && "hover:cursor-pointer",
variant === "compact" && "!border-0",
@@ -143,6 +152,7 @@ export function DataTable<TData, TValue>({
width: `calc(var(--col-${cell.column.id}-size) * 1px)`,
}}
className={classNames(
"flex shrink-0 items-center overflow-auto",
variant === "compact" && "p-1.5",
meta?.sticky && "group-hover:bg-muted bg-default sticky"
)}>
@@ -160,11 +170,6 @@ export function DataTable<TData, TValue>({
</TableCell>
</TableRow>
)}
{paddingBottom > 0 && (
<tr>
<td style={{ height: `${paddingBottom}px` }} />
</tr>
)}
</TableBody>
</TableNew>
</div>