Skip to content

Commit 0441ae9

Browse files
authored
fix(material/schematics): generate code that works in strict CLI projects (#22052)
* fix(material/schematics): generate code that works in strict CLI projects - fix `strictNullChecks` errors - fix `strictPropertyInitialization` errors - default to smaller paginator rows so that paging works out of the box - fix tslint errors from default CLI settings Fixes #21981 * fix(cdk/schematics): generate code that works in strict CLI projects - fix tslint error from default CLI settings Relates to #21981
1 parent 389e66a commit 0441ae9

File tree

6 files changed

+42
-39
lines changed

6 files changed

+42
-39
lines changed

src/cdk/schematics/ng-generate/drag-drop/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.ts.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class <%= classify(name) %>Component {
3030
'Walk dog'
3131
];
3232

33-
drop(event: CdkDragDrop<string[]>) {
33+
drop(event: CdkDragDrop<string[]>): void {
3434
if (event.previousContainer === event.container) {
3535
moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
3636
} else {

src/material/schematics/ng-generate/address-form/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.ts.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export class <%= classify(name) %>Component {
9595

9696
constructor(private fb: FormBuilder) {}
9797

98-
onSubmit() {
98+
onSubmit(): void {
9999
alert('Thanks!');
100100
}
101101
}

src/material/schematics/ng-generate/table/files/__path__/__name@dasherize@if-flat__/__name@dasherize__-datasource.ts.template

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ const EXAMPLE_DATA: <%= classify(name) %>Item[] = [
4141
*/
4242
export class <%= classify(name) %>DataSource extends DataSource<<%= classify(name) %>Item> {
4343
data: <%= classify(name) %>Item[] = EXAMPLE_DATA;
44-
paginator: MatPaginator;
45-
sort: MatSort;
44+
paginator: MatPaginator | undefined;
45+
sort: MatSort | undefined;
4646

4747
constructor() {
4848
super();
@@ -54,46 +54,49 @@ export class <%= classify(name) %>DataSource extends DataSource<<%= classify(nam
5454
* @returns A stream of the items to be rendered.
5555
*/
5656
connect(): Observable<<%= classify(name) %>Item[]> {
57-
// Combine everything that affects the rendered data into one update
58-
// stream for the data-table to consume.
59-
const dataMutations = [
60-
observableOf(this.data),
61-
this.paginator.page,
62-
this.sort.sortChange
63-
];
64-
65-
return merge(...dataMutations).pipe(map(() => {
66-
return this.getPagedData(this.getSortedData([...this.data]));
67-
}));
57+
if (this.paginator && this.sort) {
58+
// Combine everything that affects the rendered data into one update
59+
// stream for the data-table to consume.
60+
return merge(observableOf(this.data), this.paginator.page, this.sort.sortChange)
61+
.pipe(map(() => {
62+
return this.getPagedData(this.getSortedData([...this.data ]));
63+
}));
64+
} else {
65+
throw Error('Please set the paginator and sort on the data source before connecting.');
66+
}
6867
}
6968

7069
/**
7170
* Called when the table is being destroyed. Use this function, to clean up
7271
* any open connections or free any held resources that were set up during connect.
7372
*/
74-
disconnect() {}
73+
disconnect(): void {}
7574

7675
/**
7776
* Paginate the data (client-side). If you're using server-side pagination,
7877
* this would be replaced by requesting the appropriate data from the server.
7978
*/
80-
private getPagedData(data: <%= classify(name) %>Item[]) {
81-
const startIndex = this.paginator.pageIndex * this.paginator.pageSize;
82-
return data.splice(startIndex, this.paginator.pageSize);
79+
private getPagedData(data: <%= classify(name) %>Item[]): <%= classify(name) %>Item[] {
80+
if (this.paginator) {
81+
const startIndex = this.paginator.pageIndex * this.paginator.pageSize;
82+
return data.splice(startIndex, this.paginator.pageSize);
83+
} else {
84+
return data;
85+
}
8386
}
8487

8588
/**
8689
* Sort the data (client-side). If you're using server-side sorting,
8790
* this would be replaced by requesting the appropriate data from the server.
8891
*/
89-
private getSortedData(data: <%= classify(name) %>Item[]) {
90-
if (!this.sort.active || this.sort.direction === '') {
92+
private getSortedData(data: <%= classify(name) %>Item[]): <%= classify(name) %>Item[] {
93+
if (!this.sort || !this.sort.active || this.sort.direction === '') {
9194
return data;
9295
}
9396

9497
return data.sort((a, b) => {
95-
const isAsc = this.sort.direction === 'asc';
96-
switch (this.sort.active) {
98+
const isAsc = this.sort?.direction === 'asc';
99+
switch (this.sort?.active) {
97100
case 'name': return compare(a.name, b.name, isAsc);
98101
case 'id': return compare(+a.id, +b.id, isAsc);
99102
default: return 0;
@@ -103,6 +106,6 @@ export class <%= classify(name) %>DataSource extends DataSource<<%= classify(nam
103106
}
104107

105108
/** Simple sort comparator for example ID/Name columns (for client-side sorting). */
106-
function compare(a: string | number, b: string | number, isAsc: boolean) {
109+
function compare(a: string | number, b: string | number, isAsc: boolean): number {
107110
return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
108111
}

src/material/schematics/ng-generate/table/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.html.template

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
</table>
1818

1919
<mat-paginator #paginator
20-
[length]="dataSource?.data.length"
20+
[length]="dataSource?.data?.length"
2121
[pageIndex]="0"
22-
[pageSize]="50"
23-
[pageSizeOptions]="[25, 50, 100, 250]">
22+
[pageSize]="10"
23+
[pageSizeOptions]="[5, 10, 20]">
2424
</mat-paginator>
2525
</div>

src/material/schematics/ng-generate/table/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.ts.template

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AfterViewInit, Component, OnInit, ViewChild<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }%><% if(changeDetection !== 'Default') { %>, ChangeDetectionStrategy<% }%> } from '@angular/core';
1+
import { AfterViewInit, Component, ViewChild<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }%><% if(changeDetection !== 'Default') { %>, ChangeDetectionStrategy<% }%> } from '@angular/core';
22
import { MatPaginator } from '@angular/material/paginator';
33
import { MatSort } from '@angular/material/sort';
44
import { MatTable } from '@angular/material/table';
@@ -17,20 +17,20 @@ import { <%= classify(name) %>DataSource, <%= classify(name) %>Item } from './<%
1717
encapsulation: ViewEncapsulation.<%= viewEncapsulation %><% } if (changeDetection !== 'Default') { %>,
1818
changeDetection: ChangeDetectionStrategy.<%= changeDetection %><% } %>
1919
})
20-
export class <%= classify(name) %>Component implements AfterViewInit, OnInit {
21-
@ViewChild(MatPaginator) paginator: MatPaginator;
22-
@ViewChild(MatSort) sort: MatSort;
23-
@ViewChild(MatTable) table: MatTable<<%= classify(name) %>Item>;
20+
export class <%= classify(name) %>Component implements AfterViewInit {
21+
@ViewChild(MatPaginator) paginator!: MatPaginator;
22+
@ViewChild(MatSort) sort!: MatSort;
23+
@ViewChild(MatTable) table!: MatTable<<%= classify(name) %>Item>;
2424
dataSource: <%= classify(name) %>DataSource;
2525

2626
/** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
2727
displayedColumns = ['id', 'name'];
2828

29-
ngOnInit() {
29+
constructor() {
3030
this.dataSource = new <%= classify(name) %>DataSource();
3131
}
3232

33-
ngAfterViewInit() {
33+
ngAfterViewInit(): void {
3434
this.dataSource.sort = this.sort;
3535
this.dataSource.paginator = this.paginator;
3636
this.table.dataSource = this.dataSource;

src/material/schematics/ng-generate/tree/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.ts.template

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,27 +58,27 @@ export class <%= classify(name) %>Component {
5858
}
5959

6060
/** Transform the data to something the tree can read. */
61-
transformer(node: FileNode, level: number) {
61+
transformer(node: FileNode, level: number): FlatTreeNode {
6262
return {
6363
name: node.name,
6464
type: node.type,
65-
level: level,
65+
level,
6666
expandable: !!node.children
6767
};
6868
}
6969

7070
/** Get the level of the node */
71-
getLevel(node: FlatTreeNode) {
71+
getLevel(node: FlatTreeNode): number {
7272
return node.level;
7373
}
7474

7575
/** Get whether the node is expanded or not. */
76-
isExpandable(node: FlatTreeNode) {
76+
isExpandable(node: FlatTreeNode): boolean {
7777
return node.expandable;
7878
}
7979

8080
/** Get whether the node has children or not. */
81-
hasChild(index: number, node: FlatTreeNode) {
81+
hasChild(index: number, node: FlatTreeNode): boolean {
8282
return node.expandable;
8383
}
8484

0 commit comments

Comments
 (0)