@goussama/ionic-rating-component
Version:
Highly customizable ionic 6 component to display evaluations or a quick rating operation of something.
123 lines (110 loc) • 3.79 kB
text/typescript
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { IonicRatingService } from './ionic-rating.service';
export class IonicRatingComponent implements ControlValueAccessor, OnInit {
public set rating(val: number) {
this._rating = val;
if (this.onChange) {
this.onChange(val);
}
}
public get rating(): number {
return this._rating;
}
constructor(private ionicRatingService: IonicRatingService) {
this.Math = Math;
this.parseFloat = parseFloat;
}
private _rating: number;
private onChange: any;
private onTouched: any;
public disabled: boolean;
Math: any;
parseFloat: any;
iconsArray: number[] = [];
ratingChanged: EventEmitter<number> = new EventEmitter<number>();
readonly: string = 'false';
activeColor: string = '#488aff';
defaultColor: string = '#aaaaaa';
activeIcon: string = 'star';
defaultIcon: string = 'star-outline';
halfIcon: string = 'star-half';
halfStar: string = 'false';
maxRating: number = 5;
fontSize: string = '28px';
public readonly eventInfo = (() => {
const id = new Date().getTime();
const topic = `star-rating:${id}:changed`;
return {
topic,
};
})();
ngOnInit(): void {
this.rating = this.rating || 3; //default after input`s initialization
for (let i = 0; i < this.maxRating; i++) {
this.iconsArray.push(i);
}
}
writeValue(obj: number): void {
this.rating = obj;
}
registerOnChange(fn: any): void {
this.onChange = fn;
}
registerOnTouched(fn: any): void {
this.onTouched = fn;
}
setDisabledState?(isDisabled: boolean): void {
this.readonly = isDisabled ? "true" : "false";
}
changeRating(event) {
if (this.readonly && this.readonly === 'true') return;
// event is different for firefox and chrome
let id = event.target.id ? parseInt(event.target.id) : parseInt(event.target.parentElement.id);
if (this.halfStar && this.halfStar === 'true') {
this.rating = ((this.rating - id > 0) && (this.rating - id <= 0.5)) ? id + 1 : id + .5;
} else {
this.rating = id + 1;
}
// subscribe this event to get the changed value in your parent compoanent
this.ionicRatingService.publishStartRatingChanged(this.rating);
this.ionicRatingService.publishTopic(this.eventInfo.topic, this.rating);
// unique event
this.ratingChanged.emit(this.rating);
}
}