Skip to content

Commit f44c3f1

Browse files
committed
feat(): migrate everything to input signals
1 parent 503196e commit f44c3f1

File tree

23 files changed

+135
-196
lines changed

23 files changed

+135
-196
lines changed
Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable @angular-eslint/component-selector */
2-
import { Component, Input } from '@angular/core';
2+
import { Component, computed, input } from '@angular/core';
33
import { TextComponent } from './text.component';
44

55
export type StaticTextType = 'normal' | 'warning' | 'error';
@@ -8,25 +8,31 @@ export type StaticTextType = 'normal' | 'warning' | 'error';
88
selector: 'static-text',
99
imports: [TextComponent],
1010
template: `
11-
<text [font]="font" [color]="color">This is a static text</text>
11+
<text [font]="font()" [color]="color()">This is a static text</text>
1212
`,
1313
})
1414
export class TextStaticComponent {
15-
@Input() set type(type: StaticTextType) {
16-
switch (type) {
17-
case 'error': {
18-
this.font = 30;
19-
this.color = 'red';
20-
break;
21-
}
22-
case 'warning': {
23-
this.font = 25;
24-
this.color = 'orange';
25-
break;
26-
}
15+
type = input<StaticTextType>('normal');
16+
17+
font = computed(() => {
18+
switch (this.type()) {
19+
case 'error':
20+
return 30;
21+
case 'warning':
22+
return 25;
23+
default:
24+
return 10;
2725
}
28-
}
26+
});
2927

30-
font = 10;
31-
color = 'black';
28+
color = computed(() => {
29+
switch (this.type()) {
30+
case 'error':
31+
return 'red';
32+
case 'warning':
33+
return 'orange';
34+
default:
35+
return 'black';
36+
}
37+
});
3238
}
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
/* eslint-disable @angular-eslint/component-selector */
2-
import { Component, Input } from '@angular/core';
2+
import { Component, input } from '@angular/core';
33

44
@Component({
55
selector: 'text',
6-
standalone: true,
76
template: `
87
<p style="font-size: {{ font }}px; color: {{ color }}">
9-
<ng-content></ng-content>
8+
<ng-content />
109
</p>
1110
`,
1211
})
1312
export class TextComponent {
14-
@Input() font = 10;
15-
@Input() color = 'black';
13+
font = input(10);
14+
color = input('black');
1615
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
/* eslint-disable @angular-eslint/component-selector */
2-
import { Component, Input } from '@angular/core';
2+
import { Component, input } from '@angular/core';
3+
34
@Component({
45
selector: 'nav-button',
5-
standalone: true,
66
template: `
7-
<a [href]="href">
8-
<ng-content></ng-content>
7+
<a [href]="href()">
8+
<ng-content />
99
</a>
1010
`,
1111
host: {
1212
class: 'block w-fit border border-red-500 rounded-md p-4 m-2',
1313
},
1414
})
1515
export class NavButtonComponent {
16-
@Input() href = '';
16+
href = input('');
1717
}

apps/angular/32-change-detection-bug/src/app/main-navigation.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AsyncPipe } from '@angular/common';
2-
import { Component, inject, Input } from '@angular/core';
2+
import { Component, inject, input } from '@angular/core';
33
import { RouterLink, RouterLinkActive } from '@angular/router';
44
import { FakeServiceService } from './fake.service';
55

@@ -12,7 +12,7 @@ interface MenuItem {
1212
selector: 'app-nav',
1313
imports: [RouterLink, RouterLinkActive],
1414
template: `
15-
@for (menu of menus; track menu.path) {
15+
@for (menu of menus(); track menu.path) {
1616
<a
1717
class="rounded-md border px-4 py-2"
1818
[routerLink]="menu.path"
@@ -33,7 +33,7 @@ interface MenuItem {
3333
},
3434
})
3535
export class NavigationComponent {
36-
@Input() menus!: MenuItem[];
36+
menus = input.required<MenuItem[]>();
3737
}
3838

3939
@Component({

apps/angular/5-crud-application/src/app/app.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { CommonModule } from '@angular/common';
22
import { HttpClient } from '@angular/common/http';
3-
import { Component, OnInit } from '@angular/core';
3+
import { Component, inject, OnInit } from '@angular/core';
44
import { randText } from '@ngneat/falso';
55

66
@Component({
@@ -15,9 +15,9 @@ import { randText } from '@ngneat/falso';
1515
styles: [],
1616
})
1717
export class AppComponent implements OnInit {
18-
todos!: any[];
18+
private http = inject(HttpClient);
1919

20-
constructor(private http: HttpClient) {}
20+
todos!: any[];
2121

2222
ngOnInit(): void {
2323
this.http
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { Component } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component } from '@angular/core';
22
import { RouterOutlet } from '@angular/router';
33

44
@Component({
55
imports: [RouterOutlet],
66
selector: 'app-root',
77
template: `
8-
<router-outlet></router-outlet>
8+
<router-outlet />
99
`,
10-
styles: [],
10+
changeDetection: ChangeDetectionStrategy.OnPush,
1111
})
1212
export class AppComponent {}

apps/angular/6-structural-directive/src/app/button.component.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
import { ChangeDetectionStrategy, Component } from '@angular/core';
33

44
@Component({
5-
standalone: true,
65
selector: 'button[app-button]',
76
template: `
8-
<ng-content></ng-content>
7+
<ng-content />
98
`,
109
host: {
1110
class: 'border border-blue-700 bg-blue-400 p-2 rounded-sm text-white',

apps/angular/6-structural-directive/src/app/information.component.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import { CommonModule } from '@angular/common';
2-
import { ChangeDetectionStrategy, Component } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
32
import { UserStore } from './user.store';
43

54
@Component({
65
selector: 'app-information',
7-
imports: [CommonModule],
86
template: `
97
<h2 class="mt-10 text-xl">Information Panel</h2>
108
<!-- admin can see everything -->
@@ -18,6 +16,7 @@ import { UserStore } from './user.store';
1816
changeDetection: ChangeDetectionStrategy.OnPush,
1917
})
2018
export class InformationComponent {
19+
private readonly userStore = inject(UserStore);
20+
2121
user$ = this.userStore.user$;
22-
constructor(private userStore: UserStore) {}
2322
}

