Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions frontend/src/components/data-table/__tests__/columns.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,53 @@ describe("generateColumns", () => {
expect(cell?.props.className).toContain("center");
});

it("should apply text justification to column headers", () => {
const columns = generateColumns({
rowHeaders: [],
selection: null,
fieldTypes,
textJustifyColumns: { name: "right", age: "center" },
});

const mockColumn = (col: (typeof columns)[number]) => ({
id: col.id,
getCanSort: () => true,
getCanFilter: () => false,
getIsSorted: () => false,
getSortIndex: () => -1,
getFilterValue: () => undefined,
columnDef: { meta: col.meta },
});

// Right-justified header should have flex-row-reverse and text-right
const { container: rightContainer } = render(
<TooltipProvider>
{/* eslint-disable-next-line @typescript-eslint/no-explicit-any */}
{(columns[0].header as any)({ column: mockColumn(columns[0]) })}
</TooltipProvider>,
);
const rightHeader = rightContainer.querySelector(
"[data-testid='data-table-sort-button']",
);
expect(rightHeader?.className).toContain("flex-row-reverse");
const rightSpan = rightHeader?.querySelector("span");
expect(rightSpan?.className).toContain("text-right");

// Center-justified header should have text-center but not flex-row-reverse
const { container: centerContainer } = render(
<TooltipProvider>
{/* eslint-disable-next-line @typescript-eslint/no-explicit-any */}
{(columns[1].header as any)({ column: mockColumn(columns[1]) })}
</TooltipProvider>,
);
const centerHeader = centerContainer.querySelector(
"[data-testid='data-table-sort-button']",
);
expect(centerHeader?.className).not.toContain("flex-row-reverse");
const centerSpan = centerHeader?.querySelector("span");
expect(centerSpan?.className).toContain("text-center");
});

it("should not include index column if it exists", () => {
const columns = generateColumns({
rowHeaders: [],
Expand Down
25 changes: 23 additions & 2 deletions frontend/src/components/data-table/column-header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@ interface DataTableColumnHeaderProps<TData, TValue>
extends React.HTMLAttributes<HTMLDivElement> {
column: Column<TData, TValue>;
header: React.ReactNode;
justify?: "left" | "center" | "right";
calculateTopKRows?: CalculateTopKRows;
table?: Table<TData>;
}

export const DataTableColumnHeader = <TData, TValue>({
column,
header,
justify,
className,
calculateTopKRows,
table,
Expand All @@ -88,7 +90,17 @@ export const DataTableColumnHeader = <TData, TValue>({

// No sorting or filtering
if (!column.getCanSort() && !column.getCanFilter()) {
return <div className={cn(className)}>{header}</div>;
return (
<div
className={cn(
className,
justify === "center" && "text-center",
justify === "right" && "text-right",
)}
>
{header}
</div>
);
}

const hasFilter = column.getFilterValue() !== undefined;
Expand All @@ -101,11 +113,20 @@ export const DataTableColumnHeader = <TData, TValue>({
<div
className={cn(
"group flex items-center my-1 space-between w-full select-none gap-2 border hover:border-border border-transparent hover:bg-(--slate-3) data-[state=open]:bg-(--slate-3) data-[state=open]:border-border rounded px-1 -mx-1",
justify === "right" && "flex-row-reverse",
className,
)}
data-testid="data-table-sort-button"
>
<span className="flex-1">{header}</span>
<span
className={cn(
"flex-1",
justify === "center" && "text-center",
justify === "right" && "text-right",
)}
>
{header}
</span>
<span
className={cn(
"h-5 py-1 px-1",
Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/data-table/columns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ export function generateColumns<T>({
<DataTableColumnHeader
header={headerWithTooltip}
column={column}
justify={textJustifyColumns?.[key]}
calculateTopKRows={calculateTopKRows}
table={table}
/>
Expand Down
Loading