projects/storefrontlib/shared/components/star-rating/star-rating.component.ts
Star rating component can be used to view existing ratings as well as create new ratings. The component can be used for any ratings.
changeDetection | ChangeDetectionStrategy.OnPush |
selector | cx-star-rating |
templateUrl | ./star-rating.component.html |
Properties |
|
Methods |
Inputs |
Outputs |
HostListeners |
disabled | |
Type : boolean
|
|
Default value : true
|
|
The rating component can be used in disabled mode, so that the interaction is not provided. Defaults to true. |
rating | |
Type : number
|
|
Default value : this.initialRate
|
|
The rating is used to color the rating stars. It can have a precise number. The rating number is used for a CSS custom property (AKA css variable) value. The actually coloring is done in CSS. |
change | |
Type : EventEmitter
|
|
Emits the given rating when the user clicks on a star. |
mouseout |
mouseout()
|
reset |
reset()
|
Decorators :
@HostListener('mouseout')
|
Returns :
void
|
saveRate | ||||||
saveRate(rating: number)
|
||||||
Parameters :
Returns :
void
|
setRate | ||||||
setRate(value: number)
|
||||||
Parameters :
Returns :
void
|
change |
Default value : new EventEmitter<number>()
|
Decorators :
@Output()
|
Emits the given rating when the user clicks on a star. |
disabled |
Default value : true
|
Decorators :
@Input()
|
The rating component can be used in disabled mode, so that the interaction is not provided. Defaults to true. |
icon |
Default value : ICON_TYPE.STAR
|
Protected initialRate |
Type : number
|
Default value : 0
|
rating |
Type : number
|
Default value : this.initialRate
|
Decorators :
@Input()
|
The rating is used to color the rating stars. It can have a precise number. The rating number is used for a CSS custom property (AKA css variable) value. The actually coloring is done in CSS. |
import {
ChangeDetectionStrategy,
Component,
EventEmitter,
HostBinding,
HostListener,
Input,
Output,
} from '@angular/core';
import { ICON_TYPE } from '../../../cms-components/misc/index';
/**
* Star rating component can be used to view existing ratings as well
* as create new ratings. The component can be used for any ratings.
*/
@Component({
selector: 'cx-star-rating',
templateUrl: './star-rating.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class StarRatingComponent {
protected initialRate = 0;
icon = ICON_TYPE.STAR;
/**
* The rating component can be used in disabled mode,
* so that the interaction is not provided.
*
* Defaults to true.
*/
@Input() @HostBinding('attr.disabled') disabled = true;
/**
* The rating is used to color the rating stars. It can have a
* precise number. The rating number is used for a CSS custom property
* (AKA css variable) value. The actually coloring is done in CSS.
*/
@Input()
@HostBinding('style.--star-fill')
rating: number = this.initialRate;
/**
* Emits the given rating when the user clicks on a star.
*/
// eslint-disable-next-line @angular-eslint/no-output-native
@Output() change = new EventEmitter<number>();
setRate(value: number): void {
if (this.disabled) {
return;
}
this.rating = value;
}
@HostListener('mouseout')
reset() {
if (this.disabled) {
return;
}
this.rating = this.initialRate ?? 0;
}
saveRate(rating: number): void {
if (this.disabled) {
return;
}
this.initialRate = rating;
this.setRate(rating);
this.change.emit(rating);
}
}
<cx-icon
*ngFor="let i of [1, 2, 3, 4, 5]"
[type]="icon"
class="star"
(mouseover)="setRate(i)"
(click)="saveRate(i)"
(keydown.space)="saveRate(i)"
[attr.tabindex]="disabled ? null : 0"
></cx-icon>