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

Use api ref #86

Merged
merged 4 commits into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions packages/grid/x-grid-modules/src/components/api-context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from 'react';
import { GridApiRef } from '../models';
import { ApiRef } from '../models';

export const ApiContext = React.createContext<GridApiRef | undefined>(undefined);
export const ApiContext = React.createContext<ApiRef | undefined>(undefined);
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export const HeaderCheckbox: React.FC<ColParams> = React.memo(({ api }) => {
};
const selectionChanged = React.useCallback(
(event: SelectionChangedParams) => {
const isAllSelected = api.getAllRowIds().length === e.rows.length && e.rows.length > 0;
const isAllSelected =
api.getAllRowIds().length === event.rows.length && event.rows.length > 0;
const hasNoneSelected = event.rows.length === 0;
setChecked(isAllSelected || !hasNoneSelected);
setIndeterminate(!isAllSelected && !hasNoneSelected);
Expand Down
16 changes: 11 additions & 5 deletions packages/grid/x-grid-modules/src/gridComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import {
useSelection,
useSorting,
} from './hooks/features';
import { DEFAULT_GRID_OPTIONS, ElementSize, GridApi, GridOptions, GridRootRef } from './models';
import {
DEFAULT_GRID_OPTIONS,
ElementSize,
GridApi,
GridOptions,
RootContainerRef,
} from './models';
import { DATA_CONTAINER_CSS_CLASS } from './constants';
import { ColumnsContainer, DataContainer, GridRoot } from './components/styled-wrappers';
import { useVirtualRows } from './hooks/virtualization';
Expand Down Expand Up @@ -35,7 +41,7 @@ export const GridComponent: React.FC<GridComponentProps> = React.memo(
({ rows, columns, options, apiRef, loading, licenseStatus, className, components }) => {
useLoggerFactory(options?.logger, options?.logLevel);
const logger = useLogger('Grid');
const gridRootRef: GridRootRef = React.useRef<HTMLDivElement>(null);
const rootContainerRef: RootContainerRef = React.useRef<HTMLDivElement>(null);
const footerRef = React.useRef<HTMLDivElement>(null);
const columnsHeaderRef = React.useRef<HTMLDivElement>(null);
const columnsContainerRef = React.useRef<HTMLDivElement>(null);
Expand All @@ -55,7 +61,7 @@ export const GridComponent: React.FC<GridComponentProps> = React.memo(
apiRef = internalApiRef;
}

const initialised = useApi(gridRootRef, windowRef, internalOptions, apiRef);
const initialised = useApi(rootContainerRef, windowRef, internalOptions, apiRef);
const internalColumns = useColumns(internalOptions, columns, apiRef);
const internalRows = useRows(internalOptions, rows, initialised, apiRef);
useKeyboard(internalOptions, initialised, apiRef);
Expand Down Expand Up @@ -91,7 +97,7 @@ export const GridComponent: React.FC<GridComponentProps> = React.memo(
components,
paginationProps,
apiRef,
gridRootRef,
rootContainerRef,
);

const onResize = React.useCallback(
Expand Down Expand Up @@ -143,7 +149,7 @@ export const GridComponent: React.FC<GridComponentProps> = React.memo(
<AutoSizerWrapper onResize={debouncedOnResize} style={{ height: 'unset', width: 'unset' }}>
{(size: any) => (
<GridRoot
ref={gridRootRef}
ref={rootContainerRef}
className={`material-grid MuiGrid ${className || ''}`}
options={internalOptions}
style={{ width: size.width, height: getTotalHeight(size) }}
Expand Down
4 changes: 2 additions & 2 deletions packages/grid/x-grid-modules/src/gridComponentProps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Columns, GridApiRef, GridComponentOverridesProp, GridOptions, RowsProp } from './models';
import { Columns, ApiRef, GridComponentOverridesProp, GridOptions, RowsProp } from './models';

/**
* Partial set of [[GridOptions]].
Expand Down Expand Up @@ -28,7 +28,7 @@ export interface GridComponentProps {
/**
* The ref object that allows grid manipulation. Can be instantiated with [[gridApiRef()]]
*/
apiRef?: GridApiRef;
apiRef?: ApiRef;
/**
* Boolean prop that toggle the loading overlay
*/
Expand Down
8 changes: 8 additions & 0 deletions packages/grid/x-grid-modules/src/hooks/features/useApiRef.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as React from 'react';
import { ApiRef } from '../../models/api/apiRef';
import { GridApi } from '../../models/api/gridApi';

/**
* Hook that instantiate an ApiRef to pass in component prop.
*/
export const useApiRef = (): ApiRef => React.useRef<GridApi | null | undefined>();
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { ScrollParams, useLogger } from '../utils';
import { COL_RESIZE_START, COL_RESIZE_STOP, SCROLLING } from '../../constants/eventsConstants';
import { findCellElementsFromCol, findDataContainerFromCurrent } from '../../utils';
import { useStateRef } from '../utils/useStateRef';
import { GridApiRef } from '../../models';
import { ApiRef } from '../../models';

const MIN_COL_WIDTH = 50;
const MOUSE_LEFT_TIMEOUT = 1000;

// TODO improve experience for last column
export const useColumnResize = (
columnsRef: React.RefObject<HTMLDivElement>,
apiRef: GridApiRef,
apiRef: ApiRef,
headerHeight: number,
) => {
const logger = useLogger('useColumnResize');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import * as React from 'react';
import { PaginationProps } from './usePagination';
import {
ComponentParams,
GridApiRef,
ApiRef,
GridComponentOverridesProp,
GridOptions,
GridRootRef,
RootContainerRef,
InternalColumns,
Rows,
} from '../../models';
Expand All @@ -17,8 +17,8 @@ export const useComponents = (
options: GridOptions,
componentOverrides: GridComponentOverridesProp | undefined,
paginationProps: PaginationProps,
apiRef: GridApiRef,
gridRootRef: GridRootRef,
apiRef: ApiRef,
gridRootRef: RootContainerRef,
) => {
const componentParams: ComponentParams = React.useMemo(
() => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { PageChangedParams } from '../../models/params/pageChangedParams';
import { Rows } from '../../models/rows';
import { InternalColumns } from '../../models/colDef/colDef';
import { GridOptions } from '../../models/gridOptions';
import { GridApiRef } from '../../models/api/gridApiRef';
import { PaginationApi } from '../../models/api/paginationApi';
import { ApiRef } from '../../models/api';

export interface PaginationProps {
page: number;
Expand Down Expand Up @@ -49,7 +49,7 @@ export const usePagination = (
rows: Rows,
columns: InternalColumns,
options: GridOptions,
apiRef: GridApiRef,
apiRef: ApiRef,
): PaginationProps => {
const logger = useLogger('usePagination');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
RowSelectedParams,
RowsProp,
SelectionChangedParams,
GridApiRef,
ApiRef,
} from '../../models';
import { useLogger } from '../utils/useLogger';
import {
Expand All @@ -24,7 +24,7 @@ export const useSelection = (
options: GridOptions,
rowsProp: RowsProp,
initialised: boolean,
apiRef: GridApiRef,
apiRef: ApiRef,
): void => {
const logger = useLogger('useSelection');
const selectedItemsRef = React.useRef<RowId[]>([]);
Expand Down
4 changes: 2 additions & 2 deletions packages/grid/x-grid-modules/src/hooks/features/useSorting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
RowModel,
RowsProp,
SortApi,
GridApiRef,
ApiRef,
} from '../../models';
import {
COLUMN_HEADER_CLICKED,
Expand All @@ -31,7 +31,7 @@ export const useSorting = (
options: GridOptions,
rowsProp: RowsProp,
colsProp: Columns,
apiRef: GridApiRef,
apiRef: ApiRef,
) => {
const logger = useLogger('useSorting');
const sortModelRef = React.useRef<SortModel>([]);
Expand Down
4 changes: 2 additions & 2 deletions packages/grid/x-grid-modules/src/hooks/root/useApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
ROW_SELECTED_EVENT,
SELECTION_CHANGED_EVENT,
} from '../../constants/eventsConstants';
import { CellClickedParam, GridOptions, RowClickedParam, GridApiRef } from '../../models';
import { CellClickedParam, GridOptions, RowClickedParam, ApiRef } from '../../models';
import { GridApi } from '../../models/api/gridApi';
import {
CELL_CSS_CLASS,
Expand All @@ -40,7 +40,7 @@ export function useApi(
gridRootRef: React.RefObject<HTMLDivElement>,
windowRef: React.RefObject<HTMLDivElement>,
options: GridOptions,
apiRef: GridApiRef,
apiRef: ApiRef,
): boolean {
const [isApiInitialised, setApiInitialised] = React.useState(false);
const [initialised, setInit] = React.useState(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as React from 'react';
import { useLogger } from '../utils';
import { GridApiRef } from '../../models/api';
import { ApiRef } from '../../models/api';

export function useApiEventHandler(
apiRef: GridApiRef,
apiRef: ApiRef,
eventName: string,
handler?: (args: any) => void,
) {
Expand Down
4 changes: 2 additions & 2 deletions packages/grid/x-grid-modules/src/hooks/root/useApiMethod.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as React from 'react';
import { GridApi, GridApiRef } from '../../models/api';
import { ApiRef, GridApi } from '../../models/api';
import { useLogger } from '../utils';

export function useApiMethod(apiRef: GridApiRef, apiMethods: Partial<GridApi>, apiName: string) {
export function useApiMethod(apiRef: ApiRef, apiMethods: Partial<GridApi>, apiName: string) {
const logger = useLogger('useApiMethod');

React.useEffect(() => {
Expand Down
8 changes: 4 additions & 4 deletions packages/grid/x-grid-modules/src/hooks/root/useColumns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
GridOptions,
InternalColumns,
SortModel,
GridApiRef,
ApiRef,
} from '../../models';
import { Logger, useLogger } from '../utils/useLogger';
import { COLUMNS_UPDATED, POST_SORT } from '../../constants/eventsConstants';
Expand All @@ -31,7 +31,7 @@ function hydrateColumns(
columns: Columns,
withCheckboxSelection: boolean,
logger: Logger,
apiRef: GridApiRef,
apiRef: ApiRef,
): Columns {
logger.debug('Hydrating Columns with default definitions');
let mappedCols = columns.map((c) => ({ ...getColDef(c.type), ...c }));
Expand Down Expand Up @@ -90,7 +90,7 @@ const resetState = (
columns: Columns,
withCheckboxSelection: boolean,
logger: Logger,
apiRef: GridApiRef,
apiRef: ApiRef,
): InternalColumns => {
if (columns.length === 0) {
return initialState;
Expand Down Expand Up @@ -140,7 +140,7 @@ const getUpdatedColumnState = (
export function useColumns(
options: GridOptions,
columns: Columns,
apiRef: GridApiRef,
apiRef: ApiRef,
): InternalColumns {
const logger = useLogger('useColumns');
const [, forceUpdate] = React.useState();
Expand Down
8 changes: 2 additions & 6 deletions packages/grid/x-grid-modules/src/hooks/root/useKeyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
isTabKey,
} from '../../utils';
import { CELL_CSS_CLASS, ROW_CSS_CLASS } from '../../constants/cssClassesConstants';
import { CellIndexCoordinates, GridOptions, GridApiRef } from '../../models';
import { CellIndexCoordinates, GridOptions, ApiRef } from '../../models';
import { useApiEventHandler } from './useApiEventHandler';

const getNextCellIndexes = (code: string, indexes: CellIndexCoordinates) => {
Expand All @@ -42,11 +42,7 @@ const getNextCellIndexes = (code: string, indexes: CellIndexCoordinates) => {
return { ...indexes, rowIndex: indexes.rowIndex + 1 };
};

export const useKeyboard = (
options: GridOptions,
initialised: boolean,
apiRef: GridApiRef,
): void => {
export const useKeyboard = (options: GridOptions, initialised: boolean, apiRef: ApiRef): void => {
const logger = useLogger('useKeyboard');
const isMultipleKeyPressed = React.useRef(false);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as React from 'react';
import { useLogger } from '../utils';
import { isFunction } from '../../utils';
import { GridApiRef } from '../../models';
import { ApiRef } from '../../models';

export const useNativeEventListener = (
apiRef: GridApiRef,
apiRef: ApiRef,
ref: React.MutableRefObject<HTMLDivElement | null> | (() => Element | undefined | null),
eventName: string,
handler?: (event: Event) => any,
Expand Down
4 changes: 2 additions & 2 deletions packages/grid/x-grid-modules/src/hooks/root/useRows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
RowModel,
Rows,
RowsProp,
GridApiRef,
ApiRef,
} from '../../models';
import { useLogger } from '../utils/useLogger';
import { useRafUpdate } from '../utils';
Expand All @@ -27,7 +27,7 @@ export const useRows = (
options: GridOptions,
rows: RowsProp,
initialised: boolean,
apiRef: GridApiRef,
apiRef: ApiRef,
): RowModel[] => {
const logger = useLogger('useRows');
const rowModels = React.useMemo(() => rows.map((r) => createRow(r)), [rows]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
GridOptions,
RenderColumnsProps,
VirtualizationApi,
GridApiRef,
ApiRef,
} from '../../models';
import { useLogger } from '../utils/useLogger';
import { COLUMNS_UPDATED } from '../../constants/eventsConstants';
Expand All @@ -22,7 +22,7 @@ type UseVirtualColumnsReturnType = [

export const useVirtualColumns = (
options: GridOptions,
apiRef: GridApiRef,
apiRef: ApiRef,
): UseVirtualColumnsReturnType => {
const logger = useLogger('useVirtualColumns');
const renderedColRef = useRef<RenderColumnsProps | null>(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
RenderRowProps,
Rows,
VirtualizationApi,
GridApiRef,
ApiRef,
} from '../../models';
import { ScrollParams, useScrollFn } from '../utils';
import { useLogger } from '../utils/useLogger';
Expand All @@ -34,7 +34,7 @@ export const useVirtualRows = (
internalColumns: InternalColumns,
rows: Rows,
options: GridOptions,
apiRef: GridApiRef,
apiRef: ApiRef,
): UseVirtualRowsReturnType => {
const logger = useLogger('useVirtualRows');
const pageRef = useRef<number>(0);
Expand Down
6 changes: 6 additions & 0 deletions packages/grid/x-grid-modules/src/models/api/apiRef.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as React from 'react';
import { GridApi } from './gridApi';
/**
* The apiRef component prop type.
*/
export type ApiRef = React.MutableRefObject<GridApi | null | undefined>;
19 changes: 0 additions & 19 deletions packages/grid/x-grid-modules/src/models/api/gridApiRef.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/grid/x-grid-modules/src/models/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export * from './apiRef';
export * from './columnApi';
export * from './gridApi';
export * from './gridApiRef';
export * from './paginationApi';
export * from './rowApi';
export * from './selectionApi';
Expand Down
1 change: 1 addition & 0 deletions packages/grid/x-grid-modules/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './colDef';
export * from './containerProps';
export * from './elementSize';
export * from './gridOptions';
export * from './rootContainerRef';
export * from './renderContextProps';
export * from './rows';
export * from './sortModel';
Expand Down
Loading