Skip to content

Commit 1e900fd

Browse files
committed
feat(data-table): allow custom roles
1 parent aafa6b0 commit 1e900fd

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

src/lib/core/data-table/data-table.spec.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('CdkTable', () => {
2121

2222
TestBed.configureTestingModule({
2323
imports: [CdkDataTableModule],
24-
declarations: [SimpleCdkTableApp],
24+
declarations: [SimpleCdkTableApp, CustomRoleCdkTableApp],
2525
}).compileComponents();
2626

2727
fixture = TestBed.createComponent(SimpleCdkTableApp);
@@ -81,6 +81,12 @@ describe('CdkTable', () => {
8181
expect(row).toBeRole('row');
8282
getCells(row).forEach(cell => expect(cell).toBeRole('gridcell'));
8383
});
84+
85+
// Check that if the component was created with a custom role, that it does not override it.
86+
fixture = TestBed.createComponent(CustomRoleCdkTableApp);
87+
fixture.detectChanges();
88+
89+
expect(fixture.nativeElement.querySelector('cdk-table')).toBeRole('custom-role');
8490
});
8591
});
8692

@@ -255,6 +261,26 @@ class SimpleCdkTableApp {
255261
@ViewChild(CdkTable) table: CdkTable<TestData>;
256262
}
257263

264+
@Component({
265+
template: `
266+
<cdk-table [dataSource]="dataSource" role="custom-role">
267+
<ng-container cdkColumnDef="column_a">
268+
<cdk-header-cell *cdkHeaderCellDef> Column A</cdk-header-cell>
269+
<cdk-cell *cdkCellDef="let row"> {{row.a}}</cdk-cell>
270+
</ng-container>
271+
272+
<cdk-header-row *cdkHeaderRowDef="columnsToRender"></cdk-header-row>
273+
<cdk-row *cdkRowDef="let row; columns: columnsToRender"></cdk-row>
274+
</cdk-table>
275+
`
276+
})
277+
class CustomRoleCdkTableApp {
278+
dataSource: FakeDataSource = new FakeDataSource();
279+
columnsToRender = ['column_a'];
280+
281+
@ViewChild(CdkTable) table: CdkTable<TestData>;
282+
}
283+
258284
function getElements(element: Element, query: string): Element[] {
259285
return [].slice.call(element.querySelectorAll(query));
260286
}

src/lib/core/data-table/data-table.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import {
2+
Attribute,
23
ChangeDetectionStrategy,
34
ChangeDetectorRef,
45
Component,
56
ContentChild,
67
ContentChildren,
78
Directive,
9+
ElementRef,
810
Input,
911
IterableChangeRecord,
1012
IterableDiffer,
1113
IterableDiffers,
1214
NgIterable,
1315
QueryList,
16+
Renderer2,
1417
ViewChild,
1518
ViewContainerRef,
1619
ViewEncapsulation
@@ -54,7 +57,6 @@ export class HeaderRowPlaceholder {
5457
`,
5558
host: {
5659
'class': 'cdk-table',
57-
'role': 'grid' // TODO(andrewseguin): Allow the user to choose either grid or treegrid
5860
},
5961
encapsulation: ViewEncapsulation.None,
6062
changeDetection: ChangeDetectionStrategy.OnPush,
@@ -104,12 +106,19 @@ export class CdkTable<T> implements CollectionViewer {
104106
@ContentChildren(CdkRowDef) _rowDefinitions: QueryList<CdkRowDef>;
105107

106108
constructor(private readonly _differs: IterableDiffers,
107-
private readonly _changeDetectorRef: ChangeDetectorRef) {
109+
private readonly _changeDetectorRef: ChangeDetectorRef,
110+
elementRef: ElementRef,
111+
renderer: Renderer2,
112+
@Attribute('role') role: string) {
108113
// Show the stability warning of the data-table only if it doesn't run inside of jasmine.
109114
// This is just temporary and should reduce warnings when running the tests.
110115
if (!(typeof window !== 'undefined' && window['jasmine'])) {
111116
console.warn('The data table is still in active development ' +
112-
'and should be considered unstable.');
117+
'and should be considered unstable.');
118+
}
119+
120+
if (!role) {
121+
renderer.setAttribute(elementRef.nativeElement, 'role', 'grid');
113122
}
114123

115124
// TODO(andrewseguin): Add trackby function input.

src/lib/core/testing/jasmine-matchers.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ export const customMatchers: jasmine.CustomMatcherFactories = {
77
return {
88
compare: function (element: Element, expectedRole: string) {
99
const result: jasmine.CustomMatcherResult = {pass: false};
10-
result.pass = element.getAttribute('role') === expectedRole;
10+
const actualRole = element.getAttribute('role');
11+
12+
result.pass = actualRole === expectedRole;
1113
result.message = `Expected role for ${element.tagName} to be ${expectedRole}`;
1214

1315
if (!result.pass) {
14-
result.message += ` but was ${expectedRole}`;
16+
result.message += ` but was ${actualRole}`;
1517
}
1618

1719
return result;

0 commit comments

Comments
 (0)