Skip to content

Commit 7acdf10

Browse files
authored
fix: clear details-opened attribute when item is removed (#225) (CP 6.0)
2 parents 226b5eb + c51af02 commit 7acdf10

File tree

2 files changed

+77
-17
lines changed

2 files changed

+77
-17
lines changed

src/vaadin-grid.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,7 @@ class GridElement extends ElementMixin(
807807
this._a11yUpdateRowSelected(row, model.selected);
808808
this._a11yUpdateRowLevel(row, model.level);
809809
this._toggleAttribute('expanded', model.expanded, row);
810+
this._toggleAttribute('details-opened', this._isDetailsOpened(item), row);
810811
if (this._rowDetailsTemplate || this.rowDetailsRenderer) {
811812
this._toggleDetailsCell(row, item);
812813
}

test/row-details.test.js

Lines changed: 76 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { expect } from '@esm-bundle/chai';
22
import sinon from 'sinon';
33
import { fixtureSync, nextFrame } from '@open-wc/testing-helpers';
4-
import { PolymerElement, html } from '@polymer/polymer/polymer-element.js';
4+
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
55
import '@polymer/polymer/lib/elements/dom-repeat.js';
66
import {
7+
buildDataSet,
78
click,
89
flushGrid,
910
getBodyCellContent,
1011
getCellContent,
11-
getRows,
1212
getRowCells,
13+
getRows,
1314
infiniteDataProvider,
1415
scrollToEnd
1516
} from './helpers.js';
@@ -49,6 +50,14 @@ describe('row details', () => {
4950
let grid;
5051
let bodyRows;
5152

53+
function openRowDetails(index) {
54+
grid.openItemDetails(grid._cache.items[index]);
55+
}
56+
57+
function closeRowDetails(index) {
58+
grid.closeItemDetails(grid._cache.items[index]);
59+
}
60+
5261
it('should not increase row update count', () => {
5362
grid = fixtureSync(`
5463
<vaadin-grid style="width: 50px; height: 400px" size="100">
@@ -66,10 +75,6 @@ describe('row details', () => {
6675
});
6776

6877
describe('simple', () => {
69-
function openRowDetails(index) {
70-
grid.openItemDetails(grid._cache.items[index]);
71-
}
72-
7378
beforeEach(async () => {
7479
grid = fixtureSync(`
7580
<vaadin-grid style="width: 50px; height: 400px" size="100">
@@ -85,10 +90,6 @@ describe('row details', () => {
8590
await nextFrame();
8691
});
8792

88-
function closeRowDetails(index) {
89-
grid.closeItemDetails(grid._cache.items[index]);
90-
}
91-
9293
it('should not activate on click', () => {
9394
openRowDetails(0);
9495
const detailsCellContent = getBodyCellContent(grid, 0, 1);
@@ -157,13 +158,6 @@ describe('row details', () => {
157158
assertDetailsBounds();
158159
});
159160

160-
it('should have state attribute', () => {
161-
openRowDetails(1);
162-
expect(bodyRows[1].hasAttribute('details-opened')).to.be.true;
163-
closeRowDetails(1);
164-
expect(bodyRows[1].hasAttribute('details-opened')).to.be.false;
165-
});
166-
167161
it('should have correct bounds when modified after opening', async () => {
168162
openRowDetails(1);
169163
const cells = getRowCells(bodyRows[1]);
@@ -297,4 +291,69 @@ describe('row details', () => {
297291
expect(row.offsetHeight).to.be.above(70);
298292
});
299293
});
294+
295+
describe('details opened attribute', () => {
296+
let dataset = [];
297+
const dataProvider = (params, callback) => callback(dataset);
298+
299+
const countRowsMarkedAsDetailsOpened = (grid) => {
300+
return grid.$.items.querySelectorAll('tr[details-opened]').length;
301+
};
302+
303+
beforeEach(async () => {
304+
dataset = buildDataSet(10);
305+
grid = fixtureSync(`
306+
<vaadin-grid style="width: 50px; height: 400px" size="100">
307+
<template class="row-details"><span>[[index]]</span>-details</template>
308+
<vaadin-grid-column>
309+
<template>[[index]]</template>
310+
</vaadin-grid-column>
311+
</vaadin-grid>
312+
`);
313+
grid.dataProvider = dataProvider;
314+
flushGrid(grid);
315+
bodyRows = getRows(grid.$.items);
316+
await nextFrame();
317+
});
318+
319+
it('should update when opening/closing imperatively', () => {
320+
openRowDetails(1);
321+
expect(bodyRows[1].hasAttribute('details-opened')).to.be.true;
322+
expect(countRowsMarkedAsDetailsOpened(grid)).to.equal(1);
323+
closeRowDetails(1);
324+
expect(bodyRows[1].hasAttribute('details-opened')).to.be.false;
325+
expect(countRowsMarkedAsDetailsOpened(grid)).to.equal(0);
326+
});
327+
328+
it('should be removed when item is removed', () => {
329+
openRowDetails(0);
330+
dataset.shift(); // remove opened item
331+
grid.clearCache();
332+
333+
expect(bodyRows[0].hasAttribute('details-opened')).to.be.false;
334+
expect(countRowsMarkedAsDetailsOpened(grid)).to.equal(0);
335+
});
336+
337+
it('should be removed when items are replaced', () => {
338+
openRowDetails(0);
339+
dataset = buildDataSet(10); // replace data
340+
grid.clearCache();
341+
342+
expect(bodyRows[0].hasAttribute('details-opened')).to.be.false;
343+
expect(countRowsMarkedAsDetailsOpened(grid)).to.equal(0);
344+
});
345+
346+
it('should be removed on all rows when items are replaced', () => {
347+
// Open all rows
348+
dataset.forEach((_, i) => {
349+
openRowDetails(i);
350+
});
351+
expect(countRowsMarkedAsDetailsOpened(grid)).to.equal(dataset.length);
352+
353+
dataset = buildDataSet(10); // replace data
354+
grid.clearCache();
355+
356+
expect(countRowsMarkedAsDetailsOpened(grid)).to.equal(0);
357+
});
358+
});
300359
});

0 commit comments

Comments
 (0)