feat: challenge 43 input signal

This commit is contained in:
thomas
2024-01-15 21:41:57 +01:00
parent ef1f6b644c
commit 79b0301a87
21 changed files with 328 additions and 10 deletions

View File

@@ -0,0 +1,45 @@
import { JsonPipe } from '@angular/common';
import { Component } from '@angular/core';
import { UserComponent } from './user.component';
@Component({
standalone: true,
imports: [UserComponent, JsonPipe],
selector: 'app-root',
template: `
<div class="flex flex-col gap-3">
<div class="flex gap-2 ">
Name:
<input #name class="border" />
@if (showUser && !name.value) {
<div class="text-sm text-red-500">name required</div>
}
</div>
<div class="flex gap-2 ">
LastName:
<input #lastName class="border" />
</div>
<div class="flex gap-2 ">
Age:
<input type="number" #age class="border" />
</div>
<button
(click)="showUser = true"
class="w-fit rounded-md border border-blue-500 bg-blue-200 px-4 py-2">
Submit
</button>
</div>
@if (showUser && !!name.value) {
<app-user
[name]="name.value"
[lastName]="lastName.value"
[age]="age.value" />
}
`,
host: {
class: 'p-10 block flex flex-col gap-10',
},
})
export class AppComponent {
showUser = false;
}

View File

@@ -0,0 +1,5 @@
import { ApplicationConfig } from '@angular/core';
export const appConfig: ApplicationConfig = {
providers: [],
};

View File

@@ -0,0 +1,41 @@
import { TitleCasePipe } from '@angular/common';
import {
ChangeDetectionStrategy,
Component,
Input,
OnChanges,
} 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',
standalone: true,
imports: [TitleCasePipe],
template: `
{{ 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';
ngOnChanges(): void {
this.fullName = `${this.name} ${this.lastName ?? ''}`;
this.category = ageToCategory(Number(this.age) ?? 0);
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View File

@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>angular-signal-input</title>
<base href="/" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" type="image/x-icon" href="favicon.ico" />
</head>
<body>
<app-root></app-root>
</body>
</html>

View File

@@ -0,0 +1,7 @@
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app/app.component';
import { appConfig } from './app/app.config';
bootstrapApplication(AppComponent, appConfig).catch((err) =>
console.error(err),
);

View File

@@ -0,0 +1,5 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
/* You can add global styles to this file, and also import other style files */