Skip to content

Commit

Permalink
[core] Add React 18 to peer dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
m4theushw committed Apr 2, 2022
1 parent b4c16bd commit 850295a
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 41 deletions.
2 changes: 1 addition & 1 deletion docs/data/data-grid/events/events.json
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@
"name": "rowsScroll",
"description": "Fired during the scroll of the grid viewport.",
"params": "GridScrollParams",
"event": "MuiEvent<{}>"
"event": "MuiEvent<React.UIEvent | MuiBaseEvent>"
},
{
"name": "rowsScrollEnd",
Expand Down
2 changes: 1 addition & 1 deletion docs/src/pages/components/data-grid/events/events.json
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@
"name": "rowsScroll",
"description": "Fired during the scroll of the grid viewport.",
"params": "GridScrollParams",
"event": "MuiEvent<{}>"
"event": "MuiEvent<React.UIEvent | MuiBaseEvent>"
},
{
"name": "rowsScrollEnd",
Expand Down
2 changes: 1 addition & 1 deletion packages/grid/x-data-grid-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"peerDependencies": {
"@mui/icons-material": "^5.2.5",
"@mui/material": "^5.2.8",
"react": "^17.0.2"
"react": "^17.0.2 || ^18.0.0"
},
"setupFiles": [
"<rootDir>/src/setupTests.js"
Expand Down
2 changes: 1 addition & 1 deletion packages/grid/x-data-grid-pro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"peerDependencies": {
"@mui/material": "^5.2.8",
"@mui/system": "^5.2.8",
"react": "^17.0.2"
"react": "^17.0.2 || ^18.0.0"
},
"setupFiles": [
"<rootDir>/src/setupTests.js"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,27 +122,15 @@ export const DataGridProColumnHeaders = React.forwardRef<
const pinnedColumns = useGridSelector(apiRef, gridPinnedColumnsSelector);
const [leftPinnedColumns, rightPinnedColumns] = filterColumns(pinnedColumns, visibleColumnFields);

const {
isDragging,
renderContext,
updateInnerPosition,
getRootProps,
getInnerProps,
getColumns,
} = useGridColumnHeaders({
innerRef,
minColumnIndex: leftPinnedColumns.length,
});
const { isDragging, renderContext, getRootProps, getInnerProps, getColumns } =
useGridColumnHeaders({
innerRef,
minColumnIndex: leftPinnedColumns.length,
});

const ownerState = { leftPinnedColumns, rightPinnedColumns, classes: rootProps.classes };
const classes = useUtilityClasses(ownerState);

React.useEffect(() => {
if (renderContext) {
updateInnerPosition(renderContext);
}
}, [renderContext, updateInnerPosition]);

const leftRenderContext =
renderContext && leftPinnedColumns.length
? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,6 @@ const DataGridProVirtualScroller = React.forwardRef<
useGridApiEventHandler(apiRef, GridEvents.columnWidthChange, refreshRenderZonePosition);
useGridApiEventHandler(apiRef, GridEvents.columnOrderChange, refreshRenderZonePosition);

React.useEffect(() => {
refreshRenderZonePosition();
}, [refreshRenderZonePosition]);

const leftRenderContext =
renderContext && leftPinnedColumns.length > 0
? {
Expand Down
2 changes: 1 addition & 1 deletion packages/grid/x-data-grid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"peerDependencies": {
"@mui/material": "^5.2.8",
"@mui/system": "^5.2.8",
"react": "^17.0.2"
"react": "^17.0.2 || ^18.0.0"
},
"setupFiles": [
"<rootDir>/src/setupTests.js"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { useForkRef } from '@mui/material/utils';
import { useGridApiContext } from '../../utils/useGridApiContext';
import { useGridSelector } from '../../utils/useGridSelector';
Expand Down Expand Up @@ -26,6 +27,10 @@ interface UseGridColumnHeadersProps {
minColumnIndex?: number;
}

function isUIEvent(event: any): event is React.UIEvent {
return !!event.target;
}

export const useGridColumnHeaders = (props: UseGridColumnHeadersProps) => {
const { innerRef: innerRefProp, minColumnIndex = 0 } = props;

Expand Down Expand Up @@ -70,8 +75,14 @@ export const useGridColumnHeaders = (props: UseGridColumnHeadersProps) => {
[columnPositions, minColumnIndex, rootProps.columnBuffer],
);

React.useLayoutEffect(() => {
if (renderContext) {
updateInnerPosition(renderContext);
}
}, [renderContext, updateInnerPosition]);

const handleScroll = React.useCallback<GridEventListener<GridEvents.rowsScroll>>(
({ left, renderContext: nextRenderContext = null }) => {
({ left, renderContext: nextRenderContext = null }, event) => {
if (!innerRef.current) {
return;
}
Expand All @@ -87,13 +98,30 @@ export const useGridColumnHeaders = (props: UseGridColumnHeadersProps) => {
}
prevScrollLeft.current = left;

// We can only update the position when we guarantee that the render context has been
// rendered. This is achieved using ReactDOM.flushSync or when the context doesn't change.
let canUpdateInnerPosition = false;

if (nextRenderContext !== prevRenderContext.current || !prevRenderContext.current) {
setRenderContext(nextRenderContext);
// ReactDOM.flushSync cannot be called on `scroll` events fired inside effects
if (isUIEvent(event)) {
// To prevent flickering, the inner position can only be updated after the new context has
// been rendered. ReactDOM.flushSync ensures that the state changes will happen before
// updating the position.
ReactDOM.flushSync(() => {
setRenderContext(nextRenderContext);
});
canUpdateInnerPosition = true;
} else {
setRenderContext(nextRenderContext);
}
prevRenderContext.current = nextRenderContext;
} else {
canUpdateInnerPosition = true;
}

// Pass directly the render context to avoid waiting for the next render
if (nextRenderContext) {
if (nextRenderContext && canUpdateInnerPosition) {
updateInnerPosition(nextRenderContext);
}
},
Expand Down Expand Up @@ -204,7 +232,6 @@ export const useGridColumnHeaders = (props: UseGridColumnHeadersProps) => {
renderContext,
getColumns,
isDragging: !!dragCol,
updateInnerPosition,
getRootProps: (other = {}) => ({ style: rootStyle, ...other }),
getInnerProps: () => ({ ref: handleInnerRef, 'aria-rowindex': 1, role: 'row' }),
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { useForkRef } from '@mui/material/utils';
import { useGridApiContext } from '../../utils/useGridApiContext';
import { useGridRootProps } from '../../utils/useGridRootProps';
Expand Down Expand Up @@ -197,13 +198,18 @@ export const useGridVirtualScroller = (props: UseGridVirtualScrollerProps) => {
],
);

React.useLayoutEffect(() => {
if (renderContext) {
updateRenderZonePosition(renderContext);
}
}, [renderContext, updateRenderZonePosition]);

const updateRenderContext = React.useCallback(
(nextRenderContext) => {
setRenderContext(nextRenderContext);
updateRenderZonePosition(nextRenderContext);
prevRenderContext.current = nextRenderContext;
},
[setRenderContext, prevRenderContext, updateRenderZonePosition],
[setRenderContext, prevRenderContext],
);

React.useEffect(() => {
Expand All @@ -212,7 +218,6 @@ export const useGridVirtualScroller = (props: UseGridVirtualScrollerProps) => {
}

const initialRenderContext = computeRenderContext();
prevRenderContext.current = initialRenderContext;
updateRenderContext(initialRenderContext);

const { top, left } = scrollPosition.current!;
Expand Down Expand Up @@ -257,14 +262,21 @@ export const useGridVirtualScroller = (props: UseGridVirtualScrollerProps) => {
prevTotalWidth.current !== columnsTotalWidth;

// TODO v6: rename event to a wider name, it's not only fired for row scrolling
apiRef.current.publishEvent(GridEvents.rowsScroll, {
top: scrollTop,
left: scrollLeft,
renderContext: shouldSetState ? nextRenderContext : prevRenderContext.current,
});
apiRef.current.publishEvent(
GridEvents.rowsScroll,
{
top: scrollTop,
left: scrollLeft,
renderContext: shouldSetState ? nextRenderContext : prevRenderContext.current,
},
event,
);

if (shouldSetState) {
updateRenderContext(nextRenderContext);
// Prevents batching render context changes
ReactDOM.flushSync(() => {
updateRenderContext(nextRenderContext);
});
prevTotalWidth.current = columnsTotalWidth;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export interface GridEventLookup
};

// Scroll
rowsScroll: { params: GridScrollParams };
rowsScroll: { params: GridScrollParams; event: React.UIEvent | MuiBaseEvent };
virtualScrollerContentSizeChange: {};

// Selection
Expand Down
2 changes: 1 addition & 1 deletion packages/x-license-pro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"yargs": "^17.4.0"
},
"peerDependencies": {
"react": "^17.0.2"
"react": "^17.0.2 || ^18.0.0"
},
"setupFiles": [
"<rootDir>/src/setupTests.js"
Expand Down

0 comments on commit 850295a

Please sign in to comment.