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

[core] Remove use of getState #2300

Merged
merged 4 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ Big thanks to the 11 contributors who made this release possible. Here are some

```diff
-const filterState = apiRef.current.getState('filter');
+const filterState = apiRef.current.getState().filter;
+const filterState = apiRef.current.state.filter;
```

- [DataGrid] Improve controllable sorting (#2095) @dtassone
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ export function ColumnHeaderFilterIcon(props: ColumnHeaderFilterIconProps) {
event.preventDefault();
event.stopPropagation();

const { open, openedPanelValue } = gridPreferencePanelStateSelector(
apiRef!.current.getState(),
);
const { open, openedPanelValue } = gridPreferencePanelStateSelector(apiRef!.current.state);

if (open && openedPanelValue === GridPreferencePanelsValue.filters) {
apiRef!.current.hideFilterPanel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export function GridColumnHeaderItem(props: GridColumnHeaderItemProps) {
);

React.useLayoutEffect(() => {
const columnMenuState = apiRef!.current.getState().columnMenu;
const columnMenuState = apiRef!.current.state.columnMenu;
if (hasFocus && !columnMenuState.open) {
const focusableElement = headerCellRef.current!.querySelector(
'[tabindex="0"]',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export const GridHeaderCheckbox = React.forwardRef<HTMLInputElement, GridColumnH
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const checked = event.target.checked;
const rowsToBeSelected = options.checkboxSelectionVisibleOnly
? gridPaginatedVisibleSortedGridRowIdsSelector(apiRef.current.getState())
: visibleSortedGridRowIdsSelector(apiRef.current.getState());
? gridPaginatedVisibleSortedGridRowIdsSelector(apiRef.current.state)
: visibleSortedGridRowIdsSelector(apiRef.current.state);
apiRef!.current.selectRows(rowsToBeSelected, checked, !event.target.indeterminate);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export function useGridColumns(
logger.debug('updating GridColumns with new state');

// Avoid dependency on gridState to avoid infinite loop
const refGridState = apiRef.current.getState();
const refGridState = apiRef.current.state;
const newColumns: GridColumns = newState.all.map((field) => newState.lookup[field]);
const updatedCols = getStateColumns(newColumns, refGridState.viewportSizes.width);

Expand All @@ -220,7 +220,7 @@ export function useGridColumns(
const updateColumns = React.useCallback(
(cols: GridColDef[]) => {
// Avoid dependency on gridState to avoid infinite loop
const newState = upsertColumnsState(cols, apiRef.current.getState().columns);
const newState = upsertColumnsState(cols, apiRef.current.state.columns);
setColumnsState(newState, false);
},
[apiRef, setColumnsState],
Expand Down Expand Up @@ -340,7 +340,7 @@ export function useGridColumns(
`GridColumns gridState.viewportSizes.width, changed ${gridState.viewportSizes.width}`,
);
// Avoid dependency on gridState as I only want to update cols when viewport size changed.
const currentColumns = allGridColumnsSelector(apiRef.current.getState());
const currentColumns = allGridColumnsSelector(apiRef.current.state);

apiRef.current.updateColumns(currentColumns);
}, [apiRef, gridState.viewportSizes.width, logger]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ export const useGridFilter = (

const onColUpdated = React.useCallback(() => {
logger.debug('onColUpdated - GridColumns changed, applying filters');
const filterState = apiRef.current.getState().filter;
const filterState = apiRef.current.state.filter;
const columnsIds = filterableGridColumnsIdsSelector(apiRef.current.state);
logger.debug('GridColumns changed, applying filters');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const useGridFocus = (apiRef: GridApiRef, props: Pick<GridComponentProps,

const setColumnHeaderFocus = React.useCallback(
(field: string, event?: React.SyntheticEvent) => {
const { cell } = apiRef.current.getState().focus;
const { cell } = apiRef.current.state.focus;
if (cell) {
apiRef.current.publishEvent(
GRID_CELL_FOCUS_OUT,
Expand Down Expand Up @@ -92,7 +92,7 @@ export const useGridFocus = (apiRef: GridApiRef, props: Pick<GridComponentProps,

const handleCellMouseUp = React.useCallback(
(params: GridCellParams) => {
const { cell } = apiRef.current.getState().focus;
const { cell } = apiRef.current.state.focus;
if (!cell) {
return;
}
Expand All @@ -109,7 +109,7 @@ export const useGridFocus = (apiRef: GridApiRef, props: Pick<GridComponentProps,
const isInsideFocusedCell = insideFocusedCell.current;
insideFocusedCell.current = false;

const { cell } = apiRef.current.getState().focus;
const { cell } = apiRef.current.state.focus;
if (!cell || isInsideFocusedCell) {
return;
}
Expand Down Expand Up @@ -138,7 +138,7 @@ export const useGridFocus = (apiRef: GridApiRef, props: Pick<GridComponentProps,
if (params.cellMode === 'view') {
return;
}
const { cell } = apiRef.current.getState().focus;
const { cell } = apiRef.current.state.focus;
if (cell?.id !== params.id || cell?.field !== params.field) {
apiRef.current.setCellFocus(params.id, params.field);
}
Expand All @@ -156,7 +156,7 @@ export const useGridFocus = (apiRef: GridApiRef, props: Pick<GridComponentProps,
);

React.useEffect(() => {
const { cell } = apiRef.current.getState().focus;
const { cell } = apiRef.current.state.focus;

if (cell) {
const updatedRow = apiRef.current.getRow(cell.id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const useGridKeyboard = (apiRef: GridApiRef): void => {

apiRef.current.publishEvent(GRID_CELL_NAVIGATION_KEY_DOWN, params, event);

const focusCell = apiRef.current.getState().focus.cell!;
const focusCell = apiRef.current.state.focus.cell!;
const rowIndex = apiRef.current.getRowIndex(focusCell.id);
// We select the rows in between
const rowIds = Array(Math.abs(rowIndex - selectionFromRowIndex) + 1).fill(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const useGridPageSize = (

React.useEffect(() => {
const autoPageSize = containerSizes?.viewportPageSize;
const prevPageSize = apiRef.current.getState().pagination.pageSize;
const prevPageSize = apiRef.current.state.pagination.pageSize;

let pageSize = prevPageSize;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export function useGridEditRows(
);

const handleColumnHeaderDragStart = useEventCallback((nativeEvent) => {
const { cell } = apiRef.current.getState().focus;
const { cell } = apiRef.current.state.focus;
if (!cell) {
return;
}
Expand Down Expand Up @@ -106,7 +106,7 @@ export function useGridEditRows(

const getCellMode = React.useCallback(
(id, field) => {
const editState = apiRef.current.getState().editRows;
const editState = apiRef.current.state.editRows;
const isEditing = editState[id] && editState[id][field];
return isEditing ? 'edit' : 'view';
},
Expand Down Expand Up @@ -172,7 +172,7 @@ export function useGridEditRows(
);

const getEditRowsModel = React.useCallback(
(): GridEditRowsModel => apiRef.current.getState().editRows,
(): GridEditRowsModel => apiRef.current.state.editRows,
[apiRef],
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const useGridSelection = (apiRef: GridApiRef, props: GridComponentProps):
options;

const getSelectedRows = React.useCallback(
() => selectedGridRowsSelector(apiRef.current.getState()),
() => selectedGridRowsSelector(apiRef.current.state),
[apiRef],
);

Expand Down Expand Up @@ -153,7 +153,7 @@ export const useGridSelection = (apiRef: GridApiRef, props: GridComponentProps):

const setSelectionModel = React.useCallback<GridSelectionApi['setSelectionModel']>(
(model) => {
const currentModel = apiRef.current.getState().selection;
const currentModel = apiRef.current.state.selection;
if (currentModel !== model) {
setGridState((state) => ({ ...state, selection: model }));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export const useGridSorting = (
return;
}

const sortModel = apiRef.current.getState().sorting.sortModel;
const sortModel = apiRef.current.state.sorting.sortModel;

if (sortModel.length > 0) {
const comparatorList = buildComparatorList(sortModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export const useGridVirtualRows = (apiRef: GridApiRef): void => {

const updateViewport = React.useCallback(
(forceReRender = false) => {
const lastState = apiRef.current.getState();
const lastState = apiRef.current.state;
const containerProps = lastState.containerSizes;
if (!windowRef || !windowRef.current || !containerProps) {
return;
Expand Down Expand Up @@ -302,7 +302,7 @@ export const useGridVirtualRows = (apiRef: GridApiRef): void => {
);

const getScrollPosition = React.useCallback(
() => scrollStateSelector(apiRef.current.getState()),
() => scrollStateSelector(apiRef.current.state),
[apiRef],
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function useResizeContainer(
gridLogger.debug(`resizing...`);

apiRef.current.publishEvent(GRID_DEBOUNCED_RESIZE, {
containerSize: apiRef.current.getState().containerSizes?.windowSizes,
containerSize: apiRef.current.state.containerSizes?.windowSizes,
});
}, [apiRef, gridLogger]);

Expand Down
3 changes: 1 addition & 2 deletions packages/grid/_modules_/grid/models/api/gridStateApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ export interface GridStateApi {
state: GridState;
/**
* Returns the state of the grid.
* @param {string} stateId The part of the state to be returned.
* @returns {any} The state of the grid.
* @returns {GridState} The state of the grid.
Copy link
Member

Choose a reason for hiding this comment

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

Should we not document it for now?

Suggested change
* @returns {GridState} The state of the grid.
* @returns {GridState} The state of the grid.
* @ignore - do not document.

Copy link
Member

Choose a reason for hiding this comment

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

If we stop using it internally, we can just remove it.

Copy link
Member Author

Choose a reason for hiding this comment

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

@flaviendelangle Removing the method would be a breaking change. We can move more iteratively.

*/
getState: () => GridState;
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const gridCheckboxSelectionColDef: GridColDef = {
disableColumnMenu: true,
disableReorder: true,
valueGetter: (params) => {
const selectionLookup = selectedIdsLookupSelector(params.api.getState());
const selectionLookup = selectedIdsLookupSelector(params.api.state);
return selectionLookup[params.id] !== undefined;
},
renderHeader: (params) => <GridHeaderCheckbox {...params} />,
Expand Down
2 changes: 1 addition & 1 deletion packages/grid/x-grid/src/tests/filtering.XGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ describe('<XGrid /> - Filter', () => {
render(<TestCase checkboxSelection filterModel={newModel} />);
const checkAllCell = getColumnHeaderCell(0).querySelector('input');
fireEvent.click(checkAllCell);
expect(apiRef.current.getState().selection).to.deep.equal([1]);
expect(apiRef.current.state.selection).to.deep.equal([1]);
});

it('should allow to clear filters by passing an empty filter model', () => {
Expand Down
34 changes: 17 additions & 17 deletions packages/grid/x-grid/src/tests/rows.XGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ describe('<XGrid /> - Rows', () => {
/>,
);

const isVirtualized = apiRef!.current!.getState().containerSizes!.isVirtualized;
const isVirtualized = apiRef!.current!.state.containerSizes!.isVirtualized;
expect(isVirtualized).to.equal(false);
});

Expand All @@ -339,7 +339,7 @@ describe('<XGrid /> - Rows', () => {
/>,
);

let isVirtualized = apiRef!.current!.getState().containerSizes!.isVirtualized;
let isVirtualized = apiRef!.current!.state.containerSizes!.isVirtualized;
expect(isVirtualized).to.equal(false);

render(
Expand All @@ -351,13 +351,13 @@ describe('<XGrid /> - Rows', () => {
/>,
);

isVirtualized = apiRef!.current!.getState().containerSizes!.isVirtualized;
isVirtualized = apiRef!.current!.state.containerSizes!.isVirtualized;
expect(isVirtualized).to.equal(true);
});

it('should render last row when scrolling to the bottom', () => {
render(<TestCaseVirtualization nbRows={996} hideFooter height={600} />);
const totalHeight = apiRef!.current!.getState().containerSizes?.totalSizes.height!;
const totalHeight = apiRef!.current!.state.containerSizes?.totalSizes.height!;

const gridWindow = document.querySelector('.MuiDataGrid-window')!;
const renderingZone = document.querySelector('.MuiDataGrid-renderingZone')! as HTMLElement;
Expand All @@ -374,14 +374,14 @@ describe('<XGrid /> - Rows', () => {
it('Rows should not be virtualized when the grid is in pagination autoPageSize', () => {
render(<TestCaseVirtualization autoPageSize pagination />);

const isVirtualized = apiRef!.current!.getState().containerSizes!.isVirtualized;
const isVirtualized = apiRef!.current!.state.containerSizes!.isVirtualized;
expect(isVirtualized).to.equal(false);
});

it('Rows should not be virtualized when the grid is in autoHeight', () => {
render(<TestCaseVirtualization autoHeight />);

const isVirtualized = apiRef!.current!.getState().containerSizes!.isVirtualized;
const isVirtualized = apiRef!.current!.state.containerSizes!.isVirtualized;
expect(isVirtualized).to.equal(false);
});

Expand All @@ -395,7 +395,7 @@ describe('<XGrid /> - Rows', () => {
let lastCell = document.querySelector('[role="row"]:last-child [role="cell"]:first-child')!;
expect(lastCell).to.have.text('995');

let virtualPage = apiRef!.current!.getState().rendering!.virtualPage;
let virtualPage = apiRef!.current!.state.rendering!.virtualPage;
expect(virtualPage).to.equal(98);

setProps({ nbRows: 9 });
Expand All @@ -406,10 +406,10 @@ describe('<XGrid /> - Rows', () => {
const renderingZone = document.querySelector('.MuiDataGrid-renderingZone')! as HTMLElement;
expect(renderingZone.children.length).to.equal(9);

virtualPage = apiRef!.current!.getState().rendering!.virtualPage;
virtualPage = apiRef!.current!.state.rendering!.virtualPage;
expect(virtualPage).to.equal(0);

const isVirtualized = apiRef!.current!.getState().containerSizes!.isVirtualized;
const isVirtualized = apiRef!.current!.state.containerSizes!.isVirtualized;
expect(isVirtualized).to.equal(false);
});

Expand All @@ -424,7 +424,7 @@ describe('<XGrid /> - Rows', () => {
'[role="row"]:last-child [role="cell"]:first-child',
)!;
expect(lastCell).to.have.text('31');
const totalHeight = apiRef!.current!.getState().containerSizes?.totalSizes.height!;
const totalHeight = apiRef!.current!.state.containerSizes?.totalSizes.height!;
expect(gridWindow.scrollHeight).to.equal(totalHeight);
});

Expand All @@ -449,9 +449,9 @@ describe('<XGrid /> - Rows', () => {
expect(gridWindow.scrollTop).to.equal(0);
expect(gridWindow.scrollHeight).to.equal(gridWindow.clientHeight);

const isVirtualized = apiRef!.current!.getState().containerSizes!.isVirtualized;
const isVirtualized = apiRef!.current!.state.containerSizes!.isVirtualized;
expect(isVirtualized).to.equal(false);
const virtualRowsCount = apiRef!.current!.getState().containerSizes!.virtualRowsCount;
const virtualRowsCount = apiRef!.current!.state.containerSizes!.virtualRowsCount;
expect(virtualRowsCount).to.equal(4);
});

Expand All @@ -471,9 +471,9 @@ describe('<XGrid /> - Rows', () => {
expect(gridWindow.scrollTop).to.equal(0);
expect(gridWindow.scrollHeight).to.equal(gridWindow.clientHeight);

const isVirtualized = apiRef!.current!.getState().containerSizes!.isVirtualized;
const isVirtualized = apiRef!.current!.state.containerSizes!.isVirtualized;
expect(isVirtualized).to.equal(false);
const virtualRowsCount = apiRef!.current!.getState().containerSizes!.virtualRowsCount;
const virtualRowsCount = apiRef!.current!.state.containerSizes!.virtualRowsCount;
expect(virtualRowsCount).to.equal(7);
});
});
Expand Down Expand Up @@ -608,7 +608,7 @@ describe('<XGrid /> - Rows', () => {
render(<TestCase rows={baselineProps.rows} />);

fireEvent.click(getCell(0, 0));
expect(apiRef.current.getState().focus.cell).to.deep.equal({
expect(apiRef.current.state.focus.cell).to.deep.equal({
id: baselineProps.rows[0].id,
field: baselineProps.columns[0].field,
});
Expand All @@ -619,15 +619,15 @@ describe('<XGrid /> - Rows', () => {

fireEvent.click(getCell(0, 0));
setProps({ rows: baselineProps.rows.slice(1) });
expect(apiRef.current.getState().focus.cell).to.equal(null);
expect(apiRef.current.state.focus.cell).to.equal(null);
});

it('should not reset focus when removing a row not containing the focus cell', () => {
const { setProps } = render(<TestCase rows={baselineProps.rows} />);

fireEvent.click(getCell(1, 0));
setProps({ rows: baselineProps.rows.slice(1) });
expect(apiRef.current.getState().focus.cell).to.deep.equal({
expect(apiRef.current.state.focus.cell).to.deep.equal({
id: baselineProps.rows[1].id,
field: baselineProps.columns[0].field,
});
Expand Down