forked from tomalaforge/angular-challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.component.ts
More file actions
39 lines (37 loc) · 1.05 KB
/
user.component.ts
File metadata and controls
39 lines (37 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { TitleCasePipe } from '@angular/common';
import {
ChangeDetectionStrategy,
Component,
computed,
input,
} from '@angular/core';
type Category = 'Youth' | 'Junior' | 'Open' | 'Senior';
const ageToCategory = (age: number): Category => {
if (age < 10) return 'Youth';
else if (age < 18) return 'Junior';
else if (age < 35) return 'Open';
return 'Senior';
};
@Component({
selector: 'app-user',
imports: [TitleCasePipe],
template: `
{{ fullName() | titlecase }} plays tennis in the {{ category() }} category!!
`,
host: {
class: 'text-xl text-green-800',
},
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class UserComponent {
// @Input({ required: true }) name!: string;
name = input.required<string>();
// @Input() lastName?: string;
lastName = input<string>();
// @Input() age?: string;
age = input<string>();
// fullName = '';
fullName = computed(() => `${this.name()} ${this.lastName() || ''}`);
// category: Category = 'Junior';
category = computed(() => ageToCategory(Number(this.age()) ?? 0));
}