Skip to content

Commit

Permalink
Take cell padding into account when measuring column title width (#908)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jipark authored Mar 7, 2024
1 parent 71bb990 commit 73e63f5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/core/src/data-editor/use-column-sizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function measureColumn(
}
}
}
max = Math.max(max, ctx.measureText(c.title).width + 16 + (c.icon === undefined ? 0 : 28));
max = Math.max(max, ctx.measureText(c.title).width + theme.cellHorizontalPadding * 2 + (c.icon === undefined ? 0 : 28));
const final = Math.max(Math.ceil(minColumnWidth), Math.min(Math.floor(maxColumnWidth), Math.ceil(max)));

return {
Expand Down
38 changes: 38 additions & 0 deletions packages/core/test/use-column-sizer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,44 @@ describe("use-column-sizer", () => {
expect(result.current.sizedColumns[0].width).toBe(212);
});

it("Measures column width based on title if longer than data", async () => {
const columns: GridColumn[] = [
{
title: "Some very very long title that exceeds displayData width",
id: "ColumnA",
},
{
title: "Short title",
id: "ColumnB",
},
];

const { result } = renderHook(() =>
useColumnSizer(
columns,
500,
getShortCellsForSelection,
400,
20,
500,
mergeAndRealizeTheme(theme, {cellHorizontalPadding: 12}),
getCellRenderer,
abortController
)
);

const columnA = result.current.sizedColumns.find(col => col.title === "Some very very long title that exceeds displayData width");
const columnB = result.current.sizedColumns.find(col => col.title === "Short title");

expect(columnA).toBeDefined();
expect(columnB).toBeDefined();

// Width of column title plus twice the cellHorizontalPadding
expect(columnA?.width).toBe(80);
// Maximum width of cell data
expect(columnB?.width).toBe(40);
});

it("Measures the last row", async () => {
// eslint-disable-next-line sonarjs/no-identical-functions
renderHook(() =>
Expand Down

0 comments on commit 73e63f5

Please sign in to comment.