From a0ba5fa7c01aff5a2c46a900323a2a96c532599d Mon Sep 17 00:00:00 2001 From: Aman Mahajan Date: Fri, 22 Nov 2024 15:46:31 -0600 Subject: [PATCH] Pass column in `onColumnResize` (#3644) * Pass column in `onColumnResize ` * Add a test --- src/DataGrid.tsx | 2 +- src/hooks/useColumnWidths.ts | 2 +- test/browser/column/resizable.test.tsx | 6 +++++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/DataGrid.tsx b/src/DataGrid.tsx index 16211234e9..dc6f6aeb58 100644 --- a/src/DataGrid.tsx +++ b/src/DataGrid.tsx @@ -187,7 +187,7 @@ export interface DataGridProps extends Sha /** Called when the grid is scrolled */ onScroll?: Maybe<(event: React.UIEvent) => void>; /** Called when a column is resized */ - onColumnResize?: Maybe<(idx: number, width: number) => void>; + onColumnResize?: Maybe<(column: CalculatedColumn, width: number) => void>; /** Called when a column is reordered */ onColumnsReorder?: Maybe<(sourceColumnKey: string, targetColumnKey: string) => void>; diff --git a/src/hooks/useColumnWidths.ts b/src/hooks/useColumnWidths.ts index 57d46f70da..7c4bce746c 100644 --- a/src/hooks/useColumnWidths.ts +++ b/src/hooks/useColumnWidths.ts @@ -96,7 +96,7 @@ export function useColumnWidths( updateMeasuredWidths(columnsToMeasure); }); - onColumnResize?.(column.idx, measuredWidth); + onColumnResize?.(column, measuredWidth); } return { diff --git a/test/browser/column/resizable.test.tsx b/test/browser/column/resizable.test.tsx index df92ca15c9..4e8b7d05d2 100644 --- a/test/browser/column/resizable.test.tsx +++ b/test/browser/column/resizable.test.tsx @@ -64,12 +64,16 @@ test('cannot not resize or auto resize column when resizable is not specified', }); test('should resize column when dragging the handle', async () => { - setup({ columns, rows: [] }); + const onColumnResize = vi.fn(); + setup({ columns, rows: [], onColumnResize }); const [, col2] = getHeaderCells(); const grid = getGrid(); + expect(onColumnResize).not.toHaveBeenCalled(); await expect.element(grid).toHaveStyle({ gridTemplateColumns: '100px 200px' }); await resize({ column: col2.element(), resizeBy: -50 }); await expect.element(grid).toHaveStyle({ gridTemplateColumns: '100px 150px' }); + expect(onColumnResize).toHaveBeenCalledTimes(1); + expect(onColumnResize).toHaveBeenCalledWith(expect.objectContaining(columns[1]), 150); }); test('should use the maxWidth if specified', async () => {