Skip to content

Commit

Permalink
feat: convert plain inputs to signal inputs with transform
Browse files Browse the repository at this point in the history
  • Loading branch information
mauwalB committed Dec 1, 2024
1 parent 7756baa commit f4ecced
Show file tree
Hide file tree
Showing 3 changed files with 5,305 additions and 5,722 deletions.
28 changes: 14 additions & 14 deletions apps/signal/43-signal-input/src/app/user.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { TitleCasePipe } from '@angular/common';
import {
ChangeDetectionStrategy,
Component,
Input,
OnChanges,
computed,
input,
} from '@angular/core';

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

function parseAge(age: string | undefined): number {
if (!age) return 0;
return +age;
}

@Component({
selector: 'app-user',
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;

fullName = '';
category: Category = 'Junior';
export class UserComponent {
name = input.required<string>();
lastName = input<string>();
age = input(0, { transform: parseAge });

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

0 comments on commit f4ecced

Please sign in to comment.