Skip to content

Answer:41 rating cva #1327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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,17 +13,15 @@
[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"
placeholder="Сomment text"></textarea>
<button
class="feedback-form-submit"
type="submit"
[disabled]="
!feedbackForm.touched || rating === null || feedbackForm.invalid
">
[disabled]="feedbackForm.invalid">
Submit
</button>
</form>
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,16 @@ 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,
});
if (this.feedbackForm.invalid) return;

this.feedBackSubmit.emit(this.feedbackForm.value);
this.feedbackForm.reset();
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,54 @@
import { Component, EventEmitter, Output } from '@angular/core';
import { Component, forwardRef } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';

// eslint-disable-next-line @typescript-eslint/no-empty-function
function noop() {}

@Component({
standalone: true,
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 {
protected isDisabled = false;

protected value: number | null = null;

private _onChange: (value: string) => void = noop;
private _onTouch = noop;

value: number | null = null;
writeValue(value: number | null): void {
this.value = value;
}

registerOnChange(fn: (value: string) => void): void {
this._onChange = fn;
}

registerOnTouched(fn: typeof noop): void {
this._onTouch = fn;
}

setDisabledState(isDisabled: boolean): void {
this.isDisabled = isDisabled;
}

setRating(index: number): void {
if (this.isDisabled) {
return;
}

this.value = index + 1;
this.ratingUpdated.emit(`${this.value}`);
this._onTouch();
this._onChange(this.value.toString());
}

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