Skip to content

Commit

Permalink
refactor: use HostBinding
Browse files Browse the repository at this point in the history
  • Loading branch information
edbzn committed Oct 24, 2023
1 parent cfe8686 commit bb84281
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
18 changes: 9 additions & 9 deletions projects/todo-mvc/src/app/todo-list.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { ChangeDetectionStrategy, Component, HostBinding, inject } from '@angular/core';
import {
ChangeDetectionStrategy,
Component,
HostBinding,
inject,
} from '@angular/core';
import { FormControl, ReactiveFormsModule } from '@angular/forms';
import { RxLet } from '@rx-angular/template/let';
import { TodoComponent } from './todo.component';
Expand All @@ -8,11 +13,7 @@ import { TodoService } from './todo.service';
standalone: true,
selector: 'app-todo-list',
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [
ReactiveFormsModule,
RxLet,
TodoComponent,
],
imports: [ReactiveFormsModule, RxLet, TodoComponent],
providers: [TodoService],
styles: [
`
Expand Down Expand Up @@ -40,17 +41,16 @@ import { TodoService } from './todo.service';
(click)="todoService.actions.toggleAll()"
/>
<label for="toggle-all">Mark all as complete</label>
<ng-container *rxLet="todoService.filteredTodos$; let todos">
<section class="todo-list" *rxLet="todoService.filteredTodos$; let todos">
@for (todo of todos; track todo.id; let i = $index) {
<app-todo
class="todo-list"
[attr.data-uf]="'todo-' + i"
[todo]="todo"
(update)="todoService.actions.update($event)"
(remove)="todoService.actions.remove($event)"
/>
}
</ng-container>
</section>
</section>
<footer class="footer" *rxLet="todoService.filter$; let filter">
<span class="todo-count">
Expand Down
18 changes: 11 additions & 7 deletions projects/todo-mvc/src/app/todo.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ChangeDetectorRef,
Component,
ElementRef,
HostBinding,
Input,
Output,
ViewChild,
Expand All @@ -20,11 +21,7 @@ import { Todo } from './todo.service';
selector: 'app-todo',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<article
class="todo"
[class]="{ completed: todo.done, editing: isEditing }"
>
@if (isEditing) {
@if (isEditing) {
<input
#input
class="edit"
Expand All @@ -44,8 +41,7 @@ import { Todo } from './todo.service';
<label (dblclick)="edit()">{{ todo.text }}</label>
<button class="destroy" (click)="destroy()"></button>
</div>
}
</article>
}
`,
})
export class TodoComponent {
Expand All @@ -59,6 +55,14 @@ export class TodoComponent {
@ViewChild('input') input?: ElementRef<HTMLInputElement>;
@ViewChild('toggle') toggle?: ElementRef<HTMLInputElement>;

@HostBinding('class.todo') readonly hostClass = true;
@HostBinding('class.completed') get completed(): boolean {
return this.todo.done;
}
@HostBinding('class.editing') get editing(): boolean {
return this.isEditing;
}

@Input({ required: true })
set todo(todo: Todo) {
this.state.set({ todo });
Expand Down

0 comments on commit bb84281

Please sign in to comment.