Skip to content

Commit c2cf58d

Browse files
committed
refactor(challenge 43): refactor the UserComponent to utilize SignalInput
1 parent 543770b commit c2cf58d

1 file changed

Lines changed: 12 additions & 14 deletions

File tree

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

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

910
type Category = 'Youth' | 'Junior' | 'Open' | 'Senior';
@@ -18,23 +19,20 @@ const ageToCategory = (age: number): Category => {
1819
selector: 'app-user',
1920
imports: [TitleCasePipe],
2021
template: `
21-
{{ fullName | titlecase }} plays tennis in the {{ category }} category!!
22+
{{ fullName() | titlecase }} plays tennis in the {{ category() }} category!!
2223
`,
2324
host: {
2425
class: 'text-xl text-green-800',
2526
},
2627
changeDetection: ChangeDetectionStrategy.OnPush,
2728
})
28-
export class UserComponent implements OnChanges {
29-
@Input({ required: true }) name!: string;
30-
@Input() lastName?: string;
31-
@Input() age?: string;
29+
export class UserComponent {
30+
name = input.required<string>();
31+
lastName = input<string>();
32+
age = input<number, string>(0, { transform: numberAttribute });
3233

33-
fullName = '';
34-
category: Category = 'Junior';
35-
36-
ngOnChanges(): void {
37-
this.fullName = `${this.name} ${this.lastName ?? ''}`;
38-
this.category = ageToCategory(Number(this.age));
39-
}
34+
fullName = computed(() => `${this.name()} ${this.lastName() ?? ''}`);
35+
category = computed(() => {
36+
return ageToCategory(this.age());
37+
});
4038
}

0 commit comments

Comments
 (0)