Skip to content

Commit 0e6b079

Browse files
jyash97arminmeh
andauthored
[DataGridPro] Prevent text selection when reordering rows (#16568)
Co-authored-by: Armin Mehinovic <[email protected]>
1 parent 1f30912 commit 0e6b079

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

packages/x-data-grid-pro/src/components/GridRowReorderCell.tsx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
useGridApiContext,
1010
useGridSelector,
1111
getDataGridUtilityClass,
12+
gridClasses,
1213
} from '@mui/x-data-grid';
1314
import { gridEditRowsStateSelector, isEventTargetInPortal } from '@mui/x-data-grid/internals';
1415
import type { DataGridProProcessedProps } from '../models/dataGridProProps';
@@ -77,11 +78,34 @@ function GridRowReorderCell(params: GridRenderCellParams) {
7778
[apiRef, params.id],
7879
);
7980

81+
const handleMouseDown = React.useCallback(() => {
82+
// Prevent text selection as it will block all the drag events. More context: https://github.com/mui/mui-x/issues/16303
83+
apiRef.current.rootElementRef?.current?.classList.add(
84+
gridClasses['root--disableUserSelection'],
85+
);
86+
}, [apiRef]);
87+
88+
const handleMouseUp = React.useCallback(() => {
89+
apiRef.current.rootElementRef?.current?.classList.remove(
90+
gridClasses['root--disableUserSelection'],
91+
);
92+
}, [apiRef]);
93+
94+
const handleDragEnd = React.useCallback(
95+
(event: React.MouseEvent<HTMLDivElement>) => {
96+
handleMouseUp();
97+
publish('rowDragEnd')(event);
98+
},
99+
[publish, handleMouseUp],
100+
);
101+
80102
const draggableEventHandlers = isDraggable
81103
? {
82104
onDragStart: publish('rowDragStart'),
83105
onDragOver: publish('rowDragOver'),
84-
onDragEnd: publish('rowDragEnd'),
106+
onDragEnd: handleDragEnd,
107+
onMouseDown: handleMouseDown,
108+
onMouseUp: handleMouseUp,
85109
}
86110
: null;
87111

packages/x-data-grid/src/components/containers/GridRootStyles.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ export const GridRootStyles = styled('div', {
593593
borderTop: 'none',
594594
},
595595
},
596-
[`&.${c['root--disableUserSelection']} .${c.cell}`]: {
596+
[`&.${c['root--disableUserSelection']}`]: {
597597
userSelect: 'none',
598598
},
599599
[`& .${c['row--dynamicHeight']} > .${c.cell}`]: {

0 commit comments

Comments
 (0)