Skip to content

fix(table): add missing rowgroup roles #15131

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 1 commit into from
Feb 14, 2019
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
75 changes: 65 additions & 10 deletions src/cdk/table/table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,32 @@ describe('CdkTable', () => {
]);
});

it('should apply correct roles for native table elements', () => {
const thisFixture = createComponent(NativeHtmlTableApp);
const thisTableElement: HTMLTableElement = thisFixture.nativeElement.querySelector('table');
thisFixture.detectChanges();

const rowGroups = Array.from(thisTableElement.querySelectorAll('thead, tbody, tfoot'));
expect(rowGroups.length).toBe(3, 'Expected table to have a thead, tbody, and tfoot');
for (const group of rowGroups) {
expect(group.getAttribute('role'))
.toBe('rowgroup', 'Expected thead, tbody, and tfoot to have role="rowgroup"');
}
});

it('should hide thead/tfoot when there are no header/footer rows', () => {
const thisFixture = createComponent(NativeTableWithNoHeaderOrFooterRows);
const thisTableElement: HTMLTableElement = thisFixture.nativeElement.querySelector('table');
thisFixture.detectChanges();

const rowGroups: HTMLElement[] = Array.from(thisTableElement.querySelectorAll('thead, tfoot'));
expect(rowGroups.length).toBe(2, 'Expected table to have a thead and tfoot');
for (const group of rowGroups) {
expect(group.style.display)
.toBe('none', 'Expected thead and tfoot to be `display: none`');
}
});

