Skip to content

Commit

Permalink
[core] List the requirement of each hooks (#2319)
Browse files Browse the repository at this point in the history
* [core] List the requirement of each hooks
  • Loading branch information
flaviendelangle authored Aug 30, 2021
1 parent e4568b5 commit ce914c8
Show file tree
Hide file tree
Showing 27 changed files with 153 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ function writeToClipboardPolyfill(data: string) {
}
}

/**
* @requires useGridColumns (state)
* @requires useGridParamsApi (method)
* @requires useGridSelection (method)
*/
export const useGridClipboard = (apiRef: GridApiRef): void => {
const visibleColumns = useGridSelector(apiRef, visibleGridColumnsSelector);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { useGridState } from '../core/useGridState';
import { useGridApiEventHandler } from '../../root/useGridApiEventHandler';
import { GridEvents } from '../../../constants/eventsConstants';

/**
* @requires useGridPreferencePanel (method)
* @requires useGridVirtualRows (state)
*/
export const useGridColumnMenu = (apiRef: GridApiRef): void => {
const logger = useLogger('useGridColumnMenu');
const [gridState, setGridState, forceUpdate] = useGridState(apiRef);
Expand Down Expand Up @@ -41,7 +45,7 @@ export const useGridColumnMenu = (apiRef: GridApiRef): void => {
hideColumnMenu();
}
},
[logger, showColumnMenu, hideColumnMenu, gridState],
[logger, showColumnMenu, hideColumnMenu, gridState.columnMenu],
);

