Skip to content

Commit 9c32902

Browse files
crisbetoangular-robot[bot]
authored andcommitted
fix(material/schematics): support standalone projects in table schematic
Updates the `ng generate table` schematic to support standalone projects.
1 parent 8897b64 commit 9c32902

File tree

5 files changed

+62
-13
lines changed

5 files changed

+62
-13
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { waitForAsync, ComponentFixture, TestBed } from '@angular/core/testing';
2-
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
2+
import { NoopAnimationsModule } from '@angular/platform-browser/animations';<% if(!standalone) { %>
33
import { MatPaginatorModule } from '@angular/material/paginator';
44
import { MatSortModule } from '@angular/material/sort';
5-
import { MatTableModule } from '@angular/material/table';
5+
import { MatTableModule } from '@angular/material/table';<% } %>
66

77
import { <%= classify(name) %>Component } from './<%= dasherize(name) %>.component';
88

@@ -11,14 +11,15 @@ describe('<%= classify(name) %>Component', () => {
1111
let fixture: ComponentFixture<<%= classify(name) %>Component>;
1212

1313
beforeEach(waitForAsync(() => {
14-
TestBed.configureTestingModule({
15-
declarations: [ <%= classify(name) %>Component ],
14+
TestBed.configureTestingModule({<% if(standalone) { %>
15+
imports: [NoopAnimationsModule]<% } else { %>
16+
declarations: [<%= classify(name) %>Component],
1617
imports: [
1718
NoopAnimationsModule,
1819
MatPaginatorModule,
1920
MatSortModule,
2021
MatTableModule,
21-
]
22+
]<% } %>
2223
}).compileComponents();
2324
}));
2425

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { AfterViewInit, Component, ViewChild<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }%><% if(changeDetection !== 'Default') { %>, ChangeDetectionStrategy<% }%> } from '@angular/core';
2-
import { MatPaginator } from '@angular/material/paginator';
3-
import { MatSort } from '@angular/material/sort';
4-
import { MatTable } from '@angular/material/table';
2+
import { <% if(standalone) { %>MatTableModule, <% } %>MatTable } from '@angular/material/table';
3+
import { <% if(standalone) { %>MatPaginatorModule, <% } %>MatPaginator } from '@angular/material/paginator';
4+
import { <% if(standalone) { %>MatSortModule, <% } %>MatSort } from '@angular/material/sort';
55
import { <%= classify(name) %>DataSource, <%= classify(name) %>Item } from './<%= dasherize(name) %>-datasource';
66

77
@Component({
@@ -15,7 +15,9 @@ import { <%= classify(name) %>DataSource, <%= classify(name) %>Item } from './<%
1515
`]<% } else { %>
1616
styleUrls: ['./<%= dasherize(name) %>.component.<%= style %>']<% } %><% if(!!viewEncapsulation) { %>,
1717
encapsulation: ViewEncapsulation.<%= viewEncapsulation %><% } if (changeDetection !== 'Default') { %>,
18-
changeDetection: ChangeDetectionStrategy.<%= changeDetection %><% } %>
18+
changeDetection: ChangeDetectionStrategy.<%= changeDetection %><% } %><% if(standalone) { %>,
19+
standalone: true,
20+
imports: [MatTableModule, MatPaginatorModule, MatSortModule]<% } %>
1921
})
2022
export class <%= classify(name) %>Component implements AfterViewInit {
2123
@ViewChild(MatPaginator) paginator!: MatPaginator;

src/material/schematics/ng-generate/table/index.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,38 @@ describe('material-table-schematic', () => {
7070
).toBeRejectedWithError(/required property 'name'/);
7171
});
7272

73+
describe('standalone option', () => {
74+
it('should generate a standalone component', async () => {
75+
const app = await createTestApp(runner);
76+
const tree = await runner.runSchematic('table', {...baseOptions, standalone: true}, app);
77+
const module = getFileContent(tree, '/projects/material/src/app/app.module.ts');
78+
const component = getFileContent(tree, '/projects/material/src/app/foo/foo.component.ts');
79+
const requiredModules = ['MatTableModule', 'MatPaginatorModule', 'MatSortModule'];
80+
81+
requiredModules.forEach(name => {
82+
expect(module).withContext('Module should not import dependencies').not.toContain(name);
83+
expect(component).withContext('Component should import dependencies').toContain(name);
84+
});
85+
86+
expect(module).not.toContain('FooComponent');
87+
expect(component).toContain('standalone: true');
88+
expect(component).toContain('imports: [');
89+
});
90+
91+
it('should infer the standalone option from the project structure', async () => {
92+
const app = await createTestApp(runner, {standalone: true});
93+
const tree = await runner.runSchematic('table', baseOptions, app);
94+
const componentContent = getFileContent(
95+
tree,
96+
'/projects/material/src/app/foo/foo.component.ts',
97+
);
98+
99+
expect(tree.exists('/projects/material/src/app/app.module.ts')).toBe(false);
100+
expect(componentContent).toContain('standalone: true');
101+
expect(componentContent).toContain('imports: [');
102+
});
103+
});
104+
73105
describe('style option', () => {
74106
it('should respect the option value', async () => {
75107
const tree = await runner.runSchematic(

src/material/schematics/ng-generate/table/index.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
addModuleImportToModule,
1212
buildComponent,
1313
findModuleFromOptions,
14+
isStandaloneSchematic,
1415
} from '@angular/cdk/schematics';
1516
import {Schema} from './schema';
1617

@@ -38,9 +39,18 @@ export default function (options: Schema): Rule {
3839
*/
3940
function addTableModulesToModule(options: Schema) {
4041
return async (host: Tree) => {
41-
const modulePath = (await findModuleFromOptions(host, options))!;
42-
addModuleImportToModule(host, modulePath, 'MatTableModule', '@angular/material/table');
43-
addModuleImportToModule(host, modulePath, 'MatPaginatorModule', '@angular/material/paginator');
44-
addModuleImportToModule(host, modulePath, 'MatSortModule', '@angular/material/sort');
42+
const isStandalone = await isStandaloneSchematic(host, options);
43+
44+
if (!isStandalone) {
45+
const modulePath = (await findModuleFromOptions(host, options))!;
46+
addModuleImportToModule(host, modulePath, 'MatTableModule', '@angular/material/table');
47+
addModuleImportToModule(
48+
host,
49+
modulePath,
50+
'MatPaginatorModule',
51+
'@angular/material/paginator',
52+
);
53+
addModuleImportToModule(host, modulePath, 'MatSortModule', '@angular/material/sort');
54+
}
4555
};
4656
}

src/material/schematics/ng-generate/table/schema.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
"type": "boolean",
4040
"alias": "t"
4141
},
42+
"standalone": {
43+
"description": "Whether the generated component is standalone.",
44+
"type": "boolean"
45+
},
4246
"viewEncapsulation": {
4347
"description": "Specifies the view encapsulation strategy.",
4448
"enum": ["Emulated", "None"],

0 commit comments

Comments
 (0)