Skip to content

Commit b6d8193

Browse files
authored
fix(material/table): style no data row properly (#31895)
The "no data" row in the table goes through a different creation flow than the regular table rows which means that it can't get its CSS classes and ends up being unstyled. These changes add some logic to apply the appropriate classes. Fixes #22349.
1 parent 977f46f commit b6d8193

File tree

7 files changed

+26
-7
lines changed

7 files changed

+26
-7
lines changed

goldens/cdk/table/index.api.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,11 @@ export class CdkHeaderRowDef extends BaseRowDef implements CanStick, OnChanges {
249249
export class CdkNoDataRow {
250250
constructor(...args: unknown[]);
251251
// (undocumented)
252-
_contentClassName: string;
252+
_cellClassNames: string[];
253+
// (undocumented)
254+
_cellSelector: string;
255+
// (undocumented)
256+
_contentClassNames: string[];
253257
// (undocumented)
254258
templateRef: TemplateRef<any>;
255259
// (undocumented)

goldens/material/table/index.api.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,9 @@ export class MatHeaderRowDef extends CdkHeaderRowDef {
130130

131131
// @public
132132
export class MatNoDataRow extends CdkNoDataRow {
133+
constructor();
133134
// (undocumented)
134-
_contentClassName: string;
135+
_cellSelector: string;
135136
// (undocumented)
136137
static ɵdir: i0.ɵɵDirectiveDeclaration<MatNoDataRow, "ng-template[matNoDataRow]", never, {}, {}, never, never, true, never>;
137138
// (undocumented)

src/cdk/table/row.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,9 @@ export class CdkRow {}
366366
export class CdkNoDataRow {
367367
templateRef = inject<TemplateRef<any>>(TemplateRef);
368368

369-
_contentClassName = 'cdk-no-data-row';
369+
_contentClassNames = ['cdk-no-data-row', 'cdk-row'];
370+
_cellClassNames = ['cdk-cell', 'cdk-no-data-cell'];
371+
_cellSelector = 'td, cdk-cell, [cdk-cell], .cdk-cell';
370372

371373
constructor(...args: unknown[]);
372374
constructor() {}

src/cdk/table/table.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1419,7 +1419,13 @@ export class CdkTable<T>
14191419
// to figure out which one to add it to when there are multiple.
14201420
if (view.rootNodes.length === 1 && rootNode?.nodeType === this._document.ELEMENT_NODE) {
14211421
rootNode.setAttribute('role', 'row');
1422-
rootNode.classList.add(noDataRow._contentClassName);
1422+
rootNode.classList.add(...noDataRow._contentClassNames);
1423+
1424+
const cells = rootNode.querySelectorAll(noDataRow._cellSelector);
1425+
1426+
for (let i = 0; i < cells.length; i++) {
1427+
cells[i].classList.add(...noDataRow._cellClassNames);
1428+
}
14231429
}
14241430
} else {
14251431
container.clear();

src/material/table/row.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,11 @@ export class MatRow extends CdkRow {}
130130
providers: [{provide: CdkNoDataRow, useExisting: MatNoDataRow}],
131131
})
132132
export class MatNoDataRow extends CdkNoDataRow {
133-
override _contentClassName = 'mat-mdc-no-data-row';
133+
override _cellSelector = 'td, mat-cell, [mat-cell], .mat-cell';
134+
135+
constructor() {
136+
super();
137+
this._contentClassNames.push('mat-mdc-no-data-row', 'mat-mdc-row', 'mdc-data-table__row');
138+
this._cellClassNames.push('mat-mdc-cell', 'mdc-data-table__cell', 'mat-no-data-cell');
139+
}
134140
}

src/material/table/testing/cell-harness.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export abstract class _MatCellHarnessBase extends ContentContainerComponentHarne
5656
/** Harness for interacting with an Angular Material table cell. */
5757
export class MatCellHarness extends _MatCellHarnessBase {
5858
/** The selector for the host element of a `MatCellHarness` instance. */
59-
static hostSelector = '.mat-mdc-cell';
59+
static hostSelector = '.mat-mdc-cell:not(.mat-no-data-cell)';
6060

6161
/**
6262
* Gets a `HarnessPredicate` that can be used to search for a table cell with specific attributes.

src/material/table/testing/row-harness.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export abstract class _MatRowHarnessBase<
6161
/** Harness for interacting with an Angular Material table row. */
6262
export class MatRowHarness extends _MatRowHarnessBase<typeof MatCellHarness, MatCellHarness> {
6363
/** The selector for the host element of a `MatRowHarness` instance. */
64-
static hostSelector = '.mat-mdc-row';
64+
static hostSelector = '.mat-mdc-row:not(.mat-mdc-no-data-row)';
6565
protected _cellHarness = MatCellHarness;
6666

6767
/**

0 commit comments

Comments
 (0)