From cf888022886d643445fe179a90dc49f17609fa67 Mon Sep 17 00:00:00 2001 From: Aman Mahajan Date: Mon, 2 Oct 2023 11:02:10 -0500 Subject: [PATCH] Fix drag handle position when frozen column is resized (#3352) Fix drag handle posistion when frozen column is resized --- src/DataGrid.tsx | 18 +++++++++++++++--- src/DragHandle.tsx | 12 ++++++------ src/hooks/useCalculatedColumns.ts | 11 ++++------- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/DataGrid.tsx b/src/DataGrid.tsx index f9a1b5b6b8..28e9ef41d8 100644 --- a/src/DataGrid.tsx +++ b/src/DataGrid.tsx @@ -283,6 +283,15 @@ function DataGrid( const [draggedOverRowIdx, setOverRowIdx] = useState(undefined); const [scrollToPosition, setScrollToPosition] = useState(null); + const getColumnWidth = useCallback( + (column: CalculatedColumn) => { + return ( + resizedColumnWidths.get(column.key) ?? measuredColumnWidths.get(column.key) ?? column.width + ); + }, + [measuredColumnWidths, resizedColumnWidths] + ); + const [gridRef, gridWidth, gridHeight] = useGridDimensions(); const { columns, @@ -297,8 +306,7 @@ function DataGrid( } = useCalculatedColumns({ rawColumns, defaultColumnOptions, - measuredColumnWidths, - resizedColumnWidths, + getColumnWidth, scrollLeft, viewportWidth: gridWidth, enableVirtualization @@ -830,11 +838,15 @@ function DataGrid( return; } + const column = columns[selectedPosition.idx]; + const columnWidth = getColumnWidth(column); + return ( extends Pick, 'rows' | 'onRowsChange'> { gridRowStart: number; - columns: readonly CalculatedColumn[]; + column: CalculatedColumn; + columnWidth: number | string; selectedPosition: SelectCellState; latestDraggedOverRowIdx: React.MutableRefObject; isCellEditable: (position: Position) => boolean; @@ -47,7 +48,8 @@ interface Props extends Pick, 'rows' | 'onRowsChange export default function DragHandle({ gridRowStart, rows, - columns, + column, + columnWidth, selectedPosition, latestDraggedOverRowIdx, isCellEditable, @@ -58,7 +60,6 @@ export default function DragHandle({ setDraggedOverRowIdx }: Props) { const { idx, rowIdx } = selectedPosition; - const column = columns[idx]; function handleMouseDown(event: React.MouseEvent) { // keep the focus on the cell @@ -99,7 +100,6 @@ export default function DragHandle({ } function updateRows(startRowIdx: number, endRowIdx: number) { - const column = columns[idx]; const sourceRow = rows[rowIdx]; const updatedRows = [...rows]; const indexes: number[] = []; @@ -127,8 +127,8 @@ export default function DragHandle({ ...style, gridRowStart, insetInlineStart: - style.insetInlineStart && typeof column.width === 'number' - ? `calc(${style.insetInlineStart} + ${column.width}px - var(--rdg-drag-handle-size))` + style.insetInlineStart && typeof columnWidth === 'number' + ? `calc(${style.insetInlineStart} + ${columnWidth}px - var(--rdg-drag-handle-size))` : undefined }} className={clsx(cellDragHandleClassname, column.frozen && cellDragHandleFrozenClassname)} diff --git a/src/hooks/useCalculatedColumns.ts b/src/hooks/useCalculatedColumns.ts index dfb8ef8363..0bc5b9ff26 100644 --- a/src/hooks/useCalculatedColumns.ts +++ b/src/hooks/useCalculatedColumns.ts @@ -32,16 +32,14 @@ interface CalculatedColumnsArgs { defaultColumnOptions: DataGridProps['defaultColumnOptions']; viewportWidth: number; scrollLeft: number; - measuredColumnWidths: ReadonlyMap; - resizedColumnWidths: ReadonlyMap; + getColumnWidth: (column: CalculatedColumn) => string | number; enableVirtualization: boolean; } export function useCalculatedColumns({ rawColumns, defaultColumnOptions, - measuredColumnWidths, - resizedColumnWidths, + getColumnWidth, viewportWidth, scrollLeft, enableVirtualization @@ -177,8 +175,7 @@ export function useCalculatedColumns({ const templateColumns: string[] = []; for (const column of columns) { - let width = - resizedColumnWidths.get(column.key) ?? measuredColumnWidths.get(column.key) ?? column.width; + let width = getColumnWidth(column); if (typeof width === 'number') { width = clampColumnWidth(width, column); @@ -205,7 +202,7 @@ export function useCalculatedColumns({ } return { templateColumns, layoutCssVars, totalFrozenColumnWidth, columnMetrics }; - }, [measuredColumnWidths, resizedColumnWidths, columns, lastFrozenColumnIndex]); + }, [getColumnWidth, columns, lastFrozenColumnIndex]); const [colOverscanStartIdx, colOverscanEndIdx] = useMemo((): [number, number] => { if (!enableVirtualization) {