Skip to content

Commit

Permalink
Prevent weird state with headers on drag when dragging cells enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
jassmith committed Aug 1, 2022
1 parent 3ad9e27 commit c3d2bc1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
4 changes: 4 additions & 0 deletions packages/core/src/data-editor/data-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1648,6 +1648,10 @@ const DataEditorImpl: React.ForwardRefRenderFunction<DataEditorRef, DataEditorPr
const isActivelyDragging = React.useRef(false);
const onDragStartImpl = React.useCallback(
(args: GridDragEventArgs) => {
if (args.location[0] === 0 && rowMarkerOffset > 0) {
args.preventDefault();
return;
}
onDragStart?.({
...args,
location: [args.location[0] - rowMarkerOffset, args.location[1]] as any,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3214,7 +3214,10 @@ export const DragSource: React.VFC<{ isDraggable: boolean | "header" | "cell" }>
{...defaultProps}
getCellContent={getCellContent}
columns={cols}
rowMarkers="both"
rows={5000}
onRowMoved={(s, e) => window.alert(`Moved row ${s} to ${e}`)}
onColumnMoved={(s, e) => window.alert(`Moved col ${s} to ${e}`)}
onColumnResize={onColumnResize}
isDraggable={p.isDraggable}
onDragStart={e => {
Expand Down
42 changes: 28 additions & 14 deletions packages/core/src/data-grid-dnd/data-grid-dnd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ const DataGridDnd: React.FunctionComponent<DataGridDndProps> = p => {
(args: GridMouseEventArgs) => {
if (args.button === 0) {
const [col, row] = args.location;
// if (!isDraggable) {
if (args.kind === "out-of-bounds" && args.isEdge && canResize) {
const bounds = gridRef?.current?.getBounds(columns.length - 1, -1);
if (bounds !== undefined) {
Expand All @@ -107,7 +106,6 @@ const DataGridDnd: React.FunctionComponent<DataGridDndProps> = p => {
setDragStartY(args.bounds.y);
setDragRow(row);
}
// }
}
onMouseDown?.(args);
},
Expand All @@ -124,6 +122,20 @@ const DataGridDnd: React.FunctionComponent<DataGridDndProps> = p => {

const lastResizeWidthRef = React.useRef(-1);

const clearAll = React.useCallback(() => {
lastResizeWidthRef.current = -1;
setDragRow(undefined);
setDropRow(undefined);
setDragStartY(undefined);
setDragRowActive(false);
setDragCol(undefined);
setDropCol(undefined);
setDragStartX(undefined);
setDragColActive(false);
setResizeCol(undefined);
setResizeColStartX(undefined);
}, []);

const onMouseUpImpl = React.useCallback(
(args: GridMouseEventArgs, isOutside: boolean) => {
if (args.button === 0) {
Expand Down Expand Up @@ -163,17 +175,7 @@ const DataGridDnd: React.FunctionComponent<DataGridDndProps> = p => {
}
}

lastResizeWidthRef.current = -1;
setDragRow(undefined);
setDropRow(undefined);
setDragStartY(undefined);
setDragRowActive(false);
setDragCol(undefined);
setDropCol(undefined);
setDragStartX(undefined);
setDragColActive(false);
setResizeCol(undefined);
setResizeColStartX(undefined);
clearAll();
if (dragCol !== undefined && dropCol !== undefined) {
onColumnMoved?.(dragCol, dropCol);
}
Expand All @@ -198,6 +200,7 @@ const DataGridDnd: React.FunctionComponent<DataGridDndProps> = p => {
onColumnResize,
onColumnMoved,
onRowMoved,
clearAll,
]
);

Expand Down Expand Up @@ -275,6 +278,17 @@ const DataGridDnd: React.FunctionComponent<DataGridDndProps> = p => {
[dragRow, dropRow, getCellContent]
);

const dragStart = p.onDragStart;
const onDragStart = React.useCallback<NonNullable<DataGridDndProps["onDragStart"]>>(
args => {
dragStart?.(args);
if (!args.defaultPrevented()) {
clearAll();
}
},
[clearAll, dragStart]
);

return (
<DataGrid
// I know the below could be done with ...rest, but it adds about 2-3% cpu load in the hot loop
Expand Down Expand Up @@ -316,7 +330,7 @@ const DataGridDnd: React.FunctionComponent<DataGridDndProps> = p => {
isDraggable={p.isDraggable}
onDragEnd={p.onDragEnd}
onCellFocused={p.onCellFocused}
onDragStart={p.onDragStart}
onDragStart={onDragStart}
onDragOverCell={p.onDragOverCell}
onDragLeave={p.onDragLeave}
onDrop={p.onDrop}
Expand Down

0 comments on commit c3d2bc1

Please sign in to comment.