Skip to content

Commit

Permalink
feat: fix signal input challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
AzureAD\IoannisTsironis committed Apr 17, 2024
1 parent b61c242 commit 1e7f1ff
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions apps/angular/signal-input/src/app/user.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { TitleCasePipe } from '@angular/common';
import {
ChangeDetectionStrategy,
Component,
Input,
OnChanges,
Signal,
computed,
input,
} from '@angular/core';

type Category = 'Youth' | 'Junior' | 'Open' | 'Senior';
Expand All @@ -19,23 +20,24 @@ const ageToCategory = (age: number): Category => {
standalone: true,
imports: [TitleCasePipe],
template: `
{{ fullName | titlecase }} plays tennis in the {{ category }} category!!
{{ fullName() | titlecase }} plays tennis in the {{ category() }} category!!
`,
host: {
class: 'text-xl text-green-800',
},
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class UserComponent implements OnChanges {
@Input({ required: true }) name!: string;
@Input() lastName?: string;
@Input() age?: string;
export class UserComponent {
name = input.required<string>();
lastName = input<string>();
age = input(0, {
transform: (value: string): number => Number(value),
});

fullName = '';
category: Category = 'Junior';

ngOnChanges(): void {
this.fullName = `${this.name} ${this.lastName ?? ''}`;
this.category = ageToCategory(Number(this.age) ?? 0);
}
fullName: Signal<string> = computed(
() => `${this.name()} ${this.lastName() ?? ''}`,
);
category: Signal<string> = computed(() =>
ageToCategory(Number(this.age()) ?? 0),
);
}

0 comments on commit 1e7f1ff

Please sign in to comment.