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

[DataGrid] Fix column reorder instability #950

Merged
merged 9 commits into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from 8 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
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import {
InternalColumns,
} from '../../../models/colDef/colDef';
import { ColumnTypesRecord } from '../../../models/colDef/colTypeDef';
import { getDefaultColumnTypes } from '../../../models/colDef/defaultColumnTypes';
import { getColDef } from '../../../models/colDef/getColDef';
import { mergeColTypes } from '../../../utils/mergeUtils';
import { useApiMethod } from '../../root/useApiMethod';
import { optionsSelector } from '../../utils/optionsSelector';
import { Logger, useLogger } from '../../utils/useLogger';
Expand Down Expand Up @@ -56,8 +58,8 @@ function hydrateColumns(
logger: Logger,
): Columns {
logger.debug('Hydrating Columns with default definitions');

const extendedColumns = columns.map((c) => ({ ...getColDef(columnTypes, c.type), ...c }));
const mergedColTypes = mergeColTypes(getDefaultColumnTypes(), columnTypes);
const extendedColumns = columns.map((c) => ({ ...getColDef(mergedColTypes, c.type), ...c }));

if (withCheckboxSelection) {
return [checkboxSelectionColDef, ...extendedColumns];
Expand Down
2 changes: 1 addition & 1 deletion packages/grid/_modules_/grid/hooks/utils/useOptionsProp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DEFAULT_LOCALE_TEXT } from '../../constants/localeTextConstants';
import { GridComponentProps, GridOptionsProp } from '../../GridComponentProps';
import { ApiRef } from '../../models/api/apiRef';
import { DEFAULT_GRID_OPTIONS, GridOptions } from '../../models/gridOptions';
import { mergeOptions } from '../../utils/mergeOptions';
import { mergeOptions } from '../../utils/mergeUtils';
import { useGridReducer } from '../features/core/useGridReducer';

// REDUCER
Expand Down
2 changes: 1 addition & 1 deletion packages/grid/_modules_/grid/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ export * from './sortingUtils';
export * from './domUtils';
export * from './classnames';
export * from './keyboardUtils';
export * from './mergeOptions';
export * from './mergeUtils';
export * from './paramsUtils';
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ export function removeUndefinedProps(options: Object) {
// We intentionally set the types to any to avoid circular deps
export function mergeOptions(defaultOptions: any, options?: any) {
options = removeUndefinedProps(options);
const mergedColTypes = mergeColTypes(defaultOptions.columnTypes, options?.columnTypes);
const mergedOptions = {
...defaultOptions,
...options,
};
mergedOptions.columnTypes = mergedColTypes;
return mergedOptions;
}
22 changes: 22 additions & 0 deletions packages/grid/x-grid/src/tests/reorder.XGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,26 @@ describe('<XGrid /> - Reorder', () => {
expect(getColumnHeaders()).to.deep.equal(['brand', 'id']);
});
});
it('should not reset the column order when a prop change', () => {
oliviertassinari marked this conversation as resolved.
Show resolved Hide resolved
let apiRef: ApiRef;
const rows = [{ id: 0, brand: 'Nike' }];
const columns = [{ field: 'brand' }, { field: 'desc' }, { field: 'type' }];

const Test = () => {
apiRef = useApiRef();

return (
<div style={{ width: 300, height: 300 }}>
<XGrid apiRef={apiRef} rows={rows} columns={columns} onPageChange={() => {}} />
</div>
);
};

const { forceUpdate } = render(<Test />);
expect(getColumnHeaders()).to.deep.equal(['brand', 'desc', 'type']);
apiRef!.current.moveColumn('brand', 2);
expect(getColumnHeaders()).to.deep.equal(['desc', 'type', 'brand']);
forceUpdate(); // test stability
expect(getColumnHeaders()).to.deep.equal(['desc', 'type', 'brand']);
});
});
15 changes: 15 additions & 0 deletions packages/storybook/src/stories/grid-selection.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useDemoData } from '@material-ui/x-grid-data-generator';
import * as React from 'react';
import { action } from '@storybook/addon-actions';
import { XGrid, GridOptionsProp, useApiRef } from '@material-ui/x-grid';
Expand Down Expand Up @@ -63,3 +64,17 @@ export const MultipleSelectWithCheckboxNoClick = () => {
<XGrid rows={data.rows} columns={data.columns} checkboxSelection disableSelectionOnClick />
);
};

export function HandleSelection() {
const { data } = useDemoData({
dataSet: 'Commodity',
rowLength: 100,
});

const [myState, setMyState] = React.useState(false);
const handleSelection = React.useCallback(() => {
setMyState(!myState);
}, [myState]);

return <XGrid {...data} checkboxSelection onSelectionChange={handleSelection} />;
}