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 colspan calculation #3471

Merged
merged 3 commits into from
Jan 17, 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
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,11 @@ describe('TableGroupRow Plugin computeds', () => {
const getCellColSpanGetter = tableGroupCellColSpanGetter(
parentGetCellColSpan, [],
);
const tableColumn = { type: TABLE_GROUP_TYPE, column: { name: 'a' } };
const tableColumn = {
type: TABLE_GROUP_TYPE,
column: { name: 'a' },
key: `${TABLE_GROUP_TYPE.toString()}_a`,
};

expect(getCellColSpanGetter({
tableColumn,
Expand Down Expand Up @@ -197,7 +201,11 @@ describe('TableGroupRow Plugin computeds', () => {

describe('Virtual Table', () => {
[0, 1, 2].map((firstVisibleColumnIndex) => {
const tableColumn = { type: TABLE_GROUP_TYPE, column: { name: 'a' } };
const tableColumn = {
type: TABLE_GROUP_TYPE,
column: { name: 'a' },
key: `${TABLE_GROUP_TYPE.toString()}_a`,
};
const getCellColSpanGetter = tableGroupCellColSpanGetter(
parentGetCellColSpan, [], firstVisibleColumnIndex,
);
Expand Down Expand Up @@ -247,8 +255,12 @@ describe('TableGroupRow Plugin computeds', () => {
const getCellColSpanGetter = tableGroupCellColSpanGetter(
parentGetCellColSpan, [],
);
const tableDataColumn = { column: { name: 'a' } };
const tableGroupColumn = { type: TABLE_GROUP_TYPE, column: { name: 'a' } };
const tableDataColumn = { column: { name: 'a' }, key: `${TABLE_DATA_TYPE.toString()}_a` };
const tableGroupColumn = {
type: TABLE_GROUP_TYPE,
column: { name: 'a' },
key: `${TABLE_GROUP_TYPE.toString()}_a`,
};
const tableRow = { type: TABLE_GROUP_TYPE, row: { groupedBy: 'a' } };
const tableColumns = [tableGroupColumn, tableDataColumn, {}];

Expand All @@ -267,9 +279,55 @@ describe('TableGroupRow Plugin computeds', () => {
.toBe(3);
});

it('should return correct colspan, grouped several columns', () => {
const getCellColSpanGetter = tableGroupCellColSpanGetter(
parentGetCellColSpan, [],
);
const tableRow = { type: TABLE_GROUP_TYPE, row: { groupedBy: 'c' } };
const tableColumns = [
{ type: TABLE_GROUP_TYPE, column: { name: 'a' }, key: `${TABLE_GROUP_TYPE.toString()}_a` },
{ type: TABLE_GROUP_TYPE, column: { name: 'c' }, key: `${TABLE_GROUP_TYPE.toString()}_c` },
{ column: { name: 'a' }, key: `${TABLE_DATA_TYPE.toString()}_a` },
{ column: { name: 'b' }, key: `${TABLE_DATA_TYPE.toString()}_b` },
{ column: { name: 'c' }, key: `${TABLE_DATA_TYPE.toString()}_c` },
{ column: { name: 'd' }, key: `${TABLE_DATA_TYPE.toString()}_d` },
];

expect(tableColumns.map(
tableColumn => getCellColSpanGetter({
tableColumn,
tableRow,
tableColumns,
})))
.toEqual([1, 5, 'original', 'original', 'original', 'original']);
});

it('should return correct colspan, grouped and columns with the same name', () => {
const getCellColSpanGetter = tableGroupCellColSpanGetter(
parentGetCellColSpan, [],
);
const tableRow = { type: TABLE_GROUP_TYPE, row: { groupedBy: 'a' } };
const tableColumns = [
{ type: TABLE_GROUP_TYPE, column: { name: 'a' }, key: `${TABLE_GROUP_TYPE.toString()}_a` },
{ column: { name: 'a' }, key: `${TABLE_DATA_TYPE.toString()}_a` },
{ column: { name: 'b' }, key: `${TABLE_DATA_TYPE.toString()}_b` },
{ column: { name: 'c' }, key: `${TABLE_DATA_TYPE.toString()}_c` },
{ column: { name: 'a' }, key: `${TABLE_DATA_TYPE.toString()}_a` },
{ column: { name: 'd' }, key: `${TABLE_DATA_TYPE.toString()}_d` },
];

expect(tableColumns.map(
tableColumn => getCellColSpanGetter({
tableColumn,
tableRow,
tableColumns,
})))
.toEqual([6, 'original', 'original', 'original', 'original', 'original']);
});

describe('with summary', () => {
const tableColumns = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
.map(name => ({ column: { name } }));
.map(name => ({ column: { name }, key: `type_${name}` }));
const groupSummaryItems = [
{ columnName: 'b', type: 'sum', showInGroupFooter: false, alignByColumn: true },
{ columnName: 'd', type: 'sum', showInGroupFooter: false },
Expand All @@ -288,7 +346,7 @@ describe('TableGroupRow Plugin computeds', () => {
tableRow,
tableColumns,
})))
.toEqual(['original', 1, 2, 'original', 1, 3, 'original', 'original']);
.toEqual([1, 1, 2, 'original', 1, 3, 'original', 'original']);
});
});
});
Expand Down
13 changes: 6 additions & 7 deletions packages/dx-grid-core/src/plugins/table-group-row/computeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ const groupSummaryChains: GroupSummaryChainsFn = (
return sortAndSpliceColumns(tableColumns, firstVisibleColumnIndex)
.reduce((acc, col) => {
const colName = (col.column && col.column.name) as string;
const colKey = col.key;
const isStartOfGroupCaption = col.type === TABLE_GROUP_TYPE
&& tableRow.row.groupedBy === colName;
const isIndentColumn = col.type === TABLE_GROUP_TYPE
Expand All @@ -96,12 +97,12 @@ const groupSummaryChains: GroupSummaryChainsFn = (
}

if (isStartOfGroupCaption || isIndentColumn) {
acc.push([colName]);
acc.push([colKey]);
} else if (groupSummaryItems && isRowLevelSummary(groupSummaryItems, colName)) {
acc.push([colName]);
acc.push([colKey]);
acc.push([]);
} else {
acc[acc.length - 1].push(colName);
acc[acc.length - 1].push(colKey);
}
return acc;
}, [[]] as string[][]);
Expand All @@ -113,13 +114,11 @@ export const tableGroupCellColSpanGetter: GroupCellColSpanGetter = (
const { tableRow, tableColumns, tableColumn } = params;

if (tableRow.type === TABLE_GROUP_TYPE) {
const colName = tableColumn.column?.name;
const dataColumnGroupedBy =
tableRow.row.groupedBy === colName && tableColumn.type !== TABLE_GROUP_TYPE;
const colKey = tableColumn.key;
const chains = groupSummaryChains(
tableRow, tableColumns, groupSummaryItems, firstVisibleColumnIndex,
);
const chain = chains.find(ch => !dataColumnGroupedBy && ch[0] === colName);
const chain = chains.find(ch => ch[0] === colKey);

if (chain) {
return chain.length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,36 +146,36 @@ describe('TableRowDetail Plugin helpers', () => {

it('should work with the Table (firstVisibleColumnIndex is not defined)', () => {
expect(sortAndSpliceColumns([groupColumn, otherColumn, dataColumn, flexColumn]))
.toEqual([groupColumn, otherColumn, dataColumn, flexColumn]);
.toEqual([groupColumn, otherColumn, dataColumn]);

expect(sortAndSpliceColumns([otherColumn, groupColumn, dataColumn, flexColumn]))
.toEqual([groupColumn, otherColumn, dataColumn, flexColumn]);
.toEqual([groupColumn, otherColumn, dataColumn]);

expect(sortAndSpliceColumns([otherColumn, groupColumn, otherColumn, dataColumn, flexColumn]))
.toEqual([groupColumn, otherColumn, otherColumn, dataColumn, flexColumn]);
.toEqual([groupColumn, otherColumn, otherColumn, dataColumn]);
});

describe('should work with the Virtual Table (firstVisibleColumnIndex is defined)', () => {
it('should work with one group', () => {
expect(sortAndSpliceColumns(
[otherColumn, otherColumn, groupColumn, otherColumn, dataColumn, flexColumn], 0,
))
.toEqual([groupColumn, otherColumn, otherColumn, otherColumn, dataColumn, flexColumn]);
.toEqual([groupColumn, otherColumn, otherColumn, otherColumn, dataColumn]);

expect(sortAndSpliceColumns(
[otherColumn, otherColumn, groupColumn, otherColumn, dataColumn, flexColumn], 1,
))
.toEqual([groupColumn, otherColumn, otherColumn, dataColumn, flexColumn]);
.toEqual([groupColumn, otherColumn, otherColumn, dataColumn]);

expect(sortAndSpliceColumns(
[otherColumn, otherColumn, groupColumn, otherColumn, dataColumn, flexColumn], 2,
))
.toEqual([groupColumn, otherColumn, dataColumn, flexColumn]);
.toEqual([groupColumn, otherColumn, dataColumn]);

expect(sortAndSpliceColumns(
[otherColumn, otherColumn, groupColumn, otherColumn, dataColumn, flexColumn], 3,
))
.toEqual([groupColumn, otherColumn, dataColumn, flexColumn]);
.toEqual([groupColumn, otherColumn, dataColumn]);
});

it('should work with two groups', () => {
Expand Down
3 changes: 1 addition & 2 deletions packages/dx-grid-core/src/plugins/table-group-row/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ export const sortAndSpliceColumns: PureComputed<[TableColumn[], number]> = (
) => {
const groupColumns = tableColumns.filter(col => col.type === TABLE_GROUP_TYPE);
const dataColumns = tableColumns.filter(col => col.type === TABLE_DATA_TYPE);
const flexColumns = tableColumns.filter(col => col.type === TABLE_FLEX_TYPE);
const otherColumns = tableColumns.filter(
col =>
col.type !== TABLE_DATA_TYPE &&
Expand All @@ -106,5 +105,5 @@ export const sortAndSpliceColumns: PureComputed<[TableColumn[], number]> = (
otherColumns.splice(0, Math.min(firstVisibleColumnIndex, firstGroupIndex));
}

return [...groupColumns, ...otherColumns, ...dataColumns, ...flexColumns];
return [...groupColumns, ...otherColumns, ...dataColumns];
};