Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.

Commit 4197b8e

Browse files
authored
refactor: switch to signal queries (#1271)
* refactor: switch to signal queries Runs the signal queries migration in this repository. * fixup! refactor: switch to signal queries Fix type * fixup! refactor: switch to signal queries Fix type
1 parent 0b9a906 commit 4197b8e

File tree

14 files changed

+65
-66
lines changed

14 files changed

+65
-66
lines changed

scenes/src/app/scene-viewer/scene-viewer.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import {
44
HostBinding,
55
Input,
66
OnInit,
7-
ViewChild,
87
ViewContainerRef,
9-
ViewEncapsulation
8+
ViewEncapsulation,
9+
viewChild
1010
} from '@angular/core';
1111
import {ActivatedRoute} from '@angular/router';
1212
import {DomSanitizer, SafeStyle} from '@angular/platform-browser';
@@ -44,8 +44,7 @@ export class SceneViewer implements OnInit {
4444
/** Component of scene to display */
4545
@Input() component: any;
4646

47-
@ViewChild('scene', {read: ViewContainerRef, static: true})
48-
scene!: ViewContainerRef;
47+
readonly scene = viewChild.required('scene', { read: ViewContainerRef });
4948

5049
constructor(private readonly componentFactoryResolver: ComponentFactoryResolver,
5150
private route: ActivatedRoute,
@@ -57,7 +56,7 @@ export class SceneViewer implements OnInit {
5756

5857
ngOnInit() {
5958
const componentFactory = this.componentFactoryResolver.resolveComponentFactory(this.component);
60-
this.scene.createComponent(componentFactory);
59+
this.scene().createComponent(componentFactory);
6160
const container = document.querySelector('#scene-content-container') as HTMLElement;
6261
container.style.transform = `scale(${this.scale})`;
6362
container.style.transformOrigin = 'center';

scenes/src/app/scenes/autocomplete/autocomplete-scene.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {
22
AfterViewInit,
33
Component,
4-
ViewChild,
5-
ViewEncapsulation
4+
ViewEncapsulation,
5+
viewChild
66
} from '@angular/core';
77
import {FormControl, FormsModule, ReactiveFormsModule} from '@angular/forms';
88
import {MatFormFieldModule} from '@angular/material/form-field';
@@ -27,10 +27,10 @@ export class AutocompleteScene implements AfterViewInit {
2727
myControl = new FormControl('');
2828
options: string[] = ['hello', 'hello world'];
2929

30-
@ViewChild(MatInput) input!: MatInput;
30+
readonly input = viewChild.required(MatInput);
3131

3232
ngAfterViewInit() {
33-
this.input.focus();
33+
this.input().focus();
3434
}
3535
}
3636

scenes/src/app/scenes/menu/menu-scene.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {AfterViewInit, Component, ViewChild, ViewEncapsulation} from '@angular/core';
1+
import {AfterViewInit, Component, ViewEncapsulation, viewChild} from '@angular/core';
22
import {MatButtonModule} from '@angular/material/button';
33
import {MatIconModule} from '@angular/material/icon';
44
import {MatMenuModule, MatMenuTrigger} from '@angular/material/menu';
@@ -12,9 +12,9 @@ import {MatMenuModule, MatMenuTrigger} from '@angular/material/menu';
1212
imports: [MatButtonModule, MatMenuModule, MatIconModule]
1313
})
1414
export class MenuScene implements AfterViewInit {
15-
@ViewChild('menuTrigger') trigger!: MatMenuTrigger;
15+
readonly trigger = viewChild.required<MatMenuTrigger>('menuTrigger');
1616

1717
ngAfterViewInit() {
18-
this.trigger.openMenu();
18+
this.trigger().openMenu();
1919
}
2020
}

scenes/src/app/scenes/ripples/ripples-scene.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {AfterViewInit, Component, ViewChild, ViewEncapsulation} from '@angular/core';
1+
import {AfterViewInit, Component, ViewEncapsulation, viewChild} from '@angular/core';
22
import {MatButtonModule} from '@angular/material/button';
33
import {MatRipple, MatRippleModule} from '@angular/material/core';
44

@@ -11,17 +11,17 @@ import {MatRipple, MatRippleModule} from '@angular/material/core';
1111
imports: [MatRippleModule, MatButtonModule]
1212
})
1313
export class RipplesScene implements AfterViewInit {
14-
@ViewChild('button', {read: MatRipple}) buttonRipple!: MatRipple;
15-
@ViewChild('wrapper', {read: MatRipple}) wrapperRipple!: MatRipple;
14+
readonly buttonRipple = viewChild.required('button', { read: MatRipple });
15+
readonly wrapperRipple = viewChild.required('wrapper', { read: MatRipple });
1616

1717
ngAfterViewInit() {
18-
this.buttonRipple.launch(140, 100, {
18+
this.buttonRipple().launch(140, 100, {
1919
persistent: true,
2020
animation: {enterDuration: 0},
2121
radius: 50,
2222
});
2323

24-
this.wrapperRipple.launch(300, 100, {
24+
this.wrapperRipple().launch(300, 100, {
2525
persistent: true,
2626
animation: {enterDuration: 0},
2727
radius: 150,

scenes/src/app/scenes/select/select-scene.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {AfterViewInit, Component, ViewChild, ViewEncapsulation} from '@angular/core';
1+
import {AfterViewInit, Component, ViewEncapsulation, viewChild} from '@angular/core';
22
import {MatFormFieldModule} from '@angular/material/form-field';
33
import {MatSelect, MatSelectModule} from '@angular/material/select';
44
import {MatOptionModule} from '@angular/material/core';
@@ -12,9 +12,9 @@ import {MatOptionModule} from '@angular/material/core';
1212
imports: [MatFormFieldModule, MatSelectModule, MatOptionModule]
1313
})
1414
export class SelectScene implements AfterViewInit {
15-
@ViewChild(MatSelect) select!: MatSelect;
15+
readonly select = viewChild.required(MatSelect);
1616

1717
ngAfterViewInit() {
18-
this.select.open();
18+
this.select().open();
1919
}
2020
}

scenes/src/app/scenes/tooltip/tooltip-scene.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Component, ViewChild, AfterViewInit, ViewEncapsulation} from '@angular/core';
1+
import {Component, AfterViewInit, ViewEncapsulation, viewChild} from '@angular/core';
22
import {MatButtonModule} from '@angular/material/button';
33
import {MatTooltipModule, MatTooltip} from '@angular/material/tooltip';
44
import {MatIconModule} from '@angular/material/icon';
@@ -16,9 +16,9 @@ import {MatIconModule} from '@angular/material/icon';
1616
],
1717
})
1818
export class TooltipScene implements AfterViewInit {
19-
@ViewChild(MatTooltip) tooltip!: MatTooltip;
19+
readonly tooltip = viewChild.required(MatTooltip);
2020

2121
ngAfterViewInit() {
22-
this.tooltip.toggle();
22+
this.tooltip().toggle();
2323
}
2424
}

src/app/pages/component-sidenav/component-sidenav.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</mat-sidenav>
1111
}
1212
<div class="docs-component-sidenav-content">
13-
<component-page-header (toggleSidenav)="toggleSidenav(sidenav)"></component-page-header>
13+
<component-page-header (toggleSidenav)="toggleSidenav(sidenav())"></component-page-header>
1414
<div class="docs-component-sidenav-inner-content">
1515
<main class="docs-component-sidenav-body-content">
1616
<!-- If on large screen, menu resides to left of content -->

src/app/pages/component-sidenav/component-sidenav.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ describe('ComponentSidenav', () => {
2828

2929
// TODO refactor this as none of these expectations are ever verified
3030
waitForAsync(() => {
31-
expect(component.sidenav instanceof MatSidenav).toBeTruthy();
31+
expect(component.sidenav() instanceof MatSidenav).toBeTruthy();
3232
component.isScreenSmall.pipe(take(1)).subscribe(isSmall => expect(isSmall).toBeTruthy());
33-
expect(component.sidenav.opened).toBe(false);
33+
expect(component.sidenav().opened).toBe(false);
3434
});
3535
});
3636

src/app/pages/component-sidenav/component-sidenav.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import {
44
NgZone,
55
OnDestroy,
66
OnInit,
7-
ViewChild,
87
ViewEncapsulation,
98
forwardRef,
10-
input
9+
input,
10+
viewChild
1111
} from '@angular/core';
1212
import {animate, state, style, transition, trigger} from '@angular/animations';
1313
import {CdkAccordionModule} from '@angular/cdk/accordion';
@@ -82,7 +82,7 @@ const SMALL_WIDTH_BREAKPOINT = 959;
8282
],
8383
})
8484
export class ComponentSidenav implements OnInit, OnDestroy {
85-
@ViewChild(MatSidenav) sidenav!: MatSidenav;
85+
readonly sidenav = viewChild.required(MatSidenav);
8686
params: Observable<Params> | undefined;
8787
isExtraScreenSmall: Observable<boolean>;
8888
isScreenSmall: Observable<boolean>;
@@ -108,8 +108,9 @@ export class ComponentSidenav implements OnInit, OnDestroy {
108108
this.subscriptions.add(
109109
this._navigationFocusService.navigationEndEvents.pipe(map(() => this.isScreenSmall))
110110
.subscribe((shouldCloseSideNav) => {
111-
if (shouldCloseSideNav && this.sidenav) {
112-
this.sidenav.close();
111+
const sidenav = this.sidenav();
112+
if (shouldCloseSideNav && sidenav) {
113+
sidenav.close();
113114
}
114115
}
115116
));

src/app/pages/component-viewer/component-viewer.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ import {
77
NgModule,
88
OnDestroy,
99
OnInit,
10-
QueryList,
11-
ViewChild,
12-
ViewChildren,
1310
ViewEncapsulation,
11+
viewChild,
12+
viewChildren
1413
} from '@angular/core';
1514
import {MatTabsModule} from '@angular/material/tabs';
1615
import {ActivatedRoute,
@@ -95,8 +94,8 @@ export class ComponentViewer implements OnDestroy {
9594
*/
9695
@Directive()
9796
export class ComponentBaseView implements OnInit, OnDestroy {
98-
@ViewChild('toc') tableOfContents!: TableOfContents;
99-
@ViewChildren(DocViewer) viewers!: QueryList<DocViewer>;
97+
readonly tableOfContents = viewChild.required<TableOfContents>('toc');
98+
readonly viewers = viewChildren(DocViewer);
10099

101100
showToc: Observable<boolean>;
102101
private _destroyed = new Subject();
@@ -116,17 +115,18 @@ export class ComponentBaseView implements OnInit, OnDestroy {
116115

117116
ngOnInit() {
118117
this.componentViewer.componentDocItem.pipe(takeUntil(this._destroyed)).subscribe(() => {
119-
if (this.tableOfContents) {
120-
this.tableOfContents.resetHeaders();
118+
const tableOfContents = this.tableOfContents();
119+
if (tableOfContents) {
120+
tableOfContents.resetHeaders();
121121
}
122122
});
123123

124124
this.showToc.pipe(
125125
skip(1),
126126
takeUntil(this._destroyed)
127127
).subscribe(() => {
128-
if (this.tableOfContents) {
129-
this.viewers.forEach(viewer => {
128+
if (this.tableOfContents()) {
129+
this.viewers().forEach(viewer => {
130130
viewer.contentRendered.emit(viewer._elementRef.nativeElement);
131131
});
132132
}
@@ -139,9 +139,10 @@ export class ComponentBaseView implements OnInit, OnDestroy {
139139
}
140140

141141
updateTableOfContents(sectionName: string, docViewerContent: HTMLElement, sectionIndex = 0) {
142-
if (this.tableOfContents) {
143-
this.tableOfContents.addHeaders(sectionName, docViewerContent, sectionIndex);
144-
this.tableOfContents.updateScrollPosition();
142+
const tableOfContents = this.tableOfContents();
143+
if (tableOfContents) {
144+
tableOfContents.addHeaders(sectionName, docViewerContent, sectionIndex);
145+
tableOfContents.updateScrollPosition();
145146
}
146147
}
147148
}

0 commit comments

Comments
 (0)