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
Expand Up @@ -26,14 +26,14 @@ export class FeedbackFormComponent {
validators: Validators.required,
}),
comment: new FormControl(),
rating: new FormControl(null, {
validators: Validators.required,
}),
});

rating: string | null = null;

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

this.feedbackForm.reset();
Expand Down
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, forwardRef } 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: forwardRef(() => RatingControlComponent),
multi: true,
},
],
})
export class RatingControlComponent {
@Output()
readonly ratingUpdated: EventEmitter<string> = new EventEmitter<string>();
export class RatingControlComponent implements ControlValueAccessor {
onChange: (value: number | null) => void = () => {};
onTouched: () => void = () => {};

value: number | null = null;

writeValue(value: number | null): void {
this.value = value;
}
registerOnChange(fn: any): void {
this.onChange = fn;
}
registerOnTouched(fn: any): void {
this.onTouched = fn;
}

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

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