Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions apps/performance/34-default-vs-onpush/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import { Component } from '@angular/core';
import { randFirstName } from '@ngneat/falso';
import { PersonListComponent } from './person-list.component';
import { PersonComponent } from './person.component';
import { RandomComponent } from './random.component';

@Component({
imports: [PersonListComponent, RandomComponent],
imports: [RandomComponent, PersonComponent],
selector: 'app-root',
template: `
<app-random />

<div class="flex">
<app-person-list [names]="girlList" title="Female" />
<app-person-list [names]="boyList" title="Male" />
<app-person gender="female"></app-person>
<app-person gender="male"></app-person>
</div>
`,
})
export class AppComponent {
girlList = randFirstName({ gender: 'female', length: 10 });
boyList = randFirstName({ gender: 'male', length: 10 });
}
export class AppComponent {}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Component, input } from '@angular/core';
import { ChangeDetectionStrategy, Component, input } from '@angular/core';

import { CDFlashingDirective } from '@angular-challenges/shared/directives';
import { TitleCasePipe } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { MatChipsModule } from '@angular/material/chips';
import { MatFormFieldModule } from '@angular/material/form-field';
Expand All @@ -17,26 +16,9 @@ import { MatListModule } from '@angular/material/list';
MatInputModule,
MatChipsModule,
CDFlashingDirective,
TitleCasePipe,
],
template: `
<h1 class="text-center font-semibold" title="Title">
{{ title() | titlecase }}
</h1>

<mat-form-field class="w-4/5" cd-flash>
<input
placeholder="Add one member to the list"
matInput
type="text"
[(ngModel)]="label"
(keydown)="handleKey($event)" />
</mat-form-field>

<mat-list class="flex w-full">
@if (names()?.length === 0) {
<div class="empty-list-label">Empty list</div>
}
@for (name of names(); track name) {
<mat-list-item cd-flash class="text-orange-500">
<div class="flex justify-between">
Expand All @@ -45,26 +27,17 @@ import { MatListModule } from '@angular/material/list';
</h3>
</div>
</mat-list-item>
}
@if (names()?.length !== 0) {
<mat-divider></mat-divider>
} @empty {
<div class="empty-list-label">Empty list</div>
}
</mat-list>
`,
host: {
class: 'w-full flex flex-col items-center',
},
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class PersonListComponent {
names = input<string[]>([]);
title = input('');

label = '';

handleKey(event: KeyboardEvent) {
if (event.key === 'Enter') {
this.names()?.unshift(this.label);
this.label = '';
}
}
}
42 changes: 42 additions & 0 deletions apps/performance/34-default-vs-onpush/src/app/person.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { TitleCasePipe } from '@angular/common';
import {
ChangeDetectionStrategy,
Component,
effect,
inject,
input,
} from '@angular/core';
import { PersonListComponent } from './person-list.component';
import { PersonFormComponent } from './person.form.component';
import { PersonService } from './person.service';

@Component({
selector: 'app-person',
imports: [TitleCasePipe, PersonListComponent, PersonFormComponent],
template: `
<h1 class="text-center font-semibold" title="Title">
{{ gender() | titlecase }}
</h1>

<person-form (addLabel)="onAddLabel($event)" />
<app-person-list [names]="names()" />
`,
host: {
class: 'w-full flex flex-col items-center',
},
providers: [PersonService],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you forgot 'changedetection.onpush`

changeDetection: ChangeDetectionStrategy.OnPush,
})
export class PersonComponent {
service = inject(PersonService);
gender = input<'male' | 'female'>('male');
names = this.service.list;

constructor() {
effect(() => this.service.initList(this.gender()));
}

onAddLabel(label: string) {
this.service.add(label);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { CDFlashingDirective } from '@angular-challenges/shared/directives';
import {
ChangeDetectionStrategy,
Component,
model,
output,
} from '@angular/core';
import { FormsModule } from '@angular/forms';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';

@Component({
selector: 'person-form',
imports: [
FormsModule,
MatFormFieldModule,
MatInputModule,
CDFlashingDirective,
],
template: `
<mat-form-field class="w-4/5" cd-flash>
<input
placeholder="Add one member to the list"
matInput
type="text"
[(ngModel)]="label"
(keydown)="handleKey($event)" />
</mat-form-field>
`,
host: {
class: 'w-full flex flex-col items-center',
},
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class PersonFormComponent {
label = model('');
addLabel = output<string>();

handleKey(event: KeyboardEvent) {
if (event.key === 'Enter') {
this.addLabel.emit(this.label());
this.label.set('');
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}
16 changes: 16 additions & 0 deletions apps/performance/34-default-vs-onpush/src/app/person.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Injectable, signal } from '@angular/core';
import { randFirstName } from '@ngneat/falso';

@Injectable()
export class PersonService {
private _list = signal<string[]>([]);
list = this._list.asReadonly();

initList(gender: 'male' | 'female') {
this._list.set(randFirstName({ gender, length: 10 }));
}

add(name: string) {
this._list.update((names) => [...names, name]);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { CDFlashingDirective } from '@angular-challenges/shared/directives';
import { Component } from '@angular/core';
import { ChangeDetectionStrategy, Component } from '@angular/core';

@Component({
selector: 'app-random',
template: `
<div cd-flash>I do nothing but I'm here</div>
`,
imports: [CDFlashingDirective],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class RandomComponent {}
Loading