Skip to content

Commit 43593eb

Browse files
authored
refactor(react-grid): standardize draft change actions in GroupingState (#668)
BREAKING CHANGES: The `draftGrouping` getter does not contain information about draft mode anymore. The `draftGroupingChange`, and `cancelGroupingChange` actions have been renamed to `draftColumnGrouping`, and `cancelColumnGroupingDraft`.
1 parent 26735c7 commit 43593eb

File tree

23 files changed

+283
-279
lines changed

23 files changed

+283
-279
lines changed

packages/dx-grid-core/src/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export * from './plugins/integrated-filtering/computeds';
1010

1111
export * from './plugins/grouping-state/constants';
1212
export * from './plugins/grouping-state/reducers';
13-
export * from './plugins/grouping-state/computeds';
1413
export * from './plugins/integrated-grouping/computeds';
1514
export * from './plugins/custom-grouping/computeds';
1615
export * from './plugins/grouping-panel/helpers';
Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
export const groupingPanelItems = (columns, grouping) =>
2-
grouping.map(({ columnName, draft }) => {
3-
const column = columns.find(c => c.name === columnName);
4-
return {
5-
column,
6-
draft,
7-
};
1+
export const groupingPanelItems = (columns, grouping, draftGrouping) => {
2+
const items = draftGrouping.map(({ columnName }) => ({
3+
column: columns.find(c => c.name === columnName),
4+
draft: !grouping.some(columnGrouping => columnGrouping.columnName === columnName),
5+
}));
6+
7+
grouping.forEach(({ columnName }, index) => {
8+
if (draftGrouping.some(columnGrouping => columnGrouping.columnName === columnName)) return;
9+
items.splice(index, 0, {
10+
column: columns.find(c => c.name === columnName),
11+
draft: true,
12+
});
813
});
14+
15+
return items;
16+
};

packages/dx-grid-core/src/plugins/grouping-panel/helpers.test.js

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,57 @@ describe('Plugin helpers', () => {
1010
{ name: 'c' },
1111
{ name: 'd' },
1212
];
13-
const draftGrouping = [
14-
{ columnName: 'a' },
15-
{ columnName: 'c', draft: true },
16-
];
1713

18-
it('should work', () => {
19-
const processedColumns = groupingPanelItems(columns, draftGrouping);
14+
it('should work with normal conditions', () => {
15+
const grouping = [
16+
{ columnName: 'a' },
17+
{ columnName: 'c' },
18+
];
19+
const draftGrouping = [
20+
{ columnName: 'a' },
21+
{ columnName: 'c' },
22+
];
23+
24+
expect(groupingPanelItems(columns, grouping, draftGrouping))
25+
.toEqual([
26+
{ column: { name: 'a' }, draft: false },
27+
{ column: { name: 'c' }, draft: false },
28+
]);
29+
});
30+
31+
it('should work when draft group added', () => {
32+
const grouping = [
33+
{ columnName: 'a' },
34+
{ columnName: 'c' },
35+
];
36+
const draftGrouping = [
37+
{ columnName: 'a' },
38+
{ columnName: 'c' },
39+
{ columnName: 'd' },
40+
];
41+
42+
expect(groupingPanelItems(columns, grouping, draftGrouping))
43+
.toEqual([
44+
{ column: { name: 'a' }, draft: false },
45+
{ column: { name: 'c' }, draft: false },
46+
{ column: { name: 'd' }, draft: true },
47+
]);
48+
});
49+
50+
it('should work when draft group removed', () => {
51+
const grouping = [
52+
{ columnName: 'a' },
53+
{ columnName: 'c' },
54+
];
55+
const draftGrouping = [
56+
{ columnName: 'a' },
57+
];
2058

21-
expect(processedColumns).toHaveLength(2);
22-
expect(processedColumns[0].column).toBe(columns[0]);
23-
expect(processedColumns[0].draft).toBeUndefined();
24-
expect(processedColumns[1].column).toBe(columns[2]);
25-
expect(processedColumns[1].draft).toBeTruthy();
59+
expect(groupingPanelItems(columns, grouping, draftGrouping))
60+
.toEqual([
61+
{ column: { name: 'a' }, draft: false },
62+
{ column: { name: 'c' }, draft: true },
63+
]);
2664
});
2765
});
2866
});

packages/dx-grid-core/src/plugins/grouping-state/computeds.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

packages/dx-grid-core/src/plugins/grouping-state/computeds.test.js

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
11
export const GROUP_KEY_SEPARATOR = '|';
2-
export const GROUP_ADD_MODE = 'add';
3-
export const GROUP_REMOVE_MODE = 'remove';
4-
export const GROUP_REORDER_MODE = 'reorder';
Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,46 @@
11
import { GROUP_KEY_SEPARATOR } from './constants';
22

3-
export const changeColumnGrouping = (state, { columnName, groupIndex }) => {
4-
const grouping = state.grouping.slice();
5-
const groupingIndex = grouping.findIndex(g => g.columnName === columnName);
3+
const applyColumnGrouping = (grouping, { columnName, groupIndex }) => {
4+
const nextGrouping = grouping.slice();
5+
const groupingIndex = nextGrouping.findIndex(g => g.columnName === columnName);
66
let targetIndex = groupIndex;
77

88
if (groupingIndex > -1) {
9-
grouping.splice(groupingIndex, 1);
9+
nextGrouping.splice(groupingIndex, 1);
1010
} else if (groupIndex === undefined) {
11-
targetIndex = grouping.length;
11+
targetIndex = nextGrouping.length;
1212
}
1313

1414
if (targetIndex > -1) {
15-
grouping.splice(targetIndex, 0, {
15+
nextGrouping.splice(targetIndex, 0, {
1616
columnName,
1717
});
1818
}
1919

20-
const ungroupedColumnIndex = state.grouping.findIndex((group, index) =>
21-
!grouping[index] || group.columnName !== grouping[index].columnName);
20+
return nextGrouping;
21+
};
22+
23+
export const changeColumnGrouping = ({ grouping, expandedGroups }, { columnName, groupIndex }) => {
24+
const nextGrouping = applyColumnGrouping(grouping, { columnName, groupIndex });
25+
26+
const ungroupedColumnIndex = grouping.findIndex((group, index) =>
27+
!nextGrouping[index] || group.columnName !== nextGrouping[index].columnName);
2228
if (ungroupedColumnIndex === -1) {
2329
return {
24-
grouping,
30+
grouping: nextGrouping,
2531
};
2632
}
2733

28-
const filteredExpandedGroups = state.expandedGroups.filter(group =>
34+
const filteredExpandedGroups = expandedGroups.filter(group =>
2935
group.split(GROUP_KEY_SEPARATOR).length <= ungroupedColumnIndex);
30-
if (filteredExpandedGroups.length === state.expandedGroups.length) {
36+
if (filteredExpandedGroups.length === expandedGroups.length) {
3137
return {
32-
grouping,
38+
grouping: nextGrouping,
3339
};
3440
}
3541

3642
return {
37-
grouping,
43+
grouping: nextGrouping,
3844
expandedGroups: filteredExpandedGroups,
3945
};
4046
};
@@ -54,7 +60,10 @@ export const toggleExpandedGroups = (state, { groupKey }) => {
5460
};
5561
};
5662

57-
export const draftGroupingChange = (state, { columnName, groupIndex }) =>
58-
({ groupingChange: { columnName, groupIndex } });
63+
export const draftColumnGrouping = ({ grouping, draftGrouping }, { columnName, groupIndex }) => ({
64+
draftGrouping: applyColumnGrouping(draftGrouping || grouping, { columnName, groupIndex }),
65+
});
5966

60-
export const cancelGroupingChange = () => ({ groupingChange: null });
67+
export const cancelColumnGroupingDraft = () => ({
68+
draftGrouping: null,
69+
});

packages/dx-grid-core/src/plugins/grouping-state/reducers.test.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
changeColumnGrouping,
3-
draftGroupingChange,
4-
cancelGroupingChange,
3+
draftColumnGrouping,
4+
cancelColumnGroupingDraft,
55
toggleExpandedGroups,
66
} from './reducers';
77

@@ -92,30 +92,31 @@ describe('GroupingState reducers', () => {
9292
});
9393
});
9494

95-
describe('#draftGroupingChange', () => {
95+
describe('#draftColumnGrouping', () => {
9696
it('can start grouping change', () => {
9797
const state = {
98-
groupingChange: null,
98+
grouping: [{ columnName: 'column1' }],
99+
draftGrouping: null,
99100
};
100-
const payload = { columnName: 'test', groupIndex: 2 };
101+
const payload = { columnName: 'test', groupIndex: 0 };
101102

102-
expect(draftGroupingChange(state, payload))
103+
expect(draftColumnGrouping(state, payload))
103104
.toEqual({
104-
groupingChange: { columnName: 'test', groupIndex: 2 },
105+
draftGrouping: [{ columnName: 'test' }, { columnName: 'column1' }],
105106
});
106107
});
107108
});
108109

109-
describe('#cancelGroupingChange', () => {
110+
describe('#cancelColumnGroupingDraft', () => {
110111
it('can cancel grouping change', () => {
111112
const state = {
112-
groupingChange: { columnName: 'test', groupIndex: 2 },
113+
draftGrouping: { columnName: 'test', groupIndex: 2 },
113114
};
114115
const payload = null;
115116

116-
expect(cancelGroupingChange(state, payload))
117+
expect(cancelColumnGroupingDraft(state, payload))
117118
.toEqual({
118-
groupingChange: null,
119+
draftGrouping: null,
119120
});
120121
});
121122
});

packages/dx-grid-core/src/plugins/table-group-row/computeds.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
import { TABLE_DATA_TYPE } from '../table/constants';
22
import { TABLE_GROUP_TYPE } from './constants';
3-
import { GROUP_ADD_MODE, GROUP_REMOVE_MODE } from '../grouping-state/constants';
43

5-
const isGroupingChange = columnDraftGrouping => columnDraftGrouping.draft === GROUP_REMOVE_MODE
6-
|| columnDraftGrouping.draft === GROUP_ADD_MODE;
7-
8-
const tableColumnsWithDraftGrouping = (tableColumns, draftGrouping, showColumnWhenGrouped) =>
9-
tableColumns
4+
const tableColumnsWithDraftGrouping =
5+
(tableColumns, grouping, draftGrouping, showColumnWhenGrouped) => tableColumns
106
.reduce((acc, tableColumn) => {
11-
const isDataColumn = tableColumn.type === TABLE_DATA_TYPE;
12-
const tableColumnName = isDataColumn ? tableColumn.column.name : '';
13-
const columnDraftGrouping = draftGrouping
14-
.find(grouping => grouping.columnName === tableColumnName);
7+
if (tableColumn.type !== TABLE_DATA_TYPE) {
8+
acc.push(tableColumn);
9+
return acc;
10+
}
11+
12+
const columnName = tableColumn.column.name;
13+
const columnGroupingExists = grouping
14+
.some(columnGrouping => columnGrouping.columnName === columnName);
15+
const columnDraftGroupingExists = draftGrouping
16+
.some(columnGrouping => columnGrouping.columnName === columnName);
1517

16-
if (!columnDraftGrouping || showColumnWhenGrouped(tableColumnName)) {
17-
return [...acc, tableColumn];
18-
} else if (isGroupingChange(columnDraftGrouping)) {
19-
return [...acc, {
18+
if ((!columnGroupingExists && !columnDraftGroupingExists)
19+
|| showColumnWhenGrouped(columnName)) {
20+
acc.push(tableColumn);
21+
} else if ((!columnGroupingExists && columnDraftGroupingExists)
22+
|| (columnGroupingExists && !columnDraftGroupingExists)) {
23+
acc.push({
2024
...tableColumn,
2125
draft: true,
22-
}];
26+
});
2327
}
2428
return acc;
2529
}, []);
@@ -41,7 +45,7 @@ export const tableColumnsWithGrouping = (
4145
width: indentColumnWidth,
4246
};
4347
}),
44-
...tableColumnsWithDraftGrouping(tableColumns, draftGrouping, showColumnWhenGrouped),
48+
...tableColumnsWithDraftGrouping(tableColumns, grouping, draftGrouping, showColumnWhenGrouped),
4549
];
4650

4751
export const tableRowsWithGrouping = (tableRows, isGroupRow) =>

0 commit comments

Comments
 (0)