Skip to content

Commit

Permalink
[DataGrid] Apply review from #412 (#515)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari authored Nov 4, 2020
1 parent 05ea7cc commit 069daa7
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 36 deletions.
2 changes: 1 addition & 1 deletion packages/grid/_modules_/grid/GridComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* Data Grid component implementing [[GridComponentProps]].
* @returns JSX.Element
*/
import useForkRef from '@material-ui/core/utils/useForkRef';
import * as React from 'react';
import { useForkRef } from '@material-ui/core/utils';
import { AutoSizer } from './components/AutoSizer';
import { ColumnsHeader } from './components/column-headers';
import { DefaultFooter } from './components/default-footer';
Expand Down
5 changes: 1 addition & 4 deletions packages/grid/_modules_/grid/components/AutoSizer.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { useForkRef, ownerWindow } from '@material-ui/core/utils';
import { useEventCallback } from '../utils/material-ui-utils';
import { useEventCallback, useEnhancedEffect } from '../utils/material-ui-utils';
import createDetectElementResize from '../lib/createDetectElementResize';
// TODO replace with https://caniuse.com/resizeobserver.

Expand Down Expand Up @@ -47,9 +47,6 @@ export interface AutoSizerProps extends Omit<React.HTMLAttributes<HTMLDivElement
onResize?: (size: AutoSizerSize) => void;
}

// TODO v5: replace with @material-ui/core/utils/useEnhancedEffect.
const useEnhancedEffect = typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect;