it('should render cells even if row data is falsy', () => {
setupTableTestApp(BooleanRowCdkTableApp);
expectTableToMatchContent(tableElement, [
Expand Down Expand Up @@ -1591,27 +1617,27 @@ class MultipleHeaderFooterRowsCdkTableApp {

<ng-container cdkColumnDef="index1Column">
<cdk-header-cell *cdkHeaderCellDef> Column C</cdk-header-cell>
<cdk-cell *cdkCellDef="let row"> index_1_special_row</cdk-cell>
<cdk-cell *cdkCellDef> index_1_special_row</cdk-cell>
</ng-container>

<ng-container cdkColumnDef="c3Column">
<cdk-header-cell *cdkHeaderCellDef> Column C</cdk-header-cell>
<cdk-cell *cdkCellDef="let row"> c3_special_row</cdk-cell>
<cdk-cell *cdkCellDef> c3_special_row</cdk-cell>
</ng-container>

<ng-container cdkColumnDef="index">
<cdk-header-cell *cdkHeaderCellDef> Index</cdk-header-cell>
<cdk-cell *cdkCellDef="let row; let index = index"> {{index}}</cdk-cell>
<cdk-cell *cdkCellDef="let index = index"> {{index}}</cdk-cell>
</ng-container>

<ng-container cdkColumnDef="dataIndex">
<cdk-header-cell *cdkHeaderCellDef> Data Index</cdk-header-cell>
<cdk-cell *cdkCellDef="let row; let dataIndex = dataIndex"> {{dataIndex}}</cdk-cell>
<cdk-cell *cdkCellDef="let dataIndex = dataIndex"> {{dataIndex}}</cdk-cell>
</ng-container>

<ng-container cdkColumnDef="renderIndex">
<cdk-header-cell *cdkHeaderCellDef> Render Index</cdk-header-cell>
<cdk-cell *cdkCellDef="let row; let renderIndex = renderIndex"> {{renderIndex}}</cdk-cell>
<cdk-cell *cdkCellDef="let renderIndex = renderIndex"> {{renderIndex}}</cdk-cell>
</ng-container>

<cdk-header-row *cdkHeaderRowDef="columnsToRender"></cdk-header-row>
Expand Down Expand Up @@ -1662,12 +1688,12 @@ class WhenRowCdkTableApp {

<ng-container cdkColumnDef="index1Column">
<cdk-header-cell *cdkHeaderCellDef> Column C</cdk-header-cell>
<cdk-cell *cdkCellDef="let row"> index_1_special_row </cdk-cell>
<cdk-cell *cdkCellDef> index_1_special_row </cdk-cell>
</ng-container>

<ng-container cdkColumnDef="c3Column">
<cdk-header-cell *cdkHeaderCellDef> Column C</cdk-header-cell>
<cdk-cell *cdkCellDef="let row"> c3_special_row </cdk-cell>
<cdk-cell *cdkCellDef> c3_special_row </cdk-cell>
</ng-container>

<cdk-header-row *cdkHeaderRowDef="columnsToRender"></cdk-header-row>
Expand Down Expand Up @@ -1705,12 +1731,12 @@ class WhenRowWithoutDefaultCdkTableApp {

<ng-container cdkColumnDef="index1Column">
<cdk-header-cell *cdkHeaderCellDef> Column C</cdk-header-cell>
<cdk-cell *cdkCellDef="let row"> index_1_special_row </cdk-cell>
<cdk-cell *cdkCellDef> index_1_special_row </cdk-cell>
</ng-container>

<ng-container cdkColumnDef="c3Column">
<cdk-header-cell *cdkHeaderCellDef> Column C</cdk-header-cell>
<cdk-cell *cdkCellDef="let row"> c3_special_row </cdk-cell>
<cdk-cell *cdkCellDef> c3_special_row </cdk-cell>
</ng-container>

<cdk-header-row *cdkHeaderRowDef="columnsToRender"></cdk-header-row>
Expand Down Expand Up @@ -1790,7 +1816,7 @@ class TrackByCdkTableApp {
[sticky]="isStuck(stickyStartColumns, column)"
[stickyEnd]="isStuck(stickyEndColumns, column)">
<cdk-header-cell *cdkHeaderCellDef> Header {{column}} </cdk-header-cell>
<cdk-cell *cdkCellDef="let row"> {{column}} </cdk-cell>
<cdk-cell *cdkCellDef>{{column}}</cdk-cell>
<cdk-footer-cell *cdkFooterCellDef> Footer {{column}} </cdk-footer-cell>
</ng-container>

Expand Down Expand Up @@ -2226,6 +2252,35 @@ class NativeHtmlTableApp {
@ViewChild(CdkTable) table: CdkTable<TestData>;
}

@Component({
template: `
<table cdk-table [dataSource]="dataSource">
<ng-container cdkColumnDef="column_a">
<th cdk-header-cell *cdkHeaderCellDef> Column A</th>
<td cdk-cell *cdkCellDef="let row"> {{row.a}}</td>
</ng-container>

<ng-container cdkColumnDef="column_b">
<th cdk-header-cell *cdkHeaderCellDef> Column B</th>
<td cdk-cell *cdkCellDef="let row"> {{row.b}}</td>
</ng-container>

<ng-container cdkColumnDef="column_c">
<th cdk-header-cell *cdkHeaderCellDef> Column C</th>
<td cdk-cell *cdkCellDef="let row"> {{row.c}}</td>
</ng-container>

<tr cdk-row *cdkRowDef="let row; columns: columnsToRender" class="customRowClass"></tr>
</table>
`
})
class NativeTableWithNoHeaderOrFooterRows {
dataSource: FakeDataSource | undefined = new FakeDataSource();
columnsToRender = ['column_a', 'column_b', 'column_c'];

@ViewChild(CdkTable) table: CdkTable<TestData>;
}

@Component({
template: `
<table cdk-table [dataSource]="dataSource">
Expand Down
29 changes: 24 additions & 5 deletions src/cdk/table/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,11 +579,20 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
* sticky input changes. May be called manually for cases where the cell content changes outside
* of these events.
*/
updateStickyHeaderRowStyles() {
updateStickyHeaderRowStyles(): void {
const headerRows = this._getRenderedRows(this._headerRowOutlet);
this._stickyStyler.clearStickyPositioning(headerRows, ['top']);
const tableElement = this._elementRef.nativeElement as HTMLElement;

// Hide the thead element if there are no header rows. This is necessary to satisfy
// overzealous a11y checkers that fail because the `rowgroup` element does not contain
// required child `row`.
const thead = tableElement.querySelector('thead');
if (thead) {
thead.style.display = headerRows.length ? '' : 'none';
}

const stickyStates = this._headerRowDefs.map(def => def.sticky);
this._stickyStyler.clearStickyPositioning(headerRows, ['top']);
this._stickyStyler.stickRows(headerRows, stickyStates, 'top');

// Reset the dirty state of the sticky input change since it has been used.
Expand All @@ -597,11 +606,20 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
* sticky input changes. May be called manually for cases where the cell content changes outside
* of these events.
*/
updateStickyFooterRowStyles() {
updateStickyFooterRowStyles(): void {
const footerRows = this._getRenderedRows(this._footerRowOutlet);
this._stickyStyler.clearStickyPositioning(footerRows, ['bottom']);
const tableElement = this._elementRef.nativeElement as HTMLElement;

// Hide the tfoot element if there are no footer rows. This is necessary to satisfy
// overzealous a11y checkers that fail because the `rowgroup` element does not contain
// required child `row`.
const tfoot = tableElement.querySelector('tfoot');
if (tfoot) {
tfoot.style.display = footerRows.length ? '' : 'none';
}

const stickyStates = this._footerRowDefs.map(def => def.sticky);
this._stickyStyler.clearStickyPositioning(footerRows, ['bottom']);
this._stickyStyler.stickRows(footerRows, stickyStates, 'bottom');
this._stickyStyler.updateStickyFooterContainer(this._elementRef.nativeElement, stickyStates);

Expand Down Expand Up @@ -865,7 +883,7 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
}

/** Gets the list of rows that have been rendered in the row outlet. */
_getRenderedRows(rowOutlet: RowOutlet) {
_getRenderedRows(rowOutlet: RowOutlet): HTMLElement[] {
const renderedRows: HTMLElement[] = [];

for (let i = 0; i < rowOutlet.viewContainer.length; i++) {
Expand Down Expand Up @@ -983,6 +1001,7 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes

for (const section of sections) {
const element = documentRef.createElement(section.tag);
element.setAttribute('role', 'rowgroup');
element.appendChild(section.outlet.elementRef.nativeElement);
documentFragment.appendChild(element);
}
Expand Down