Skip to content

Commit 254fb49

Browse files
crisbetommalerba
authored andcommitted
fix(table): better error message if text column is missing a name
Currently if the consumer forgot to set a name on a text column, we throw a cryptic error somewhere down the line. These changes add a proper error message.
1 parent 92448bc commit 254fb49

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

src/cdk/table/table-errors.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,11 @@ export function getTableUnknownDataSourceError() {
6464
export function getTableTextColumnMissingParentTableError() {
6565
return Error(`Text column could not find a parent table for registration.`);
6666
}
67+
68+
/**
69+
* Returns an error to be thrown when a table text column doesn't have a name.
70+
* @docs-private
71+
*/
72+
export function getTableTextColumnMissingNameError() {
73+
return Error(`Table text column must have a name.`);
74+
}

src/cdk/table/text-column.spec.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import {Component} from '@angular/core';
22
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
33

4-
import {getTableTextColumnMissingParentTableError} from './table-errors';
4+
import {
5+
getTableTextColumnMissingParentTableError,
6+
getTableTextColumnMissingNameError,
7+
} from './table-errors';
58
import {CdkTableModule} from './table-module';
69
import {expectTableToMatchContent} from './table.spec';
710
import {TEXT_COLUMN_OPTIONS, TextColumnOptions} from './text-column';
@@ -19,6 +22,7 @@ describe('CdkTextColumn', () => {
1922
declarations: [
2023
BasicTextColumnApp,
2124
MissingTableApp,
25+
TextColumnWithoutNameApp,
2226
],
2327
})
2428
.compileComponents();
@@ -45,6 +49,11 @@ describe('CdkTextColumn', () => {
4549
.toThrowError(getTableTextColumnMissingParentTableError().message);
4650
});
4751

52+
it('should throw an error if the text column does not have a name', () => {
53+
expect(() => TestBed.createComponent(TextColumnWithoutNameApp).detectChanges())
54+
.toThrowError(getTableTextColumnMissingNameError().message);
55+
});
56+
4857
it('should allow for alternate header text', () => {
4958
component.headerTextB = 'column-b';
5059
fixture.detectChanges();
@@ -182,3 +191,18 @@ class BasicTextColumnApp {
182191
})
183192
class MissingTableApp {
184193
}
194+
195+
196+
@Component({
197+
template: `
198+
<cdk-table [dataSource]="data">
199+
<cdk-text-column [dataAccessor]="dataAccessorA"></cdk-text-column>
200+
201+
<cdk-header-row *cdkHeaderRowDef="displayedColumns"></cdk-header-row>
202+
<cdk-row *cdkRowDef="let row; columns: displayedColumns"></cdk-row>
203+
</cdk-table>
204+
`
205+
})
206+
class TextColumnWithoutNameApp extends BasicTextColumnApp {
207+
}
208+

src/cdk/table/text-column.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ import {
1616
OnInit,
1717
Optional,
1818
ViewChild,
19-
ViewEncapsulation
19+
ViewEncapsulation,
20+
isDevMode,
2021
} from '@angular/core';
2122
import {CdkCellDef, CdkColumnDef, CdkHeaderCellDef} from './cell';
2223
import {CdkTable} from './table';
23-
import {getTableTextColumnMissingParentTableError} from './table-errors';
24+
import {
25+
getTableTextColumnMissingParentTableError,
26+
getTableTextColumnMissingNameError,
27+
} from './table-errors';
2428

2529

2630
/** Configurable options for `CdkTextColumn`. */
@@ -164,11 +168,17 @@ export class CdkTextColumn<T> implements OnDestroy, OnInit {
164168
* has been provided. Otherwise simply capitalize the column name.
165169
*/
166170
_createDefaultHeaderText() {
171+
const name = this.name;
172+
173+
if (isDevMode() && !name) {
174+
throw getTableTextColumnMissingNameError();
175+
}
176+
167177
if (this._options && this._options.defaultHeaderTextTransform) {
168-
return this._options.defaultHeaderTextTransform(this.name);
178+
return this._options.defaultHeaderTextTransform(name);
169179
}
170180

171-
return this.name[0].toUpperCase() + this.name.slice(1);
181+
return name[0].toUpperCase() + name.slice(1);
172182
}
173183

174184
/** Synchronizes the column definition name with the text column name. */

0 commit comments

Comments
 (0)