mirror of
https://github.com/Raghu-Ch/angular-challenges.git
synced 2026-02-12 05:43:03 -05:00
feat: challenge 43 input signal
This commit is contained in:
45
apps/angular/signal-input/src/app/app.component.ts
Normal file
45
apps/angular/signal-input/src/app/app.component.ts
Normal 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;
|
||||
}
|
||||
5
apps/angular/signal-input/src/app/app.config.ts
Normal file
5
apps/angular/signal-input/src/app/app.config.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { ApplicationConfig } from '@angular/core';
|
||||
|
||||
export const appConfig: ApplicationConfig = {
|
||||
providers: [],
|
||||
};
|
||||
41
apps/angular/signal-input/src/app/user.component.ts
Normal file
41
apps/angular/signal-input/src/app/user.component.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
0
apps/angular/signal-input/src/assets/.gitkeep
Normal file
0
apps/angular/signal-input/src/assets/.gitkeep
Normal file
BIN
apps/angular/signal-input/src/favicon.ico
Normal file
BIN
apps/angular/signal-input/src/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
13
apps/angular/signal-input/src/index.html
Normal file
13
apps/angular/signal-input/src/index.html
Normal 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>
|
||||
7
apps/angular/signal-input/src/main.ts
Normal file
7
apps/angular/signal-input/src/main.ts
Normal 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),
|
||||
);
|
||||
5
apps/angular/signal-input/src/styles.scss
Normal file
5
apps/angular/signal-input/src/styles.scss
Normal 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 */
|
||||
Reference in New Issue
Block a user