apps/angular/6-structural-directive/src/app/login.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component } from '@angular/core';
1+
import { Component, inject } from '@angular/core';
22
import { RouterLink } from '@angular/router';
33
import { ButtonComponent } from './button.component';
44
import { InformationComponent } from './information.component';
@@ -28,15 +28,15 @@ import { UserStore } from './user.store';
2828
<button app-button (click)="everyone()">Everyone</button>
2929
</header>
3030
31-
<app-information></app-information>
31+
<app-information />
3232
3333
<button app-button class="mt-10" routerLink="enter">
3434
Enter application
3535
</button>
3636
`,
3737
})
3838
export class LoginComponent {
39-
constructor(private userStore: UserStore) {}
39+
private readonly userStore = inject(UserStore);
4040

4141
admin() {
4242
this.userStore.add(admin);

apps/performance/34-default-vs-onpush/src/app/person-list.component.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, Input } from '@angular/core';
1+
import { Component, input } from '@angular/core';
22

33
import { CDFlashingDirective } from '@angular-challenges/shared/directives';
44
import { TitleCasePipe } from '@angular/common';
@@ -20,8 +20,8 @@ import { MatListModule } from '@angular/material/list';
2020
TitleCasePipe,
2121
],
2222
template: `
23-
<h1 cd-flash class="text-center font-semibold" title="Title">
24-
{{ title | titlecase }}
23+
<h1 class="text-center font-semibold" title="Title">
24+
{{ title() | titlecase }}
2525
</h1>
2626
2727
<mat-form-field class="w-4/5" cd-flash>
@@ -34,10 +34,10 @@ import { MatListModule } from '@angular/material/list';
3434
</mat-form-field>
3535
3636
<mat-list class="flex w-full">
37-
@if (names?.length === 0) {
37+
@if (names()?.length === 0) {
3838
<div class="empty-list-label">Empty list</div>
3939
}
40-
@for (name of names; track name) {
40+
@for (name of names(); track name) {
4141
<mat-list-item cd-flash class="text-orange-500">
4242
<div class="flex justify-between">
4343
<h3 title="Name">
@@ -46,7 +46,7 @@ import { MatListModule } from '@angular/material/list';
4646
</div>
4747
</mat-list-item>
4848
}
49-
@if (names?.length !== 0) {
49+
@if (names()?.length !== 0) {
5050
<mat-divider></mat-divider>
5151
}
5252
</mat-list>
@@ -56,14 +56,14 @@ import { MatListModule } from '@angular/material/list';
5656
},
5757
})
5858
export class PersonListComponent {
59-
@Input() names: string[] = [];
60-
@Input() title = '';
59+
names = input<string[]>([]);
60+
title = input('');
6161

6262
label = '';
6363

6464
handleKey(event: KeyboardEvent) {
6565
if (event.key === 'Enter') {
66-
this.names?.unshift(this.label);
66+
this.names()?.unshift(this.label);
6767
this.label = '';
6868
}
6969
}

0 commit comments

Comments
 (0)