Skip to content

Commit 6b15346

Browse files
authored
fix: prevent overriding column hidden state when attaching column group (CP: 14.8) (#2216)
* fix: prevent overriding column hidden state when attaching column group (#3805) (CP: 14.8) Fixes the column group to not synchronize its hidden state to child columns when it is attached, and visible. Fixes vaadin/flow-components#2959 Cherry-pick of: vaadin/web-components#3805 * remove obsolete hidden check
1 parent e205db7 commit 6b15346

File tree

3 files changed

+56
-45
lines changed

3 files changed

+56
-45
lines changed

src/vaadin-grid-column-group.html

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,8 @@
8585

8686
static get observers() {
8787
return [
88-
'_updateVisibleChildColumns(_childColumns)',
89-
'_childColumnsChanged(_childColumns)',
9088
'_groupFrozenChanged(frozen, _rootColumns)',
91-
'_groupHiddenChanged(hidden, _rootColumns)',
92-
'_visibleChildColumnsChanged(_visibleChildColumns)',
89+
'_groupHiddenChanged(hidden)',
9390
'_colSpanChanged(_colSpan, _headerCell, _footerCell)',
9491
'_groupOrderChanged(_order, _rootColumns)',
9592
'_groupReorderStatusChanged(_reorderStatus, _rootColumns)',
@@ -117,9 +114,12 @@
117114
*/
118115
_columnPropChanged(path, value) {
119116
if (path === 'hidden') {
120-
this._preventHiddenCascade = true;
117+
// Prevent synchronization of the hidden state to child columns.
118+
// If the group is currently auto-hidden, and one column is made visible,
119+
// we don't want the other columns to become visible as well.
120+
this._preventHiddenSynchronization = true;
121121
this._updateVisibleChildColumns(this._childColumns);
122-
this._preventHiddenCascade = false;
122+
this._preventHiddenSynchronization = false;
123123
}
124124

125125
if (/flexGrow|width|hidden|_childColumns/.test(path)) {
@@ -191,15 +191,9 @@
191191

192192
/** @private */
193193
_updateVisibleChildColumns(childColumns) {
194-
this._visibleChildColumns = Array.prototype.filter.call(childColumns, col => !col.hidden);
195-
}
196-
197-
/** @private */
198-
_childColumnsChanged(childColumns) {
199-
if (!this._autoHidden && this.hidden) {
200-
Array.prototype.forEach.call(childColumns, column => column.hidden = true);
201-
this._updateVisibleChildColumns(childColumns);
202-
}
194+
this._visibleChildColumns = Array.prototype.filter.call(childColumns, (col) => !col.hidden);
195+
this._colSpan = this._visibleChildColumns.length;
196+
this._updateAutoHidden();
203197
}
204198

205199
/** @protected */
@@ -233,26 +227,31 @@
233227
}
234228

235229
/** @private */
236-
_groupHiddenChanged(hidden, rootColumns) {
237-
if (rootColumns && !this._preventHiddenCascade) {
238-
this._ignoreVisibleChildColumns = true;
239-
rootColumns.forEach(column => column.hidden = hidden);
240-
this._ignoreVisibleChildColumns = false;
230+
_groupHiddenChanged(hidden) {
231+
// When initializing the hidden property, only sync hidden state to columns
232+
// if group is actually hidden. Otherwise, we could override a hidden column
233+
// to be visible.
234+
// We always want to run this though if the property is actually changed.
235+
if (hidden || this.__groupHiddenInitialized) {
236+
this._synchronizeHidden();
241237
}
238+
this.__groupHiddenInitialized = true;
239+
}
242240

243-
this._columnPropChanged('hidden');
241+
/** @private */
242+
_updateAutoHidden() {
243+
const wasAutoHidden = this._autoHidden;
244+
this._autoHidden = (this._visibleChildColumns || []).length === 0;
245+
// Only modify hidden state if group was auto-hidden, or becomes auto-hidden
246+
if (wasAutoHidden || this._autoHidden) {
247+
this.hidden = this._autoHidden;
248+
}
244249
}
245250

246251
/** @private */
247-
_visibleChildColumnsChanged(visibleChildColumns) {
248-
this._colSpan = visibleChildColumns.length;
249-
250-
if (!this._ignoreVisibleChildColumns) {
251-
if (visibleChildColumns.length === 0) {
252-
this._autoHidden = this.hidden = true;
253-
} else if (this.hidden && this._autoHidden) {
254-
this._autoHidden = this.hidden = false;
255-
}
252+
_synchronizeHidden() {
253+
if (this._childColumns && !this._preventHiddenSynchronization) {
254+
this._childColumns.forEach((column) => (column.hidden = this.hidden));
256255
}
257256
}
258257

@@ -283,10 +282,14 @@
283282
if (info.addedNodes.filter(this._isColumnElement).length > 0 ||
284283
info.removedNodes.filter(this._isColumnElement).length > 0) {
285284

286-
this._preventHiddenCascade = true;
285+
// Prevent synchronization of the hidden state to child columns.
286+
// If the group is currently auto-hidden, and a visible column is added,
287+
// we don't want the other columns to become visible as well.
288+
this._preventHiddenSynchronization = true;
287289
this._rootColumns = this._getChildColumns(this);
288290
this._childColumns = this._rootColumns;
289-
this._preventHiddenCascade = false;
291+
this._updateVisibleChildColumns(this._childColumns);
292+
this._preventHiddenSynchronization = false;
290293

291294
// Update the column tree with microtask timing to avoid shady style scope issues
292295
Polymer.Async.microTask.run(() => {

src/vaadin-grid-column.html

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,6 @@
313313

314314
/** @private */
315315
__setColumnTemplateOrRenderer(template, renderer, cells) {
316-
// no renderer or template needed in a hidden column
317-
if (this.hidden) {
318-
return;
319-
}
320316
if (template && renderer) {
321317
throw new Error('You should only use either a renderer or a template');
322318
}

test/column-group.html

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,21 +99,21 @@
9999
expect(columns[1].frozen).to.be.true;
100100
});
101101

102-
it('should hide group column', () => {
102+
it('should hide when all columns are hidden', () => {
103103
columns[0].hidden = true;
104104
columns[1].hidden = true;
105105

106106
expect(group.hidden).to.be.true;
107107
});
108108

109-
it('should unhide group column', () => {
109+
it('should unhide when making child column visible', () => {
110110
group.hidden = true;
111111
columns[0].hidden = false;
112112

113113
expect(group.hidden).to.be.false;
114114
});
115115

116-
it('should not unhide other columns', () => {
116+
it('should not unhide other columns when making a column visible', () => {
117117
group.hidden = true;
118118
columns[0].hidden = false;
119119

@@ -127,18 +127,15 @@
127127
expect(columns[0].hidden).to.be.true;
128128
expect(columns[1].hidden).to.be.true;
129129
expect(group.hidden).to.be.true;
130-
});
131130

132-
it('should propagate hidden to child columns 2', () => {
133-
group.hidden = true;
134131
group.hidden = false;
135132

136133
expect(columns[0].hidden).to.be.false;
137134
expect(columns[1].hidden).to.be.false;
138135
expect(group.hidden).to.be.false;
139136
});
140137

141-
it('should hide the group', () => {
138+
it('should hide when removing all child columns', () => {
142139
group.removeChild(columns[0]);
143140
group.removeChild(columns[1]);
144141
Polymer.flush();
@@ -147,7 +144,7 @@
147144
expect(group.hidden).to.be.true;
148145
});
149146

150-
it('should unhide the group', () => {
147+
it('should unhide when adding a visible column', () => {
151148
group.removeChild(columns[0]);
152149
group.removeChild(columns[1]);
153150
group._observer.flush();
@@ -159,7 +156,7 @@
159156
expect(group.hidden).to.be.false;
160157
});
161158

162-
it('should not unhide the group', () => {
159+
it('should not unhide when adding a hidden column', () => {
163160
group.removeChild(columns[0]);
164161
group.removeChild(columns[1]);
165162
group._observer.flush();
@@ -171,6 +168,21 @@
171168
expect(group.hidden).to.be.true;
172169
});
173170

171+
// Regression test for https://github.com/vaadin/flow-components/issues/2959
172+
it('should not unhide columns when attached to DOM', () => {
173+
const group = document.createElement('vaadin-grid-column-group');
174+
const visibleColumn = document.createElement('vaadin-grid-column');
175+
const hiddenColumn = document.createElement('vaadin-grid-column');
176+
hiddenColumn.hidden = true;
177+
178+
group.appendChild(visibleColumn);
179+
group.appendChild(hiddenColumn);
180+
document.body.appendChild(group);
181+
group._observer.flush();
182+
183+
expect(hiddenColumn.hidden).to.be.true;
184+
});
185+
174186
it('should calculate column group width after hiding a column', () => {
175187
columns[0].hidden = true;
176188

0 commit comments

Comments
 (0)