-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from actionanand/features/2-reactive-form
Features/2 reactive form
- Loading branch information
Showing
8 changed files
with
219 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/app/control-value-accessor/rating/rating.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<!-- eslint-disable @angular-eslint/template/mouse-events-have-key-events --> | ||
<i>{{ displayText }}</i> | ||
|
||
<div class="stars" [ngClass]="{ disabled: disabled }"> | ||
@for (star of ratings; track star.stars) { | ||
<svg | ||
title="{{ star.text }}" | ||
height="25" | ||
width="23" | ||
class="star rating" | ||
[ngClass]="{ selected: star.stars <= starVal }" | ||
(mouseover)="displayText = !disabled ? star.text : ''" | ||
(mouseout)="displayText = ratingText ? ratingText : ''" | ||
(click)="setRating(star)"> | ||
<polygon points="9.9, 1.1, 3.3, 21.78, 19.8, 8.58, 0, 8.58, 16.5, 21.78" style="fill-rule: nonzero" /> | ||
</svg> | ||
} | ||
</div> | ||
|
||
<!-- <div class="stars" [ngClass]="{'disabled': disabled}"> | ||
<ng-container *ngFor="let star of ratings" > | ||
<svg title="{{star.text}}" | ||
height="25" width="23" class="star rating" [ngClass]="{'selected': star.stars <= starVal}" | ||
(mouseover)="displayText = !disabled ? star.text : ''" | ||
(mouseout)="displayText = ratingText ? ratingText : ''" | ||
(click)="setRating(star)"> | ||
<polygon points="9.9, 1.1, 3.3, 21.78, 19.8, 8.58, 0, 8.58, 16.5, 21.78" style="fill-rule:nonzero;"/> | ||
</svg> | ||
</ng-container> | ||
</div> --> |
37 changes: 37 additions & 0 deletions
37
src/app/control-value-accessor/rating/rating.component.less
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
.stars { | ||
cursor: pointer; | ||
|
||
&:hover { | ||
.star polygon { | ||
fill: #ffd055 !important; | ||
} | ||
} | ||
&.disabled:hover { | ||
cursor: not-allowed; | ||
.star { | ||
polygon { | ||
fill: #d8d8d8 !important; | ||
} | ||
} | ||
} | ||
|
||
.star { | ||
float: left; | ||
margin: 0px 5px; | ||
|
||
polygon { | ||
fill: #d8d8d8; | ||
} | ||
|
||
&:hover ~ .star { | ||
polygon { | ||
fill: #d8d8d8 !important; | ||
} | ||
} | ||
&.selected { | ||
polygon { | ||
fill: #ffd055 !important; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { NgClass } from '@angular/common'; | ||
import { Component, forwardRef } from '@angular/core'; | ||
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; | ||
|
||
@Component({ | ||
selector: 'app-rating', | ||
standalone: true, | ||
imports: [NgClass], | ||
templateUrl: './rating.component.html', | ||
styleUrl: './rating.component.less', | ||
providers: [ | ||
{ | ||
provide: NG_VALUE_ACCESSOR, | ||
useExisting: forwardRef(() => RatingComponent), | ||
multi: true, | ||
}, | ||
], | ||
}) | ||
export class RatingComponent implements ControlValueAccessor { | ||
protected readonly ratings = [ | ||
{ | ||
stars: 1, | ||
text: 'Feeling Sad 😔', | ||
}, | ||
{ | ||
stars: 2, | ||
text: 'Feeling Meh ☹️', | ||
}, | ||
{ | ||
stars: 3, | ||
text: 'Feeling Ok 🙂', | ||
}, | ||
{ | ||
stars: 4, | ||
text: 'Feeling Good 😀', | ||
}, | ||
{ | ||
stars: 5, | ||
text: 'Feeling Awesome 😍', | ||
}, | ||
]; | ||
|
||
disabled = false; | ||
ratingText = ''; | ||
displayText = ''; | ||
starVal!: number; | ||
|
||
private onChanged: any = () => {}; | ||
private onTouched: any = () => {}; | ||
|
||
writeValue(val: number) { | ||
this.starVal = val; | ||
} | ||
|
||
registerOnChange(fn: any) { | ||
this.onChanged = fn; | ||
} | ||
|
||
registerOnTouched(fn: any) { | ||
this.onTouched = fn; | ||
} | ||
|
||
setDisabledState(isDisabled: boolean): void { | ||
this.disabled = isDisabled; | ||
} | ||
|
||
setRating(star: { stars: number; text: string }) { | ||
if (!this.disabled) { | ||
this.starVal = star.stars; | ||
this.ratingText = star.text; | ||
this.onChanged(star.stars); | ||
this.onTouched(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters