Skip to content

Commit

Permalink
Merge branch 'main' into am-fix-draggable
Browse files Browse the repository at this point in the history
  • Loading branch information
amanmahajan7 authored Oct 2, 2023
2 parents cff919a + cf88802 commit d32a1ff
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
18 changes: 15 additions & 3 deletions src/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,15 @@ function DataGrid<R, SR, K extends Key>(
const [draggedOverRowIdx, setOverRowIdx] = useState<number | undefined>(undefined);
const [scrollToPosition, setScrollToPosition] = useState<PartialPosition | null>(null);

const getColumnWidth = useCallback(
(column: CalculatedColumn<R, SR>) => {
return (
resizedColumnWidths.get(column.key) ?? measuredColumnWidths.get(column.key) ?? column.width
);
},
[measuredColumnWidths, resizedColumnWidths]
);

const [gridRef, gridWidth, gridHeight] = useGridDimensions();
const {
columns,
Expand All @@ -297,8 +306,7 @@ function DataGrid<R, SR, K extends Key>(
} = useCalculatedColumns({
rawColumns,
defaultColumnOptions,
measuredColumnWidths,
resizedColumnWidths,
getColumnWidth,
scrollLeft,
viewportWidth: gridWidth,
enableVirtualization
Expand Down Expand Up @@ -830,11 +838,15 @@ function DataGrid<R, SR, K extends Key>(
return;
}

const column = columns[selectedPosition.idx];
const columnWidth = getColumnWidth(column);

return (
<DragHandle
gridRowStart={headerAndTopSummaryRowsCount + selectedPosition.rowIdx + 1}
rows={rows}
columns={columns}
column={column}
columnWidth={columnWidth}
selectedPosition={selectedPosition}
isCellEditable={isCellEditable}
latestDraggedOverRowIdx={latestDraggedOverRowIdx}
Expand Down
12 changes: 6 additions & 6 deletions src/DragHandle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ const cellDragHandleClassname = `rdg-cell-drag-handle ${cellDragHandle}`;

interface Props<R, SR> extends Pick<DataGridProps<R, SR>, 'rows' | 'onRowsChange'> {
gridRowStart: number;
columns: readonly CalculatedColumn<R, SR>[];
column: CalculatedColumn<R, SR>;
columnWidth: number | string;
selectedPosition: SelectCellState;
latestDraggedOverRowIdx: React.MutableRefObject<number | undefined>;
isCellEditable: (position: Position) => boolean;
Expand All @@ -47,7 +48,8 @@ interface Props<R, SR> extends Pick<DataGridProps<R, SR>, 'rows' | 'onRowsChange
export default function DragHandle<R, SR>({
gridRowStart,
rows,
columns,
column,
columnWidth,
selectedPosition,
latestDraggedOverRowIdx,
isCellEditable,
Expand All @@ -58,7 +60,6 @@ export default function DragHandle<R, SR>({
setDraggedOverRowIdx
}: Props<R, SR>) {
const { idx, rowIdx } = selectedPosition;
const column = columns[idx];

function handleMouseDown(event: React.MouseEvent<HTMLDivElement>) {
// keep the focus on the cell
Expand Down Expand Up @@ -99,7 +100,6 @@ export default function DragHandle<R, SR>({
}

function updateRows(startRowIdx: number, endRowIdx: number) {
const column = columns[idx];
const sourceRow = rows[rowIdx];
const updatedRows = [...rows];
const indexes: number[] = [];
Expand Down Expand Up @@ -127,8 +127,8 @@ export default function DragHandle<R, SR>({
...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)}
Expand Down
11 changes: 4 additions & 7 deletions src/hooks/useCalculatedColumns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,14 @@ interface CalculatedColumnsArgs<R, SR> {
defaultColumnOptions: DataGridProps<R, SR>['defaultColumnOptions'];
viewportWidth: number;
scrollLeft: number;
measuredColumnWidths: ReadonlyMap<string, number>;
resizedColumnWidths: ReadonlyMap<string, number>;
getColumnWidth: (column: CalculatedColumn<R, SR>) => string | number;
enableVirtualization: boolean;
}

export function useCalculatedColumns<R, SR>({
rawColumns,
defaultColumnOptions,
measuredColumnWidths,
resizedColumnWidths,
getColumnWidth,
viewportWidth,
scrollLeft,
enableVirtualization
Expand Down Expand Up @@ -177,8 +175,7 @@ export function useCalculatedColumns<R, SR>({
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);
Expand All @@ -205,7 +202,7 @@ export function useCalculatedColumns<R, SR>({
}

return { templateColumns, layoutCssVars, totalFrozenColumnWidth, columnMetrics };
}, [measuredColumnWidths, resizedColumnWidths, columns, lastFrozenColumnIndex]);
}, [getColumnWidth, columns, lastFrozenColumnIndex]);

const [colOverscanStartIdx, colOverscanEndIdx] = useMemo((): [number, number] => {
if (!enableVirtualization) {
Expand Down

0 comments on commit d32a1ff

Please sign in to comment.