UNPKG

primeng

Version:

PrimeNG is an open source UI library for Angular featuring a rich set of 80+ components, a theme designer, various theme alternatives such as Material, Bootstrap, Tailwind, premium templates and professional support. In addition, it integrates with PrimeB

389 lines (383 loc) 17.6 kB
import * as i1 from '@angular/common'; import { CommonModule } from '@angular/common'; import * as i0 from '@angular/core'; import { Injectable, forwardRef, EventEmitter, signal, inject, booleanAttribute, numberAttribute, ContentChildren, ContentChild, Output, Input, ViewEncapsulation, ChangeDetectionStrategy, Component, NgModule } from '@angular/core'; import { NG_VALUE_ACCESSOR } from '@angular/forms'; import { uuid, getFirstFocusableElement, focus } from '@primeuix/utils'; import { SharedModule, PrimeTemplate } from 'primeng/api'; import { AutoFocus } from 'primeng/autofocus'; import { BaseEditableHolder } from 'primeng/baseeditableholder'; import { StarFillIcon, StarIcon } from 'primeng/icons'; import { style } from '@primeuix/styles/rating'; import { BaseStyle } from 'primeng/base'; const theme = /*css*/ ` ${style} /* For PrimeNG */ p-rating.ng-invalid.ng-dirty > .p-rating > .p-rating-icon { stroke: dt('rating.invalid.icon.color'); } `; const classes = { root: ({ instance }) => [ 'p-rating', { 'p-readonly': instance.readonly, 'p-disabled': instance.$disabled() } ], option: ({ instance, star, value }) => [ 'p-rating-option', { 'p-rating-option-active': star + 1 <= value, 'p-focus-visible': star + 1 === instance.focusedOptionIndex() && instance.isFocusVisibleItem } ], onIcon: ({ instance }) => ['p-rating-icon p-rating-on-icon', { 'p-invalid': instance.invalid() }], offIcon: ({ instance }) => ['p-rating-icon p-rating-off-icon', { 'p-invalid': instance.invalid() }] }; class RatingStyle extends BaseStyle { name = 'rating'; theme = theme; classes = classes; static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.3", ngImport: i0, type: RatingStyle, deps: null, target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.1.3", ngImport: i0, type: RatingStyle }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.3", ngImport: i0, type: RatingStyle, decorators: [{ type: Injectable }] }); /** * * Rating component is a star based selection input. * * [Live Demo](https://www.primeng.org/rating/) * * @module ratingstyle * */ var RatingClasses; (function (RatingClasses) { /** * Class name of the root element */ RatingClasses["root"] = "p-rating"; /** * Class name of the option element */ RatingClasses["option"] = "p-rating-option"; /** * Class name of the on icon element */ RatingClasses["onIcon"] = "p-rating-on-icon"; /** * Class name of the off icon element */ RatingClasses["offIcon"] = "p-rating-off-icon"; })(RatingClasses || (RatingClasses = {})); const RATING_VALUE_ACCESSOR = { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => Rating), multi: true }; /** * Rating is an extension to standard radio button element with theming. * @group Components */ class Rating extends BaseEditableHolder { /** * When present, changing the value is not possible. * @group Props */ readonly; /** * Number of stars. * @group Props */ stars = 5; /** * Style class of the on icon. * @group Props */ iconOnClass; /** * Inline style of the on icon. * @group Props */ iconOnStyle; /** * Style class of the off icon. * @group Props */ iconOffClass; /** * Inline style of the off icon. * @group Props */ iconOffStyle; /** * When present, it specifies that the component should automatically get focus on load. * @group Props */ autofocus; /** * Emitted on value change. * @param {RatingRateEvent} value - Custom rate event. * @group Emits */ onRate = new EventEmitter(); /** * Emitted when the rating receives focus. * @param {Event} value - Browser event. * @group Emits */ onFocus = new EventEmitter(); /** * Emitted when the rating loses focus. * @param {Event} value - Browser event. * @group Emits */ onBlur = new EventEmitter(); /** * Custom on icon template. * @group Templates */ onIconTemplate; /** * Custom off icon template. * @group Templates */ offIconTemplate; templates; value; starsArray; isFocusVisibleItem = true; focusedOptionIndex = signal(-1, ...(ngDevMode ? [{ debugName: "focusedOptionIndex" }] : [])); nameattr; _componentStyle = inject(RatingStyle); _onIconTemplate; _offIconTemplate; ngOnInit() { super.ngOnInit(); this.nameattr = this.nameattr || uuid('pn_id_'); this.starsArray = []; for (let i = 0; i < this.stars; i++) { this.starsArray[i] = i; } } ngAfterContentInit() { this.templates.forEach((item) => { switch (item.getType()) { case 'onicon': this._onIconTemplate = item.template; break; case 'officon': this._offIconTemplate = item.template; break; } }); } onOptionClick(event, value) { if (!this.readonly && !this.$disabled()) { this.onOptionSelect(event, value); this.isFocusVisibleItem = false; const firstFocusableEl = getFirstFocusableElement(event.currentTarget, ''); firstFocusableEl && focus(firstFocusableEl); } } onOptionSelect(event, value) { if (!this.readonly && !this.$disabled()) { if (this.focusedOptionIndex() === value || value === this.value) { this.focusedOptionIndex.set(-1); this.updateModel(event, null); } else { this.focusedOptionIndex.set(value); this.updateModel(event, value || null); } } } onChange(event, value) { this.onOptionSelect(event, value); this.isFocusVisibleItem = true; } onInputBlur(event) { this.focusedOptionIndex.set(-1); this.onBlur.emit(event); } onInputFocus(event, value) { if (!this.readonly && !this.$disabled()) { this.focusedOptionIndex.set(value); this.isFocusVisibleItem = event.sourceCapabilities?.firesTouchEvents === false; this.onFocus.emit(event); } } updateModel(event, value) { this.writeValue(value); this.onModelChange(this.value); this.onModelTouched(); this.onRate.emit({ originalEvent: event, value }); } starAriaLabel(value) { return value === 1 ? this.config.translation.aria.star : this.config.translation.aria.stars.replace(/{star}/g, value); } getIconTemplate(i) { return !this.value || i >= this.value ? this.offIconTemplate || this._offIconTemplate : this.onIconTemplate || this.offIconTemplate; } /** * @override * * @see {@link BaseEditableHolder.writeControlValue} * Writes the value to the control. */ writeControlValue(value, setModelValue) { this.value = value; setModelValue(value); } get isCustomIcon() { return !!(this.onIconTemplate || this._onIconTemplate || this.offIconTemplate || this._offIconTemplate); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.3", ngImport: i0, type: Rating, deps: null, target: i0.ɵɵFactoryTarget.Component }); static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "20.1.3", type: Rating, isStandalone: true, selector: "p-rating", inputs: { readonly: ["readonly", "readonly", booleanAttribute], stars: ["stars", "stars", numberAttribute], iconOnClass: "iconOnClass", iconOnStyle: "iconOnStyle", iconOffClass: "iconOffClass", iconOffStyle: "iconOffStyle", autofocus: ["autofocus", "autofocus", booleanAttribute] }, outputs: { onRate: "onRate", onFocus: "onFocus", onBlur: "onBlur" }, host: { properties: { "class": "cx('root')", "attr.data-pc-name": "'rating'", "attr.data-pc-section": "'root'" } }, providers: [RATING_VALUE_ACCESSOR, RatingStyle], queries: [{ propertyName: "onIconTemplate", first: true, predicate: ["onicon"] }, { propertyName: "offIconTemplate", first: true, predicate: ["officon"] }, { propertyName: "templates", predicate: PrimeTemplate }], usesInheritance: true, ngImport: i0, template: ` <ng-template ngFor [ngForOf]="starsArray" let-star let-i="index"> <div [class]="cx('option', { star, value })" (click)="onOptionClick($event, star + 1)"> <span class="p-hidden-accessible" [attr.data-p-hidden-accessible]="true"> <input type="radio" [value]="star + 1" [attr.name]="name() || nameattr + '_name'" [attr.value]="modelValue()" [attr.required]="required() ? '' : undefined" [attr.readonly]="readonly ? '' : undefined" [attr.disabled]="$disabled() ? '' : undefined" [checked]="value === star + 1" [attr.aria-label]="starAriaLabel(star + 1)" (focus)="onInputFocus($event, star + 1)" (blur)="onInputBlur($event)" (change)="onChange($event, star + 1)" [pAutoFocus]="autofocus" /> </span> @if (star + 1 <= value) { @if (onIconTemplate || _onIconTemplate) { <ng-container *ngTemplateOutlet="onIconTemplate || _onIconTemplate; context: { $implicit: star + 1, class: cx('onIcon') }"></ng-container> } @else { <span [class]="cx('onIcon')" *ngIf="iconOnClass" [ngStyle]="iconOnStyle" [ngClass]="iconOnClass" [attr.data-pc-section]="'onIcon'"></span> <svg data-p-icon="star-fill" *ngIf="!iconOnClass" [ngStyle]="iconOnStyle" [class]="cx('onIcon')" [attr.data-pc-section]="'onIcon'" /> } } @else { @if (offIconTemplate || _offIconTemplate) { <ng-container *ngTemplateOutlet="offIconTemplate || _offIconTemplate; context: { $implicit: star + 1, class: cx('offIcon') }"></ng-container> } @else { <span [class]="cx('offIcon')" *ngIf="iconOffClass" [ngStyle]="iconOffStyle" [ngClass]="iconOffClass" [attr.data-pc-section]="'offIcon'"></span> <svg data-p-icon="star" *ngIf="!iconOffClass" [ngStyle]="iconOffStyle" [class]="cx('offIcon')" [attr.data-pc-section]="'offIcon'" /> } } </div> </ng-template> `, isInline: true, dependencies: [{ kind: "ngmodule", type: CommonModule }, { kind: "directive", type: i1.NgClass, selector: "[ngClass]", inputs: ["class", "ngClass"] }, { kind: "directive", type: i1.NgForOf, selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }, { kind: "directive", type: i1.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: i1.NgTemplateOutlet, selector: "[ngTemplateOutlet]", inputs: ["ngTemplateOutletContext", "ngTemplateOutlet", "ngTemplateOutletInjector"] }, { kind: "directive", type: i1.NgStyle, selector: "[ngStyle]", inputs: ["ngStyle"] }, { kind: "directive", type: AutoFocus, selector: "[pAutoFocus]", inputs: ["pAutoFocus"] }, { kind: "component", type: StarFillIcon, selector: "[data-p-icon=\"star-fill\"]" }, { kind: "component", type: StarIcon, selector: "[data-p-icon=\"star\"]" }, { kind: "ngmodule", type: SharedModule }], changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.3", ngImport: i0, type: Rating, decorators: [{ type: Component, args: [{ selector: 'p-rating', imports: [CommonModule, AutoFocus, StarFillIcon, StarIcon, SharedModule], standalone: true, template: ` <ng-template ngFor [ngForOf]="starsArray" let-star let-i="index"> <div [class]="cx('option', { star, value })" (click)="onOptionClick($event, star + 1)"> <span class="p-hidden-accessible" [attr.data-p-hidden-accessible]="true"> <input type="radio" [value]="star + 1" [attr.name]="name() || nameattr + '_name'" [attr.value]="modelValue()" [attr.required]="required() ? '' : undefined" [attr.readonly]="readonly ? '' : undefined" [attr.disabled]="$disabled() ? '' : undefined" [checked]="value === star + 1" [attr.aria-label]="starAriaLabel(star + 1)" (focus)="onInputFocus($event, star + 1)" (blur)="onInputBlur($event)" (change)="onChange($event, star + 1)" [pAutoFocus]="autofocus" /> </span> @if (star + 1 <= value) { @if (onIconTemplate || _onIconTemplate) { <ng-container *ngTemplateOutlet="onIconTemplate || _onIconTemplate; context: { $implicit: star + 1, class: cx('onIcon') }"></ng-container> } @else { <span [class]="cx('onIcon')" *ngIf="iconOnClass" [ngStyle]="iconOnStyle" [ngClass]="iconOnClass" [attr.data-pc-section]="'onIcon'"></span> <svg data-p-icon="star-fill" *ngIf="!iconOnClass" [ngStyle]="iconOnStyle" [class]="cx('onIcon')" [attr.data-pc-section]="'onIcon'" /> } } @else { @if (offIconTemplate || _offIconTemplate) { <ng-container *ngTemplateOutlet="offIconTemplate || _offIconTemplate; context: { $implicit: star + 1, class: cx('offIcon') }"></ng-container> } @else { <span [class]="cx('offIcon')" *ngIf="iconOffClass" [ngStyle]="iconOffStyle" [ngClass]="iconOffClass" [attr.data-pc-section]="'offIcon'"></span> <svg data-p-icon="star" *ngIf="!iconOffClass" [ngStyle]="iconOffStyle" [class]="cx('offIcon')" [attr.data-pc-section]="'offIcon'" /> } } </div> </ng-template> `, providers: [RATING_VALUE_ACCESSOR, RatingStyle], changeDetection: ChangeDetectionStrategy.OnPush, encapsulation: ViewEncapsulation.None, host: { '[class]': "cx('root')", '[attr.data-pc-name]': "'rating'", '[attr.data-pc-section]': "'root'" } }] }], propDecorators: { readonly: [{ type: Input, args: [{ transform: booleanAttribute }] }], stars: [{ type: Input, args: [{ transform: numberAttribute }] }], iconOnClass: [{ type: Input }], iconOnStyle: [{ type: Input }], iconOffClass: [{ type: Input }], iconOffStyle: [{ type: Input }], autofocus: [{ type: Input, args: [{ transform: booleanAttribute }] }], onRate: [{ type: Output }], onFocus: [{ type: Output }], onBlur: [{ type: Output }], onIconTemplate: [{ type: ContentChild, args: ['onicon', { descendants: false }] }], offIconTemplate: [{ type: ContentChild, args: ['officon', { descendants: false }] }], templates: [{ type: ContentChildren, args: [PrimeTemplate] }] } }); class RatingModule { static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.3", ngImport: i0, type: RatingModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "20.1.3", ngImport: i0, type: RatingModule, imports: [Rating, SharedModule], exports: [Rating, SharedModule] }); static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "20.1.3", ngImport: i0, type: RatingModule, imports: [Rating, SharedModule, SharedModule] }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.3", ngImport: i0, type: RatingModule, decorators: [{ type: NgModule, args: [{ imports: [Rating, SharedModule], exports: [Rating, SharedModule] }] }] }); /** * Generated bundle index. Do not edit. */ export { RATING_VALUE_ACCESSOR, Rating, RatingClasses, RatingModule, RatingStyle }; //# sourceMappingURL=primeng-rating.mjs.map