Skip to content

Commit f4ecced

Browse files
committed
feat: convert plain inputs to signal inputs with transform
1 parent 7756baa commit f4ecced

File tree

3 files changed

+5305
-5722
lines changed

3 files changed

+5305
-5722
lines changed

apps/signal/43-signal-input/src/app/user.component.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { TitleCasePipe } from '@angular/common';
22
import {
33
ChangeDetectionStrategy,
44
Component,
5-
Input,
6-
OnChanges,
5+
computed,
6+
input,
77
} from '@angular/core';
88

99
type Category = 'Youth' | 'Junior' | 'Open' | 'Senior';
@@ -14,28 +14,28 @@ const ageToCategory = (age: number): Category => {
1414
return 'Senior';
1515
};
1616

17+
function parseAge(age: string | undefined): number {
18+
if (!age) return 0;
19+
return +age;
20+
}
21+
1722
@Component({
1823
selector: 'app-user',
1924
standalone: true,
2025
imports: [TitleCasePipe],
2126
template: `
22-
{{ fullName | titlecase }} plays tennis in the {{ category }} category!!
27+
{{ fullName() | titlecase }} plays tennis in the {{ category() }} category!!
2328
`,
2429
host: {
2530
class: 'text-xl text-green-800',
2631
},
2732
changeDetection: ChangeDetectionStrategy.OnPush,
2833
})
29-
export class UserComponent implements OnChanges {
30-
@Input({ required: true }) name!: string;
31-
@Input() lastName?: string;
32-
@Input() age?: string;
33-
34-
fullName = '';
35-
category: Category = 'Junior';
34+
export class UserComponent {
35+
name = input.required<string>();
36+
lastName = input<string>();
37+
age = input(0, { transform: parseAge });
3638

37-
ngOnChanges(): void {
38-
this.fullName = `${this.name} ${this.lastName ?? ''}`;
39-
this.category = ageToCategory(Number(this.age) ?? 0);
40-
}
39+
fullName = computed(() => `${this.name()} ${this.lastName() ?? ''}`);
40+
category = computed(() => ageToCategory(this.age() ?? 0));
4141
}

0 commit comments

Comments
 (0)