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(react-grid): fix column reordering (T1079946) #3543

Merged
merged 3 commits into from
Apr 13, 2022
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
45 changes: 45 additions & 0 deletions packages/dx-grid-core/src/plugins/virtual-table/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getScrollTop,
getTopRowId,
getScrollLeft,
isColumnsWidthDifferent,
} from './helpers';
import { TOP_POSITION, BOTTOM_POSITION, LEFT_POSITION, RIGHT_POSITION } from './constants';

Expand Down Expand Up @@ -274,3 +275,47 @@ describe('#getScrollLeft', () => {
expect(getScrollLeft(3, 10, RIGHT_POSITION)).toBe(30);
});
});

describe('#isColumnsWidthDifferent', () => {
it('should return false, columns reordering only', () => {
const prevColumns = [
{ width: '20px', key: 'column1' },
{ width: '10px', key: 'column2' },
{ width: '5px', key: 'column3' },
] as any;
const columns = [
{ width: '10px', key: 'column2' },
{ width: '20px', key: 'column1' },
{ width: '5px', key: 'column3' },
] as any;
expect(isColumnsWidthDifferent(prevColumns, columns)).toBeFalsy();
});

it('should return true, columns width changed', () => {
const prevColumns = [
{ width: 20, key: 'column1' },
{ width: 10, key: 'column2' },
{ width: 5, key: 'column3' },
] as any;
const columns = [
{ width: 20, key: 'column1' },
{ width: 20, key: 'column2' },
{ width: 5, key: 'column3' },
] as any;
expect(isColumnsWidthDifferent(prevColumns, columns)).toBeTruthy();
});

it('should return true, columns changed', () => {
const prevColumns = [
{ width: 20, key: 'column1' },
{ width: 10, key: 'column2' },
{ width: 5, key: 'column3' },
] as any;
const columns = [
{ width: 20, key: 'column1' },
{ width: 10, key: 'column2' },
{ width: 5, key: 'column4' },
] as any;
expect(isColumnsWidthDifferent(prevColumns, columns)).toBeTruthy();
});
});
8 changes: 8 additions & 0 deletions packages/dx-grid-core/src/plugins/virtual-table/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
GetScrollPosition,
GetTopRowId,
GetScrollLeft,
IsColumnsWidthDifferent,
} from '../../types';
import { arraysEqual } from './utils';
import { TOP_POSITION, BOTTOM_POSITION, LEFT_POSITION } from './constants';
Expand Down Expand Up @@ -141,3 +142,10 @@ export const getTopRowId: GetTopRowId = (viewport, tableBodyRows, isDataRemote)

return undefined;
};

export const isColumnsWidthDifferent: IsColumnsWidthDifferent = (prevColumns, columns) => {
return prevColumns.some((column) => {
const currentColumn = columns.find(c => c.key === column.key);
return currentColumn ? currentColumn.width !== column.width : true;
});
};
2 changes: 2 additions & 0 deletions packages/dx-grid-core/src/types/virtual-table.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,5 @@ export type GetTopRowId = PureComputed<
export type GetScrollLeft = PureComputed<
[number, number, typeof LEFT_POSITION | typeof RIGHT_POSITION | undefined], number | undefined
>;
/** @internal */
export type IsColumnsWidthDifferent = PureComputed<[TableColumn[], TableColumn[]], boolean>;
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jest.mock('@devexpress/dx-grid-core', () => {
jest.spyOn(actual, 'getCollapsedGrids');
jest.spyOn(actual, 'getColumnWidthGetter');
jest.spyOn(actual, 'getScrollLeft');
jest.spyOn(actual, 'isColumnsWidthDifferent');
return actual;
});
jest.mock('./column-group', () => ({
Expand Down Expand Up @@ -238,7 +239,6 @@ describe('VirtualTableLayout', () => {
footerRows={defaultProps.bodyRows.slice(0, 2)}
/>
));

expect(tree.find(defaultProps.containerComponent).props().style)
.toMatchObject({ height: defaultProps.height });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { MemoizedFunction, memoize } from '@devexpress/dx-core';
import {
TableColumn, GetColumnWidthFn, getCollapsedGrids,
getColumnWidthGetter, TABLE_STUB_TYPE, getViewport, GridViewport, getScrollLeft,
isColumnsWidthDifferent,
} from '@devexpress/dx-grid-core';
import { VirtualTableLayoutState, VirtualTableLayoutProps } from '../../types';
import { VirtualTableLayoutBlock } from './virtual-table-layout-block';
Expand Down Expand Up @@ -67,9 +68,7 @@ export class VirtualTableLayout extends React.PureComponent<PropsType, VirtualTa
const columnCountChanged = prevProps.columns.length !== columns.length;

if (bodyRowsChanged || columnCountChanged || columns[0].width !== undefined &&
prevProps.columns.some((column, index) => {
return column.width !== columns[index].width;
})
isColumnsWidthDifferent(prevProps.columns, columns)
) {
this.updateViewport();
}
Expand Down