Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Add column resizing to tables

Steven Tey8193e6ff74b7
diff --git a/apps/web/ui/analytics/events/events-table.tsx b/apps/web/ui/analytics/events/events-table.tsx
index 11b5a66138..78f13f25d8 100644
--- a/apps/web/ui/analytics/events/events-table.tsx
+++ b/apps/web/ui/analytics/events/events-table.tsx
@@ -121,8 +121,9 @@ export default function EventsTable({
id: "link",
header: "Link",
accessorKey: "link",
- minSize: 200,
- maxSize: 150,
+ minSize: 150,
+ size: 200,
+ maxSize: 400,
meta: {
filterParams: ({ getValue }) => ({
domain: getValue().domain,
@@ -151,8 +152,9 @@ export default function EventsTable({
id: "customer",
header: "Customer",
accessorKey: "customer",
- minSize: 230,
- maxSize: 200,
+ minSize: 150,
+ size: 230,
+ maxSize: 400,
cell: ({ getValue }) => <CustomerRowItem customer={getValue()} />,
},
{
@@ -432,7 +434,17 @@ export default function EventsTable({
header: ({ table }) => <EditColumnsButton table={table} />,
cell: ({ row }) => <RowMenuButton row={row} />,
},
- ].filter((c) => c.id === "menu" || eventColumns[tab].all.includes(c.id)),
+ ]
+ .filter((c) => c.id === "menu" || eventColumns[tab].all.includes(c.id))
+ .map((col) => ({
+ ...col,
+ enableResizing: true,
+ size:
+ col.size ||
+ (col.id === "menu" ? 43 : Math.max(200, col.minSize || 100)),
+ minSize: col.minSize || (col.id === "menu" ? 43 : 100),
+ maxSize: col.maxSize || (col.id === "menu" ? 43 : 1000),
+ })),
[tab],
);
@@ -499,7 +511,6 @@ export default function EventsTable({
columnPinning: { right: ["menu"] },
cellRight: (cell) => {
const meta = cell.column.columnDef.meta as ColumnMeta | undefined;
-
return (
meta?.filterParams && <FilterButton set={meta.filterParams(cell)} />
);
diff --git a/packages/ui/src/table/table.tsx b/packages/ui/src/table/table.tsx
index 979dc78d0f..43e813faa5 100644
--- a/packages/ui/src/table/table.tsx
+++ b/packages/ui/src/table/table.tsx
@@ -4,6 +4,7 @@ import {
Column,
ColumnDef,
ColumnPinningState,
+ ColumnResizeMode,
flexRender,
getCoreRowModel,
PaginationState,
@@ -35,7 +36,17 @@ const tableCellClassName = (columnId: string, clickable?: boolean) =>
clickable && "group-hover/row:bg-neutral-50 transition-colors duration-75",
]);
-type UseTableProps<T> = {
+const resizingClassName = cn([
+ "absolute right-0 top-0 h-full w-1 cursor-col-resize select-none touch-none",
+ "bg-neutral-300/50",
+ "opacity-0 group-hover/resize:opacity-100 hover:opacity-100",
+ "group-hover/resize:bg-neutral-300 hover:bg-neutral-400",
+ "transition-all duration-200",
+ "-mr-px",
+ "after:absolute after:right-0 after:top-0 after:h-full after:w-4 after:translate-x-1/2",
+]);
+
+type BaseTableProps<T> = {
columns: ColumnDef<T, any>[];
data: T[];
loading?: boolean;
@@ -55,6 +66,8 @@ type UseTableProps<T> = {
columnPinning?: ColumnPinningState;
resourceName?: (plural: boolean) => string;
onRowClick?: (row: Row<T>, e: MouseEvent) => void;
+ enableColumnResizing?: boolean;
+ columnResizeMode?: ColumnResizeMode;
// Row selection
getRowId?: (row: T) => string;
@@ -67,19 +80,29 @@ type UseTableProps<T> = {
scrollWrapperClassName?: string;
thClassName?: string | ((columnId: string) => string);
tdClassName?: string | ((columnId: string) => string);
-} & (
- | {
- pagination?: PaginationState;
- onPaginationChange?: Dispatch<SetStateAction<PaginationState>>;
- rowCount: number;
- }
- | { pagination?: never; onPaginationChange?: never; rowCount?: never }
-);
+};
+
+type UseTableProps<T> = BaseTableProps<T> &
+ (
+ | {
+ pagination?: PaginationState;
+ onPaginationChange?: Dispatch<SetStateAction<PaginationState>>;
+ rowCount: number;
+ }
+ | { pagination?: never; onPaginationChange?: never; rowCount?: never }
+ );
-type TableProps<T> = UseTableProps<T> &
+type TableProps<T> = BaseTableProps<T> &
PropsWithChildren<{
table: TableType<T>;
- }>;
+ }> &
+ (
+ | {
+ pagination?: PaginationState;
+ rowCount: number;
+ }
+ | { pagination?: never; rowCount?: never }
+ );
export function useTable<T extends any>(
props: UseTableProps<T>,
@@ -93,6 +116,8 @@ export function useTable<T extends any>(
pagination,
onPaginationChange,
getRowId,
+ enableColumnResizing = true,
+ columnResizeMode = "onChange",
} = props;
const [columnVisibility, setColumnVisibility] = useState<VisibilityState>(
@@ -126,9 +151,10 @@ export function useTable<T extends any>(
rowCount,
columns,
defaultColumn: {
- minSize: 120,
- size: 0,
- maxSize: 300,
+ minSize: 100,
+ size: 200,
+ maxSize: 1000,
+ enableResizing: true,
...defaultColumn,
},
getCoreRowModel: getCoreRowModel(),
@@ -145,12 +171,15 @@ export function useTable<T extends any>(
autoResetPageIndex: false,
manualSorting: true,
getRowId,
+ enableColumnResizing,
+ columnResizeMode,
});
return {
...props,
columnVisibility,
table,
+ enableColumnResizing,
};
}
@@ -176,6 +205,7 @@ export function Table<T>({
onRowClick,
rowCount,
children,
+ enableColumnResizing = true,
}: TableProps<T>) {
return (
<div
@@ -195,13 +225,23 @@ export function Table<T>({
className={cn(
[
"group/table w-full border-separate border-spacing-0 transition-[border-spacing,margin-top]",
- // Remove side borders from table to avoid interfering with outer border
- "[&_tr>*:first-child]:border-l-transparent", // Left column
- "[&_tr>*:last-child]:border-r-transparent", // Right column
- "[&_tr>*:last-child]:border-r-transparent", // Bottom column
+ "[&_tr>*:first-child]:border-l-transparent",
+ "[&_tr>*:last-child]:border-r-transparent",
+ "[&_tr>*:last-child]:border-r-transparent",
+ "[&_th]:relative [&_th]:select-none",
+ enableColumnResizing && "[&_th]:group/resize",
],
className,
)}
+ style={{
+ width: "100%",
+ tableLayout: "fixed",
+ ...(enableColumnResizing && {
+ minWidth: table
+ .getVisibleLeafColumns()
+ .reduce((acc, column) => acc + column.getSize(), 0),
+ }),
+ }}
>
<thead>
{table.getHeaderGroups().map((headerGroup) => (
@@ -215,9 +255,10 @@ export function Table<T>({
return (
<th
key={header.id}
+ colSpan={header.colSpan}
className={cn(
tableCellClassName(header.id),
- "select-none font-medium",
+ "group select-none font-medium",
getCommonPinningClassNames(
header.column,
!table.getRowModel().rows.length,
@@ -225,11 +266,10 @@ export function Table<T>({
typeof thClassName === "function"
? thClassName(header.column.id)
: thClassName,
+ enableColumnResizing && "relative",
)}
style={{
- minWidth: header.column.columnDef.minSize,
- maxWidth: header.column.columnDef.maxSize,
- width: header.column.columnDef.size || "auto",
+ width: header.getSize(),
...getCommonPinningStyles(header.column),
}}
>
@@ -252,25 +292,29 @@ export function Table<T>({
}),
})}
>
- <span>
- {header.isPlaceholder
- ? null
- : flexRender(
- header.column.columnDef.header,
- header.getContext(),
- )}
- </span>
- {isSortableColumn && (
- <SortOrder
- order={
- sortBy === header.column.id
- ? sortOrder || "desc"
- : null
- }
- />
+ {flexRender(
+ header.column.columnDef.header,
+ header.getContext(),
)}
+ {isSortableColumn &&
+ sortBy === header.column.id && (
+ <SortOrder
+ className="h-3 w-3 shrink-0"
+ order={sortOrder || "desc"}
+ />
+ )}
</ButtonOrDiv>
</div>
+ {enableColumnResizing &&
+ header.column.getCanResize() &&
+ header.column.id !== "menu" && (
+ <div
+ onMouseDown={header.getResizeHandler()}
+ onTouchStart={header.getResizeHandler()}
+ onClick={(e) => e.stopPropagation()}
+ className={resizingClassName}
+ />
+ )}
</th>
);
})}
@@ -284,7 +328,6 @@ export function Table<T>({
className={cn(
"group/row",
onRowClick && "cursor-pointer select-none",
- // hacky fix: if there are more than 8 rows, remove the bottom border from the last row
table.getRowModel().rows.length > 8 &&
row.index === table.getRowModel().rows.length - 1 &&
"[&_td]:border-b-0",
@@ -292,7 +335,6 @@ export function Table<T>({
onClick={
onRowClick
? (e) => {
- // Ignore if click is on an interactive child
if (isClickOnInteractiveChild(e)) return;
onRowClick(row, e);
}
@@ -314,9 +356,7 @@ export function Table<T>({
: tdClassName,
)}
style={{
- minWidth: cell.column.columnDef.minSize,
- maxWidth: cell.column.columnDef.maxSize,
- width: cell.column.columnDef.size || "auto",
+ width: cell.column.getSize(),
...getCommonPinningStyles(cell.column),
}}
>