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

Fix columns #542

Merged
merged 4 commits into from
Nov 6, 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
48 changes: 31 additions & 17 deletions packages/grid/_modules_/grid/hooks/features/columns/useColumns.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { COLUMNS_UPDATED } from '../../../constants/eventsConstants';
import { COLUMNS_UPDATED, RESIZE } from '../../../constants/eventsConstants';
import { ApiRef } from '../../../models/api/apiRef';
import { ColumnApi } from '../../../models/api/columnApi';
import { checkboxSelectionColDef } from '../../../models/colDef/checkboxSelection';
Expand All @@ -12,6 +12,7 @@ import {
} from '../../../models/colDef/colDef';
import { ColumnTypesRecord } from '../../../models/colDef/colTypeDef';
import { getColDef } from '../../../models/colDef/getColDef';
import { useApiEventHandler } from '../../root/useApiEventHandler';
import { useApiMethod } from '../../root/useApiMethod';
import { Logger, useLogger } from '../../utils/useLogger';
import { useGridState } from '../core/useGridState';
Expand Down Expand Up @@ -149,7 +150,6 @@ const getUpdatedColumnState = (
export function useColumns(columns: Columns, apiRef: ApiRef): InternalColumns {
const logger = useLogger('useColumns');
const [gridState, setGridState, forceUpdate] = useGridState(apiRef);
const viewportWidth = gridState.containerSizes ? gridState.containerSizes.viewportSize.width : 0;
const updateState = React.useCallback(
(newState: InternalColumns, emit = true) => {
logger.debug('Updating columns state.');
Expand All @@ -163,24 +163,30 @@ export function useColumns(columns: Columns, apiRef: ApiRef): InternalColumns {
[logger, setGridState, forceUpdate, apiRef],
);

React.useEffect(() => {
logger.info(`Columns have change, new length ${columns.length}`);
const newState = resetState(
const resetColumns = React.useCallback(
(width: number) => {
logger.info(`Columns have change, new length ${columns.length}`);
const newState = resetState(
columns,
gridState.options.columnTypes,
width,
!!gridState.options.checkboxSelection,
logger,
);
updateState(newState);
},
[
columns,
gridState.options.checkboxSelection,
gridState.options.columnTypes,
viewportWidth,
!!gridState.options.checkboxSelection,
logger,
);
updateState(newState);
}, [
columns,
gridState.options.columnTypes,
gridState.options.checkboxSelection,
viewportWidth,
logger,
updateState,
]);
updateState,
],
);

React.useEffect(() => {
resetColumns(gridState.containerSizes?.viewportSize.width || 0);
}, [gridState.containerSizes?.viewportSize.width, resetColumns]);

const getColumnFromField: (field: string) => ColDef = React.useCallback(
(field) => gridState.columns.lookup[field],
Expand Down Expand Up @@ -226,5 +232,13 @@ export function useColumns(columns: Columns, apiRef: ApiRef): InternalColumns {

useApiMethod(apiRef, colApi, 'ColApi');

const onResize = React.useCallback(
(size) => {
resetColumns(size.width);
},
[resetColumns],
);
useApiEventHandler(apiRef, RESIZE, onResize);
Comment on lines +235 to +241
Copy link
Member

Choose a reason for hiding this comment

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

From what I understand gridState.containerSizes?.viewportSize.width already depends on the resize event, so this whole block is duplicated logic.


return gridState.columns;
}
28 changes: 28 additions & 0 deletions packages/storybook/src/stories/grid-pagination.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,31 @@ export function Page2Api() {
</div>
);
}

const rows = [
{
id: 0,
brand: 'Nike',
},
{
id: 1,
brand: 'Addidas',
},
{
id: 2,
brand: 'Puma',
},
];

const columns = [{ field: 'brand' }];
export function XGridTest2Api() {
const apiRef = useApiRef();
React.useEffect(() => {
apiRef.current.setPage(2);
}, [apiRef]);
return (
<div style={{ width: 300, height: 300 }}>
<XGrid rows={rows} apiRef={apiRef} columns={columns} pagination pageSize={1} />
</div>
);
}