Skip to content

Commit 964b52a

Browse files
committed
refactor: use signal inputs
1 parent 804dc4c commit 964b52a

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

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,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';
@@ -18,23 +18,21 @@ const ageToCategory = (age: number): Category => {
1818
selector: 'app-user',
1919
imports: [TitleCasePipe],
2020
template: `
21-
{{ fullName | titlecase }} plays tennis in the {{ category }} category!!
21+
{{ fullName() | titlecase }} plays tennis in the {{ category() }} category!!
2222
`,
2323
host: {
2424
class: 'text-xl text-green-800',
2525
},
2626
changeDetection: ChangeDetectionStrategy.OnPush,
2727
})
28-
export class UserComponent implements OnChanges {
29-
@Input({ required: true }) name!: string;
30-
@Input() lastName?: string;
31-
@Input() age?: string;
28+
export class UserComponent {
29+
name = input.required<string>();
30+
lastName = input<string>();
31+
age = input(0, {
32+
transform: (value: number | string) =>
33+
typeof value === 'string' ? Number(value) : value,
34+
});
3235

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

0 commit comments

Comments
 (0)