Skip to content

fix(cdk/table): set default role of table to 'table' #22491

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

Merged
merged 2 commits into from
Jul 12, 2021
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
18 changes: 14 additions & 4 deletions src/cdk/table/cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import {
ContentChild,
Directive,
ElementRef,
Input,
TemplateRef,
Inject,
Input,
Optional,
TemplateRef,
} from '@angular/core';
import {CanStick, CanStickCtor, mixinHasStickyInput} from './can-stick';
import {CDK_TABLE} from './tokens';
Expand Down Expand Up @@ -178,12 +178,17 @@ export class CdkHeaderCell extends BaseCdkCell {
selector: 'cdk-footer-cell, td[cdk-footer-cell]',
host: {
'class': 'cdk-footer-cell',
'role': 'gridcell',
},
})
export class CdkFooterCell extends BaseCdkCell {
constructor(columnDef: CdkColumnDef, elementRef: ElementRef) {
super(columnDef, elementRef);
if (columnDef._table?._elementRef.nativeElement.nodeType === 1) {
const tableRole = columnDef._table._elementRef.nativeElement
.getAttribute('role');
const role = (tableRole === 'grid' || tableRole === 'treegrid') ? 'gridcell' : 'cell';
elementRef.nativeElement.setAttribute('role', role);
}
}
}

Expand All @@ -192,11 +197,16 @@ export class CdkFooterCell extends BaseCdkCell {
selector: 'cdk-cell, td[cdk-cell]',
host: {
'class': 'cdk-cell',
'role': 'gridcell',
},
})
export class CdkCell extends BaseCdkCell {
constructor(columnDef: CdkColumnDef, elementRef: ElementRef) {
super(columnDef, elementRef);
if (columnDef._table?._elementRef.nativeElement.nodeType === 1) {
const tableRole = columnDef._table._elementRef.nativeElement
.getAttribute('role');
const role = (tableRole === 'grid' || tableRole === 'treegrid') ? 'gridcell' : 'cell';
elementRef.nativeElement.setAttribute('role', role);
}
}
}
17 changes: 15 additions & 2 deletions src/cdk/table/table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('CdkTable', () => {
});

it('with the right accessibility roles', () => {
expect(tableElement.getAttribute('role')).toBe('grid');
expect(tableElement.getAttribute('role')).toBe('table');

expect(getHeaderRows(tableElement)[0].getAttribute('role')).toBe('row');
const header = getHeaderRows(tableElement)[0];
Expand All @@ -118,7 +118,7 @@ describe('CdkTable', () => {
getRows(tableElement).forEach(row => {
expect(row.getAttribute('role')).toBe('row');
getCells(row).forEach(cell => {
expect(cell.getAttribute('role')).toBe('gridcell');
expect(cell.getAttribute('role')).toBe('cell');
});
});
});
Expand Down Expand Up @@ -611,6 +611,19 @@ describe('CdkTable', () => {
it('should not clobber an existing table role', () => {
setupTableTestApp(CustomRoleCdkTableApp);
expect(tableElement.getAttribute('role')).toBe('treegrid');

expect(getHeaderRows(tableElement)[0].getAttribute('role')).toBe('row');
const header = getHeaderRows(tableElement)[0];
getHeaderCells(header).forEach(cell => {
expect(cell.getAttribute('role')).toBe('columnheader');
});

getRows(tableElement).forEach(row => {
expect(row.getAttribute('role')).toBe('row');
getCells(row).forEach(cell => {
expect(cell.getAttribute('role')).toBe('gridcell');
});
});
});

it('should throw an error if two column definitions have the same name', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/cdk/table/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
@Optional() @SkipSelf() @Inject(STICKY_POSITIONING_LISTENER)
protected readonly _stickyPositioningListener: StickyPositioningListener) {
if (!role) {
this._elementRef.nativeElement.setAttribute('role', 'grid');
this._elementRef.nativeElement.setAttribute('role', 'table');
}

this._document = _document;
Expand Down
2 changes: 0 additions & 2 deletions src/material-experimental/mdc-table/cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ export class MatHeaderCell extends CdkHeaderCell {}
selector: 'mat-footer-cell, td[mat-footer-cell]',
host: {
'class': 'mat-mdc-footer-cell mdc-data-table__cell',
'role': 'gridcell',
},
})
export class MatFooterCell extends CdkFooterCell {}
Expand All @@ -103,7 +102,6 @@ export class MatFooterCell extends CdkFooterCell {}
selector: 'mat-cell, td[mat-cell]',
host: {
'class': 'mat-mdc-cell mdc-data-table__cell',
'role': 'gridcell',
},
})
export class MatCell extends CdkCell {}