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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
[formControl]="feedbackForm.controls.email"
placeholder="Email"
type="email" />
<app-rating-control (ratingUpdated)="rating = $event"></app-rating-control>
<app-rating-control [formControl]="feedbackForm.controls.rating"></app-rating-control>
<textarea
class="feedback-form-control"
[formControl]="feedbackForm.controls.comment"
Expand All @@ -22,7 +22,7 @@
class="feedback-form-submit"
type="submit"
[disabled]="
!feedbackForm.touched || rating === null || feedbackForm.invalid
!feedbackForm.touched || feedbackForm.invalid
">
Submit
</button>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, EventEmitter, Output } from '@angular/core';
import { Component, output } from '@angular/core';
import {
FormControl,
FormGroup,
Expand All @@ -14,9 +14,7 @@ import { RatingControlComponent } from '../rating-control/rating-control.compone
styleUrls: ['feedback-form.component.scss'],
})
export class FeedbackFormComponent {
@Output()
readonly feedBackSubmit: EventEmitter<Record<string, string | null>> =
new EventEmitter<Record<string, string | null>>();
feedBackSubmit = output<Record<string, string | null>>();

readonly feedbackForm = new FormGroup({
name: new FormControl('', {
Expand All @@ -25,17 +23,14 @@ export class FeedbackFormComponent {
email: new FormControl('', {
validators: Validators.required,
}),
rating: new FormControl('', {
validators: Validators.required,
}),
comment: new FormControl(),
});

rating: string | null = null;

submitForm(): void {
this.feedBackSubmit.emit({
...this.feedbackForm.value,
rating: this.rating,
});

this.feedBackSubmit.emit(this.feedbackForm.value);
this.feedbackForm.reset();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</symbol>
</svg>
<div class="rating">
@for (item of [].constructor(5); track item) {
@for (item of [].constructor(5); track $index) {
<svg
class="star"
[class.star-active]="isStarActive($index, value)"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
import { Component, EventEmitter, Output } from '@angular/core';
import { Component } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';

@Component({
selector: 'app-rating-control',
templateUrl: 'rating-control.component.html',
styleUrls: ['rating-control.component.scss'],
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: RatingControlComponent,
multi: true,
},
],
})
export class RatingControlComponent {
@Output()
readonly ratingUpdated: EventEmitter<string> = new EventEmitter<string>();
export class RatingControlComponent implements ControlValueAccessor {
onChange = (_rating: string) => {};
onTouched = () => {};

writeValue(obj: string): void {
this.value = parseInt(obj);
}
registerOnChange(fn: any): void {
this.onChange = fn;
}
registerOnTouched(fn: any): void {
this.onTouched = fn;
}

value: number | null = null;

setRating(index: number): void {
this.value = index + 1;
this.ratingUpdated.emit(`${this.value}`);
this.onChange(`${this.value}`);
Copy link
Owner

Choose a reason for hiding this comment

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

yes perfect but looking forward using signal form where it will be a lot easier 🔥

Copy link
Author

Choose a reason for hiding this comment

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

Me too. IMO ControlValueAccessor been always too complicated

}

isStarActive(index: number, value: number | null): boolean {
Expand Down