export const AutoSizer = React.forwardRef<HTMLDivElement, AutoSizerProps>(function AutoSizer(
props,
ref,
Expand Down
4 changes: 2 additions & 2 deletions packages/grid/_modules_/grid/hooks/features/core/gridState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export interface GridState {

export const getInitialState: () => GridState = () => ({
rows: getInitialRowState(),
pagination: { ...INITIAL_PAGINATION_STATE },
options: { ...DEFAULT_GRID_OPTIONS },
pagination: INITIAL_PAGINATION_STATE,
options: DEFAULT_GRID_OPTIONS,
isScrolling: false,
columns: getInitialColumnsState(),
columnReorder: getInitialColumnReorderState(),
Expand Down
20 changes: 8 additions & 12 deletions packages/grid/_modules_/grid/hooks/features/core/useGridReducer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useEffect, useRef } from 'react';
import * as React from 'react';
import { ApiRef } from '../../../models/api/apiRef';
import { useGridApi } from './useGridApi';
Expand All @@ -10,34 +9,31 @@ export const useGridReducer = <State, Action>(
reducer: React.Reducer<State, Action>,
initialState: State,
) => {
const api = useGridApi(apiRef);
const gridApi = useGridApi(apiRef);
const [gridState, setGridState, forceUpdate] = useGridState(apiRef);

const gridDispatch = React.useCallback(
(action: Action) => {
if (gridState[stateId] === undefined) {
gridState[stateId] = initialState;
}
const newLocalState = reducer(api.state[stateId], action);
setGridState((oldState) => {
const updatingState: any = {};
updatingState[stateId] = { ...newLocalState };

oldState = { ...oldState, ...updatingState };
return oldState;
const newState = { ...oldState };
newState[stateId] = reducer(oldState[stateId], action);
return newState;
});
forceUpdate();
},
[api, forceUpdate, gridState, initialState, reducer, setGridState, stateId],
[forceUpdate, gridState, initialState, reducer, setGridState, stateId],
);

const dispatchRef = useRef(gridDispatch);
const dispatchRef = React.useRef(gridDispatch);

useEffect(() => {
React.useEffect(() => {
dispatchRef.current = gridDispatch;
}, [gridDispatch]);

const dispatch = React.useCallback((args) => dispatchRef.current(args), []);

return { gridState, dispatch, gridApi: api };
return { gridState, dispatch, gridApi };
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export const useGridSelector = <State>(
apiRef: ApiRef | undefined,
selector: (state: any) => State,
) => {
const [state, ,] = useGridState(apiRef!);

const [state] = useGridState(apiRef!);
return selector(state);
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ export interface PaginationState {
paginationMode: FeatureMode;
}

const SET_PAGE_ACTION = 'SetPage';
const SET_PAGESIZE_ACTION = 'SetPageSize';
const SET_PAGINATION_MODE_ACTION = 'SetPaginationMode';
const SET_ROWCOUNT_ACTION = 'setRowCount';
const SET_PAGE_ACTION = 'SET_PAGE_ACTION';
const SET_PAGESIZE_ACTION = 'SET_PAGESIZE_ACTION';
const SET_PAGINATION_MODE_ACTION = 'SET_PAGINATION_MODE_ACTION';
const SET_ROWCOUNT_ACTION = 'SET_ROWCOUNT_ACTION';

type SetPageAction = { type: 'SetPage'; payload: { page: number } };
type SetPageSizeAction = { type: 'SetPageSize'; payload: { pageSize: number } };
type SetPageAction = { type: typeof SET_PAGE_ACTION; payload: { page: number } };
type SetPageSizeAction = { type: typeof SET_PAGESIZE_ACTION; payload: { pageSize: number } };
type SetPaginationModeAction = {
type: 'SetPaginationMode';
type: typeof SET_PAGINATION_MODE_ACTION;
payload: { paginationMode: FeatureMode };
};
type SetRowCountAction = {
type: 'setRowCount';
type: typeof SET_ROWCOUNT_ACTION;
payload: { totalRowCount: number };
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { CellIndexCoordinates } from '../../../models/cell';
import { ScrollParams } from '../../../models/params/scrollParams';
import { RenderContextProps, RenderRowProps } from '../../../models/renderContextProps';
import { isEqual } from '../../../utils/utils';
import { useEnhancedEffect } from '../../../utils/material-ui-utils';
import { useGridSelector } from '../core/useGridSelector';
import { useGridState } from '../core/useGridState';
import { PaginationState } from '../pagination/paginationReducer';
Expand All @@ -22,9 +23,6 @@ import { useVirtualColumns } from './useVirtualColumns';

type UseVirtualRowsReturnType = Partial<RenderContextProps> | null;

// TODO v5: replace with @material-ui/core/utils/useEnhancedEffect.
const useEnhancedEffect = typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect;

export const useVirtualRows = (
colRef: React.MutableRefObject<HTMLDivElement | null>,
windowRef: React.MutableRefObject<HTMLDivElement | null>,
Expand Down
2 changes: 1 addition & 1 deletion packages/grid/_modules_/grid/hooks/utils/useLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function getAppender(name: string, logLevel: string, appender: Logger = console)
loggerObj[method] = (...args: any[]) => {
const [message, ...rest] = args;

(appender as any)[method](`${name}: ${message}`, ...rest);
(appender as any)[method](`Material-UI: ${name} - ${message}`, ...rest);
};
} else {
loggerObj[method] = noop;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface VirtualizationApi {
/**
* Trigger the grid viewport to scroll to a row of x y indexes.
* @param params
* @returns boolean that indicates if the viewport has scrolled to reach the indexes
* @returns boolean Return if the index was outside of the viewport and the grid has to scroll to reach the target.
*/
scrollToIndexes: (params: CellIndexCoordinates) => boolean;
/**
Expand Down
5 changes: 5 additions & 0 deletions packages/grid/_modules_/grid/utils/material-ui-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import * as React from 'react';
import { useEventCallback as muiUseEventCallback } from '@material-ui/core/utils';

export function useEventCallback<T extends (...args: any[]) => any>(func: T): T {
// @ts-expect-error TODO remove wrapper once upgraded to v5
return muiUseEventCallback(func);
}

// TODO replace with { useEnhancedEffect } from @material-ui/core/utils.
export const useEnhancedEffect =
typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect;
8 changes: 6 additions & 2 deletions packages/grid/data-grid/src/DataGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ describe('<DataGrid />', () => {
);
clock.tick(100);
// @ts-expect-error need to migrate helpers to TypeScript
}).toWarnDev('useResizeContainer: The parent of the grid has an empty height.');
}).toWarnDev(
'Material-UI: useResizeContainer - The parent of the grid has an empty height.',
);
});

it('should warn if the container has no intrinsic width', () => {
Expand All @@ -119,7 +121,9 @@ describe('<DataGrid />', () => {
);
clock.tick(100);
// @ts-expect-error need to migrate helpers to TypeScript
}).toWarnDev('useResizeContainer: The parent of the grid has an empty width.');
}).toWarnDev(
'Material-UI: useResizeContainer - The parent of the grid has an empty width.',
);
});
});
});
Expand Down

0 comments on commit 069daa7

Please sign in to comment.