Skip to content

Commit 9213ef7

Browse files
committed
minor review changes
1 parent d9d4179 commit 9213ef7

File tree

3 files changed

+42
-14
lines changed

3 files changed

+42
-14
lines changed

src/cdk/table/table-errors.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,11 @@ export function getTableMissingRowDefsError() {
5656
export function getTableUnknownDataSourceError() {
5757
return Error(`Provided data source did not match an array, Observable, or DataSource`);
5858
}
59+
60+
/**
61+
* Returns an error to be thrown when the text column cannot find a parent table to inject.
62+
* @docs-private
63+
*/
64+
export function getTableTextColumnMissingParentTableError() {
65+
return Error(`Text column could not find a parent table for registration.`);
66+
}

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {async, ComponentFixture, TestBed} from '@angular/core/testing';
33
import {CdkTableModule} from './table-module';
44
import {expectTableToMatchContent} from './table.spec';
55
import {TextColumnOptions, TEXT_COLUMN_OPTIONS} from './text-column';
6+
import {getTableTextColumnMissingParentTableError} from './table-errors';
67

78
describe('CdkTextColumn', () => {
89
let fixture: ComponentFixture<BasicTextColumnApp>;
@@ -14,6 +15,7 @@ describe('CdkTextColumn', () => {
1415
imports: [CdkTableModule],
1516
declarations: [
1617
BasicTextColumnApp,
18+
MissingTableApp,
1719
],
1820
}).compileComponents();
1921
}));
@@ -34,6 +36,12 @@ describe('CdkTextColumn', () => {
3436
]);
3537
});
3638

39+
it('should throw an error if the text column is not in the content of a table', () => {
40+
expect(() => TestBed.createComponent(MissingTableApp).detectChanges())
41+
.toThrowError(getTableTextColumnMissingParentTableError().message);
42+
43+
});
44+
3745
it('should allow for alternate header text', () => {
3846
component.headerTextB = 'column-b';
3947
fixture.detectChanges();
@@ -97,7 +105,7 @@ describe('CdkTextColumn', () => {
97105
});
98106

99107
describe('with options', () => {
100-
function createTestComponent(options: TextColumnOptions) {
108+
function createTestComponent(options: TextColumnOptions<any>) {
101109
// Reset the previously configured testing module to be able set new providers.
102110
// The testing module has been initialized in the root describe group for the ripples.
103111
TestBed.resetTestingModule();
@@ -114,8 +122,8 @@ describe('CdkTextColumn', () => {
114122
}
115123

116124
it('should be able to provide a header text transformation', () => {
117-
const defaultHeaderTextTransformation = (name: string) => `${name}!`;
118-
createTestComponent({ defaultHeaderTextTransformation });
125+
const defaultHeaderTextTransform = (name: string) => `${name}!`;
126+
createTestComponent({defaultHeaderTextTransform});
119127

120128
expectTableToMatchContent(tableElement, [
121129
['propertyA!', 'propertyB!', 'propertyC!'],
@@ -137,7 +145,7 @@ describe('CdkTextColumn', () => {
137145
return '';
138146
}
139147
};
140-
createTestComponent({ defaultDataAccessor });
148+
createTestComponent({defaultDataAccessor});
141149

142150
expectTableToMatchContent(tableElement, [
143151
['PropertyA', 'PropertyB', 'PropertyC'],
@@ -178,3 +186,11 @@ class BasicTextColumnApp {
178186
dataAccessorA: (data: TestData) => string;
179187
justifyC = 'start';
180188
}
189+
190+
@Component({
191+
template: `
192+
<cdk-text-column name="column-a"></cdk-text-column>
193+
`
194+
})
195+
class MissingTableApp {
196+
}

src/cdk/table/text-column.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,23 @@ import {
2020
} from '@angular/core';
2121
import {CdkColumnDef} from './cell';
2222
import {CdkTable} from './table';
23+
import {getTableTextColumnMissingParentTableError} from './table-errors';
2324

2425
/** Configurable options for `CdkTextColumn`. */
25-
export interface TextColumnOptions {
26+
export interface TextColumnOptions<T> {
2627
/**
2728
* Default function that provides the header text based on the column name if a header
2829
* text is not provided.
2930
*/
30-
defaultHeaderTextTransformation?: (name: string) => string;
31+
defaultHeaderTextTransform?: (name: string) => string;
3132

3233
/** Default data accessor to use if one is not provided. */
33-
defaultDataAccessor?: (data: any, name: string) => string;
34+
defaultDataAccessor?: (data: T, name: string) => string;
3435
}
3536

3637
/** Injection token that can be used to specify the text column options. */
37-
export const TEXT_COLUMN_OPTIONS = new InjectionToken<TextColumnOptions>('text-column-options');
38+
export const TEXT_COLUMN_OPTIONS =
39+
new InjectionToken<TextColumnOptions<any>>('text-column-options');
3840

3941
/**
4042
* Column that simply shows text content for the header and row cells. Assumes that the table
@@ -70,7 +72,7 @@ export const TEXT_COLUMN_OPTIONS = new InjectionToken<TextColumnOptions>('text-c
7072
export class CdkTextColumn<T> implements OnDestroy, OnInit {
7173
/** Column name that should be used to reference this column. */
7274
@Input()
73-
get name(): string { return this._name; }
75+
get name(): string {return this._name;}
7476
set name(name: string) {
7577
this._name = name;
7678
this.columnDef.name = name;
@@ -97,7 +99,7 @@ export class CdkTextColumn<T> implements OnDestroy, OnInit {
9799
@ViewChild(CdkColumnDef) columnDef: CdkColumnDef;
98100

99101
constructor(@Optional() private table: CdkTable<T>,
100-
@Optional() @Inject(TEXT_COLUMN_OPTIONS) private options: TextColumnOptions) {
102+
@Optional() @Inject(TEXT_COLUMN_OPTIONS) private options: TextColumnOptions<T>) {
101103
this.options = options || {};
102104
}
103105

@@ -108,11 +110,13 @@ export class CdkTextColumn<T> implements OnDestroy, OnInit {
108110

109111
if (!this.dataAccessor) {
110112
this.dataAccessor = this.options.defaultDataAccessor ||
111-
((data: T, name: string) => (data as any)[name]);
113+
((data: T, name: string) => (data as any)[name]);
112114
}
113115

114116
if (this.table) {
115117
this.table.addColumnDef(this.columnDef);
118+
} else {
119+
throw getTableTextColumnMissingParentTableError();
116120
}
117121
}
118122

@@ -127,10 +131,10 @@ export class CdkTextColumn<T> implements OnDestroy, OnInit {
127131
* has been provided. Otherwise simply capitalize the column name.
128132
*/
129133
_createDefaultHeaderText() {
130-
if (this.options && this.options.defaultHeaderTextTransformation) {
131-
return this.options.defaultHeaderTextTransformation(this.name);
134+
if (this.options && this.options.defaultHeaderTextTransform) {
135+
return this.options.defaultHeaderTextTransform(this.name);
132136
}
133137

134-
return this.name.charAt(0).toUpperCase() + this.name.slice(1);
138+
return this.name[0].toUpperCase() + this.name.slice(1);
135139
}
136140
}

0 commit comments

Comments
 (0)