Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DataGridPro] Fix onColumnWidthChange being called twice on autosize #12140

Merged
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ export const useGridColumnResize = (
const logger = useGridLogger(apiRef, 'useGridColumnResize');

const colDefRef = React.useRef<GridStateColDef>();
const previousMouseClickEvent = React.useRef<MouseEvent>();
const columnHeaderElementRef = React.useRef<HTMLDivElement>();
const headerFilterElementRef = React.useRef<HTMLDivElement>();
const groupHeaderElementsRef = React.useRef<Element[]>([]);
Expand Down Expand Up @@ -378,6 +379,29 @@ export const useGridColumnResize = (
// eslint-disable-next-line @typescript-eslint/no-use-before-define
stopListening();

// This block of code checks if the current mouse event is a double-click by comparing it with the previous mouse click event.
// It prevents unintentional double-clicks from being interpreted as two separate clicks.
if (previousMouseClickEvent.current) {
// Retrieve the details of the previous mouse click event.
const prevEvent = previousMouseClickEvent.current;
const prevTimeStamp = prevEvent.timeStamp;
const prevClientX = prevEvent.clientX;
const prevClientY = prevEvent.clientY;

// Check if the time elapsed between the current and previous mouse clicks is less than 300 milliseconds,
// and if the current mouse click occurred at the same position as the previous one.
if (
nativeEvent.timeStamp - prevTimeStamp < 300 &&
nativeEvent.clientX === prevClientX &&
nativeEvent.clientY === prevClientY
) {
// If the conditions are met, it indicates that the current event is part of a double-click,
// so we clear the previous mouse click event and return early to ignore it.
previousMouseClickEvent.current = undefined;
return;
}
}

shaharyar-shamshi marked this conversation as resolved.
Show resolved Hide resolved
if (colDefRef.current) {
apiRef.current.setColumnWidth(colDefRef.current.field, colDefRef.current.width!);
logger.debug(
Expand Down Expand Up @@ -610,6 +634,8 @@ export const useGridColumnResize = (
const doc = ownerDocument(apiRef.current.rootElementRef!.current);
doc.body.style.cursor = 'col-resize';

previousMouseClickEvent.current = event.nativeEvent;

doc.addEventListener('mousemove', handleResizeMouseMove);
doc.addEventListener('mouseup', handleResizeMouseUp);

Expand Down Expand Up @@ -702,6 +728,17 @@ export const useGridColumnResize = (
}

apiRef.current.updateColumns(newColumns);

if (colDefRef.current) {
const columnsState = gridColumnsStateSelector(apiRef.current.state);
const column: GridStateColDef = columnsState.lookup[colDefRef.current.field];
const width: number = column.width as number;
apiRef.current.publishEvent('columnWidthChange', {
element: apiRef.current.getColumnHeaderElement(colDefRef.current.field),
colDef: { ...colDefRef.current, width },
width,
});
}
shaharyar-shamshi marked this conversation as resolved.
Show resolved Hide resolved
} finally {
apiRef.current.unstable_setColumnVirtualization(true);
isAutosizingRef.current = false;
Expand Down
18 changes: 18 additions & 0 deletions packages/x-data-grid-pro/src/tests/columns.DataGridPro.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,24 @@ describe('<DataGridPro /> - Columns', () => {
expect(onColumnWidthChange.args[0][0].width).to.equal(120);
});

it('should call onColumnWidthChange with correct width after resizing and then clicking the seprator', async () => {
shaharyar-shamshi marked this conversation as resolved.
Show resolved Hide resolved
const onColumnWidthChange = spy();
render(<Test onColumnWidthChange={onColumnWidthChange} columns={columns} />);
const separator = document.querySelector(`.${gridClasses['columnSeparator--resizable']}`)!;
fireEvent.mouseDown(separator, { clientX: 100 });
fireEvent.mouseMove(separator, { clientX: 110, buttons: 1 });
fireEvent.mouseMove(separator, { clientX: 120, buttons: 1 });
expect(onColumnWidthChange.callCount).to.equal(0);
fireEvent.mouseUp(separator);
clock.tick(0);
expect(onColumnWidthChange.callCount).to.equal(1);
expect(onColumnWidthChange.args[0][0].width).to.equal(120);
fireEvent.doubleClick(separator);
await microtasks();
expect(onColumnWidthChange.callCount).to.equal(2);
expect(onColumnWidthChange.args[1][0].width).to.equal(116);
});

it('should not affect other cell elements that are not part of the main DataGrid instance', () => {
render(
<Test
Expand Down
Loading