Skip to content

Commit 3f669d3

Browse files
authored
fix: adjust aria-rowcount and aria-colcount based on grid state (#9872)
1 parent 9aa641c commit 3f669d3

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

packages/grid/src/vaadin-grid-a11y-mixin.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const A11yMixin = (superClass) =>
2222
};
2323
}
2424
static get observers() {
25-
return ['_a11yUpdateGridSize(size, _columnTree)'];
25+
return ['_a11yUpdateGridSize(size, _columnTree, __emptyState)'];
2626
}
2727

2828
/** @private */
@@ -34,21 +34,27 @@ export const A11yMixin = (superClass) =>
3434

3535
/** @private */
3636
_a11yGetFooterRowCount(_columnTree) {
37-
return _columnTree.filter((level) => level.some((col) => col.headerRenderer)).length;
37+
return _columnTree.filter((level) => level.some((col) => col.footerRenderer)).length;
3838
}
3939

4040
/** @private */
41-
_a11yUpdateGridSize(size, _columnTree) {
41+
_a11yUpdateGridSize(size, _columnTree, emptyState) {
4242
if (size === undefined || _columnTree === undefined) {
4343
return;
4444
}
4545

46+
const headerRowsCount = this._a11yGetHeaderRowCount(_columnTree);
47+
const footerRowsCount = this._a11yGetFooterRowCount(_columnTree);
48+
const bodyRowsCount = emptyState ? 1 : size;
49+
const rowsCount = bodyRowsCount + headerRowsCount + footerRowsCount;
50+
51+
this.$.table.setAttribute('aria-rowcount', rowsCount);
52+
4653
const bodyColumns = _columnTree[_columnTree.length - 1];
47-
this.$.table.setAttribute(
48-
'aria-rowcount',
49-
size + this._a11yGetHeaderRowCount(_columnTree) + this._a11yGetFooterRowCount(_columnTree),
50-
);
51-
this.$.table.setAttribute('aria-colcount', (bodyColumns && bodyColumns.length) || 0);
54+
// If no header and footer rows while the empty state is active, count as one column
55+
// Otherwise, use the number of body columns, if present
56+
const columnsCount = emptyState ? 1 : (rowsCount && bodyColumns && bodyColumns.length) || 0;
57+
this.$.table.setAttribute('aria-colcount', columnsCount);
5258

5359
this._a11yUpdateHeaderRows();
5460
this._a11yUpdateFooterRows();

packages/grid/src/vaadin-grid-mixin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,7 @@ export const GridMixin = (superClass) =>
630630

631631
// Make sure the section has a tabbable element
632632
this._resetKeyboardNavigation();
633+
this._a11yUpdateGridSize(this.size, this._columnTree, this.__emptyState);
633634
}
634635

635636
/** @private */

packages/grid/test/accessibility.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,19 @@ describe('accessibility', () => {
240240
await nextFrame();
241241
expect(grid.$.table.getAttribute('aria-colcount')).to.equal('4');
242242
});
243+
244+
it('should update aria-colcount when grid is empty with empty-state and no header/footer rendered', async () => {
245+
const emptyStateText = document.createElement('span');
246+
emptyStateText.setAttribute('slot', 'empty-state');
247+
grid.appendChild(emptyStateText);
248+
grid.querySelectorAll('vaadin-grid-column').forEach((col) => {
249+
col.headerRenderer = null;
250+
col.footerRenderer = null;
251+
});
252+
grid.items = [];
253+
await nextFrame();
254+
expect(grid.$.table.getAttribute('aria-colcount')).to.equal('1');
255+
});
243256
});
244257

245258
describe('row details in use', () => {
@@ -314,6 +327,26 @@ describe('accessibility', () => {
314327
// 10 item rows + header row + footer row = 12 in total
315328
expect(grid.$.table.getAttribute('aria-rowcount')).to.equal('12');
316329
});
330+
331+
it('should count the footer rows correctly as part of the aria-rowcount', async () => {
332+
// Remove the header renderers
333+
grid.querySelectorAll('vaadin-grid-column').forEach((col) => {
334+
col.headerRenderer = null;
335+
});
336+
337+
await nextFrame();
338+
expect(grid.$.table.getAttribute('aria-rowcount')).to.equal('3');
339+
});
340+
341+
it('should count the header rows correctly as part of the aria-rowcount', async () => {
342+
// Remove the footer renderers
343+
grid.querySelectorAll('vaadin-grid-column').forEach((col) => {
344+
col.footerRenderer = null;
345+
});
346+
347+
await nextFrame();
348+
expect(grid.$.table.getAttribute('aria-rowcount')).to.equal('3');
349+
});
317350
});
318351

319352
describe('group', () => {

0 commit comments

Comments
 (0)