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

[DataGrid] Fix flickering on mount #7205

Merged
merged 4 commits into from
Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -13,7 +13,7 @@ import {
} from '@mui/x-data-grid-pro';
import { getBasicGridData } from '@mui/x-data-grid-generator';
// @ts-ignore Remove once the test utils are typed
import { createRenderer, fireEvent, act, userEvent } from '@mui/monorepo/test/utils';
import { createRenderer, fireEvent, act, userEvent, waitFor } from '@mui/monorepo/test/utils';
import { getCell, getRow } from 'test/utils/helperFn';

describe('<DataGridPro /> - Row Editing', () => {
Expand Down Expand Up @@ -1121,7 +1121,7 @@ describe('<DataGridPro /> - Row Editing', () => {
const cell = getCell(0, 2);
userEvent.mousePress(cell);
fireEvent.doubleClick(cell);
await act(() => {
await waitFor(() => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why, but without this change the test was failing with:

The current testing environment is not configured to support act

See https://app.circleci.com/pipelines/github/mui/mui-x/30074/workflows/a9f04000-72a1-436d-819b-360e41eabb40/jobs/172385

Copy link
Member

@m4theushw m4theushw Dec 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went through the call stack and found that the column headers are trying to react to a scroll event. Placing a breakpoint where scrollPositionChange is published I found that the custom edit component is calling .focus(). You can revert this change and do the following to make the test pass:

diff --git a/packages/grid/x-data-grid-pro/src/tests/rowEditing.DataGridPro.test.tsx b/packages/grid/x-data-grid-pro/src/tests/rowEditing.DataGridPro.test.tsx
index 34cf0badc..061277b03 100644
--- a/packages/grid/x-data-grid-pro/src/tests/rowEditing.DataGridPro.test.tsx
+++ b/packages/grid/x-data-grid-pro/src/tests/rowEditing.DataGridPro.test.tsx
@@ -27,7 +27,7 @@ describe('<DataGridPro /> - Row Editing', () => {
     const ref = React.useRef<HTMLInputElement>(null);
     React.useLayoutEffect(() => {
       if (hasFocus) {
-        ref.current!.focus();
+        ref.current!.focus({ preventScroll: true });
       }
     }, [hasFocus]);
     return <input ref={ref} />;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Matheus!

apiRef.current.setEditCellValue({ id: 0, field: 'price1M', value: 'USD GBP' });
});
fireEvent.keyDown(cell.querySelector('input'), { key: 'Tab' });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { unstable_useForkRef as useForkRef } from '@mui/utils';
import {
unstable_useForkRef as useForkRef,
unstable_useEnhancedEffect as useEnhancedEffect,
} from '@mui/utils';
import { useGridPrivateApiContext } from '../../utils/useGridPrivateApiContext';
import { useGridRootProps } from '../../utils/useGridRootProps';
import { useGridSelector } from '../../utils/useGridSelector';
Expand Down Expand Up @@ -213,7 +216,7 @@ export const useGridVirtualScroller = (props: UseGridVirtualScrollerProps) => {
containerDimensions,
]);

React.useEffect(() => {
useEnhancedEffect(() => {
if (disableVirtualization) {
renderZoneRef.current!.style.transform = `translate3d(0px, 0px, 0px)`;
} else {
Expand All @@ -223,7 +226,7 @@ export const useGridVirtualScroller = (props: UseGridVirtualScrollerProps) => {
}
}, [disableVirtualization]);

React.useEffect(() => {
useEnhancedEffect(() => {
setContainerDimensions({
width: rootRef.current!.clientWidth,
height: rootRef.current!.clientHeight,
Expand Down Expand Up @@ -312,7 +315,7 @@ export const useGridVirtualScroller = (props: UseGridVirtualScrollerProps) => {
[apiRef, setRenderContext, prevRenderContext, currentPage.rows.length, rootProps.rowBuffer],
);

React.useEffect(() => {
useEnhancedEffect(() => {
if (containerDimensions.width == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -716,13 +716,13 @@ describe('<DataGrid /> - Keyboard', () => {
<DataGrid rows={rows} columns={columns} />
</div>,
);
expect(renderCell.callCount).to.equal(2);
expect(renderCell.callCount).to.equal(6);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And the number of rerenders changed here again 🤷🏻‍♂️
#7155 (comment)

const input = screen.getByTestId('custom-input');
input.focus();
fireEvent.keyDown(input, { key: 'a' });
expect(renderCell.callCount).to.equal(4);
expect(renderCell.callCount).to.equal(8);
fireEvent.keyDown(input, { key: 'b' });
expect(renderCell.callCount).to.equal(4);
expect(renderCell.callCount).to.equal(8);
});

it('should not scroll horizontally when cell is wider than viewport', () => {
Expand Down