useGridApiMethod(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const hasCursorPositionChanged = (

/**
* Only available in DataGridPro
* @requires useGridColumns (method)
*/
export const useGridColumnReorder = (
apiRef: GridApiRef,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ function trackFinger(event, currentTouchId): CursorCoordinates | boolean {
};
}

// TODO improve experience for last column
/**
* TODO: improve experience for last column
* @requires useGridColumns (method, event)
*/
export const useGridColumnResize = (
apiRef: GridApiRef,
props: Pick<GridComponentProps, 'onColumnResize' | 'onColumnWidthChange'>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ const upsertColumnsState = (columnUpdates: GridColDef[], prevColumnsState?: Grid
return newState;
};

/**
* @requires useGridParamsApi (method)
* TODO: Impossible priority - useGridParamsApi also needs to be after useGridColumns
*/
export function useGridColumns(
apiRef: GridApiRef,
props: Pick<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ import { exportAs } from '../../../utils/exportAs';
import { buildCSV } from './serializers/csvSerializer';
import { GridStateColDef } from '../../../models';

/**
* @requires useGridColumns (state)
* @requires useGridFilter (state)
* @requires useGridSorting (state)
* @requires useGridSelection (state)
* @requires useGridParamsApi (method)
*/
export const useGridCsvExport = (apiRef: GridApiRef): void => {
const logger = useLogger('useGridCsvExport');
const visibleColumns = useGridSelector(apiRef, visibleGridColumnsSelector);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ import { GridFilterModel } from '../../../models/gridFilterModel';
import { visibleSortedGridRowsSelector } from './gridFilterSelector';
import { getInitialVisibleGridRowsState } from './visibleGridRowsState';

/**
* @requires useGridColumns (state, method, event)
* @requires useGridParamsApi (method)
* @requires useGridRows (event)
* @requires useGridControlState (method)
*/
export const useGridFilter = (
apiRef: GridApiRef,
props: Pick<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import { useGridApiEventHandler } from '../../root/useGridApiEventHandler';
import { GridComponentProps } from '../../../GridComponentProps';
import { isNavigationKey } from '../../../utils/keyboardUtils';

/**
* @requires useGridParamsApi (method)
* @requires useGridRows (method)
* @requires useGridEditRows (event)
*/
export const useGridFocus = (apiRef: GridApiRef, props: Pick<GridComponentProps, 'rows'>): void => {
const logger = useLogger('useGridFocus');
const [, setGridState, forceUpdate] = useGridState(apiRef);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import { renderStateSelector } from '../virtualization/renderingStateSelector';
import { GridViewportRowsChangeParams } from '../../../models/params/gridViewportRowsChangeParams';
import { GridScrollParams } from '../../../models/params/gridScrollParams';

/**
* @requires useGridColumns (state)
* @requires useGridContainerProps (state)
* @requires useGridVirtualRows (method, event)
*/
export const useGridInfiniteLoader = (
apiRef: GridApiRef,
props: Pick<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ import { useLogger } from '../../utils/useLogger';
import { useGridApiEventHandler } from '../../root/useGridApiEventHandler';
import { GridCellModes } from '../../../models/gridEditRowModel';

/**
* @requires useGridSelection (method)
* @requires useGridRows (method)
* @requires useGridFocus (state)
* @requires useGridParamsApi (method)
* @requires useGridColumnMenu (method)
*/
export const useGridKeyboard = (apiRef: GridApiRef): void => {
const logger = useLogger('useGridKeyboard');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ const getNextColumnHeaderIndexes = (key: string, indexes: GridColumnHeaderIndexC
}
};

/**
* @requires useGridPage (state)
* @requires useGridPageSize (state)
* @requires useGridColumns (state, method)
* @requires useGridRows (state, method)
* @requires useGridContainerProps (state)
* @requires useGridFocus (method)
* @requires useGridVirtualRows (method)
*/
export const useGridKeyboardNavigation = (
apiRef: GridApiRef,
props: Pick<GridComponentProps, 'pagination'>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { GridLocaleTextApi } from '../../../models/api/gridLocaleTextApi';
import { useGridApiMethod } from '../../root/useGridApiMethod';
import { useGridSelector } from '../core/useGridSelector';

/**
* @requires useOptionsProp
*/
export const useLocaleText = (apiRef: GridApiRef): void => {
const { localeText } = useGridSelector(apiRef, optionsSelector);
const getLocaleText = React.useCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ const applyValidPage = (paginationState: GridPaginationState): GridPaginationSta
};
};

/**
* @requires useGridControlState (method)
* @requires useGridPageSize (state, event)
* @requires useGridFilter (state)
*/
export const useGridPage = (
apiRef: GridApiRef,
props: Pick<GridComponentProps, 'page' | 'onPageChange' | 'rowCount'>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import { useGridSelector, useGridState } from '../core';
import { visibleGridRowCountSelector } from '../filter';
import { gridContainerSizesSelector } from '../../root/gridContainerSizesSelector';

/**
* @requires useGridControlState (method)
* @requires useGridContainerProps (state)
* @requires useGridFilter (state)
*/
export const useGridPageSize = (
apiRef: GridApiRef,
props: Pick<GridComponentProps, 'pageSize' | 'onPageSizeChange' | 'autoPageSize'>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ import { useLogger } from '../../utils/useLogger';
import { useGridState } from '../core/useGridState';
import { useGridSelector } from '../core/useGridSelector';

/**
* @requires useGridFocus - can be after, async only
* @requires useGridParamsApi (method)
* @requires useGridColumns (state)
* @requires useGridControlState (method)
*/
export function useGridEditRows(
apiRef: GridApiRef,
props: Pick<
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { GridRowsProp } from '../../../models/gridRows';
import { GridComponentProps } from '../../../GridComponentProps';
import { GridApiRef } from '../../../models/api/gridApiRef';

export function useGridFreezeRows(apiRef: any, { rows }: { rows: GridRowsProp }) {
export function useGridFreezeRows(apiRef: GridApiRef, props: Pick<GridComponentProps, 'rows'>) {
if (process.env.NODE_ENV !== 'production') {
// Freeze rows for immutability
Object.freeze(rows);
Object.freeze(props.rows);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ function warnMissingColumn(field) {
warnedOnce = true;
}

/**
* @requires useGridColumns (method)
* @requires useGridRows (method)
* @requires useGridFocus (state)
* @requires useGridEditRows (method)
* TODO: Impossible priority - useGridEditRows also needs to be after useGridParamsApi
* TODO: Impossible priority - useGridFocus also needs to be after useGridParamsApi
*/
export function useGridParamsApi(apiRef: GridApiRef) {
const getColumnHeaderParams = React.useCallback(
(field: string): GridColumnHeaderParams => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ export function convertGridRowsPropToState(
return state;
}

/**
* @requires useGridSorting (method)
* TODO: Impossible priority - useGridSorting also needs to be after useGridRows (which causes all the existence check for apiRef.current.apiRef.current.getSortedRowIds)
*/
export const useGridRows = (
apiRef: GridApiRef,
props: Pick<GridComponentProps, 'rows' | 'getRowId' | 'rowCount'>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ import {
selectedIdsLookupSelector,
} from './gridSelectionSelector';

/**
* @requires useOptionsProp (state)
* @requires useGridRows (state, method)
* @requires useGridParamsApi (method)
* @requires useGridControlState (method)
*/
export const useGridSelection = (apiRef: GridApiRef, props: GridComponentProps): void => {
const logger = useLogger('useGridSelection');
const [, setGridState, forceUpdate] = useGridState(apiRef);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ import { useGridState } from '../core/useGridState';
import { gridRowCountSelector } from '../rows/gridRowsSelector';
import { sortedGridRowIdsSelector, sortedGridRowsSelector } from './gridSortingSelector';

/**
* @requires useGridRows (state, event)
* @requires useGridControlState (method)
* @requires useGridColumns (event)
*/
export const useGridSorting = (
apiRef: GridApiRef,
props: Pick<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ function getIdxFromScroll(
: getIdxFromScroll(offset, positions, pivot + 1, sliceEnd);
}

/**
* @requires useGridContainerProps (state)
* @requires useGridColumns (state)
* @requires useGridPage (state)
* @requires useGridPageSize (state)
* @requires useGridRows (state)
* @requires useGridDensity (state)
*/
export const useGridVirtualRows = (
apiRef: GridApiRef,
props: Pick<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ function getScrollbarSize(doc: Document, element: HTMLElement): number {
return scrollbarSize;
}

/**
* @requires useOptionsProp (state)
* @requires useGridDensity (state)
* @requires useGridColumns (state)
* @requires useGridFilter (state)
* @requires useGridPage (state)
* @requires useGridPageSize (state)
* TODO: Impossible priority - useGridPageSize also needs to be after useGridContainerProps
*/
export const useGridContainerProps = (
apiRef: GridApiRef,
props: Pick<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { GridEvents } from '../../constants/eventsConstants';
import { useGridApiOptionHandler } from './useGridApiEventHandler';
import { GridComponentProps } from '../../GridComponentProps';

export function useEvents(
/**
* @requires useGridFocus (event)
* @requires useGridColumns (event)
*/
export function useGridEvents(
apiRef: GridApiRef,
props: Pick<
GridComponentProps,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import { GridComponentProps } from '../../GridComponentProps';

const isTestEnvironment = process.env.NODE_ENV === 'test';

export function useResizeContainer(
/**
* @requires useGridContainerProps (state) - can be after, async only
*/
export function useGridResizeContainer(
apiRef,
props: Pick<GridComponentProps, 'autoHeight' | 'rows' | 'onResize'>,
) {
Expand Down
3 changes: 3 additions & 0 deletions packages/grid/_modules_/grid/hooks/utils/useRenderInfoLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { GridApiRef } from '../../models/api/gridApiRef';
import { useGridState } from '../features/core/useGridState';
import { useLogger } from './useLogger';

/**
* @requires useGridVirtualRows (state)
*/
export function useRenderInfoLog(apiRef: GridApiRef) {
const [gridState] = useGridState(apiRef);
const logger = useLogger('useRenderInfoLog');
Expand Down
20 changes: 10 additions & 10 deletions packages/grid/data-grid/src/useDataGridComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ import { useGridSorting } from '../../_modules_/grid/hooks/features/sorting/useG
import { useGridComponents } from '../../_modules_/grid/hooks/features/useGridComponents';
import { useGridVirtualRows } from '../../_modules_/grid/hooks/features/virtualization/useGridVirtualRows';
import { useApi } from '../../_modules_/grid/hooks/root/useApi';
import { useEvents } from '../../_modules_/grid/hooks/root/useEvents';
import { useGridEvents } from '../../_modules_/grid/hooks/root/useGridEvents';
import { useGridContainerProps } from '../../_modules_/grid/hooks/root/useGridContainerProps';
import { useErrorHandler } from '../../_modules_/grid/hooks/utils/useErrorHandler';
import { useLoggerFactory } from '../../_modules_/grid/hooks/utils/useLogger';
import { useOptionsProp } from '../../_modules_/grid/hooks/utils/useOptionsProp';
import { useRenderInfoLog } from '../../_modules_/grid/hooks/utils/useRenderInfoLog';
import { useResizeContainer } from '../../_modules_/grid/hooks/utils/useResizeContainer';
import { useGridResizeContainer } from '../../_modules_/grid/hooks/utils/useGridResizeContainer';
import { useStateProp } from '../../_modules_/grid/hooks/utils/useStateProp';
import { GridApiRef } from '../../_modules_/grid/models/api/gridApiRef';

Expand All @@ -40,32 +40,32 @@ export const useDataGridComponent = (apiRef: GridApiRef, props: GridComponentPro
useErrorHandler(apiRef, props);
useGridControlState(apiRef, props);
useOptionsProp(apiRef, props);
useEvents(apiRef, props);
useLocaleText(apiRef);
useResizeContainer(apiRef, props);
useGridResizeContainer(apiRef, props);
useGridFreezeRows(apiRef, props);
useGridColumns(apiRef, props);
useGridParamsApi(apiRef);
useGridRows(apiRef, props);
useGridParamsApi(apiRef);
useGridEditRows(apiRef, props);
useGridFocus(apiRef, props);
useGridKeyboard(apiRef);
useGridKeyboardNavigation(apiRef, props);
useGridSelection(apiRef, props);
useGridSorting(apiRef, props);
useGridColumnMenu(apiRef);
useGridPreferencesPanel(apiRef);
useGridFilter(apiRef, props);
useGridContainerProps(apiRef, props);
useGridDensity(apiRef, props);
useGridVirtualRows(apiRef, props);
useGridContainerProps(apiRef, props);
useGridColumnResize(apiRef, props);
useGridPageSize(apiRef, props);
useGridPage(apiRef, props);
useGridVirtualRows(apiRef, props);
useGridColumnMenu(apiRef);
useGridKeyboard(apiRef);
useGridKeyboardNavigation(apiRef, props);
useGridCsvExport(apiRef);
useGridInfiniteLoader(apiRef, props);
useGridClipboard(apiRef);
useGridComponents(apiRef, props);
useGridEvents(apiRef, props);
useStateProp(apiRef, props);
useRenderInfoLog(apiRef);
};
Loading

0 comments on commit ce914c8

Please sign in to comment.