primeng
Version:
PrimeNG is a premium UI library for Angular featuring a rich set of 90+ components, a theme designer, various theme alternatives such as Material, Bootstrap, Tailwind, premium templates and professional support. In addition, it integrates with PrimeBlock,
350 lines (344 loc) • 20 kB
JavaScript
export * from 'primeng/types/togglebutton';
import * as i0 from '@angular/core';
import { Injectable, InjectionToken, forwardRef, inject, input, numberAttribute, booleanAttribute, output, contentChild, signal, computed, HostListener, ChangeDetectionStrategy, Component, NgModule } from '@angular/core';
import { style as style$1 } from '@primeuix/styles/togglebutton';
import { BaseStyle } from 'primeng/base';
import { NgTemplateOutlet } from '@angular/common';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import { SharedModule } from 'primeng/api';
import { PARENT_INSTANCE } from 'primeng/basecomponent';
import { BaseEditableHolder } from 'primeng/baseeditableholder';
import * as i2 from 'primeng/bind';
import { Bind, BindModule } from 'primeng/bind';
import * as i1 from 'primeng/ripple';
import { Ripple } from 'primeng/ripple';
const style = /*css*/ `
${style$1}
/* For PrimeNG (iconPos) */
.p-togglebutton-icon-right {
order: 1;
}
`;
const classes = {
root: ({ instance }) => [
'p-togglebutton p-component',
{
'p-togglebutton-checked': instance.checked(),
'p-invalid': instance.invalid(),
'p-disabled': instance.$disabled(),
'p-togglebutton-sm p-inputfield-sm': instance.size() === 'small',
'p-togglebutton-lg p-inputfield-lg': instance.size() === 'large',
'p-togglebutton-fluid': instance.fluid()
}
],
content: 'p-togglebutton-content',
icon: 'p-togglebutton-icon',
iconLeft: 'p-togglebutton-icon-left',
iconRight: 'p-togglebutton-icon-right',
label: 'p-togglebutton-label'
};
class ToggleButtonStyle extends BaseStyle {
name = 'togglebutton';
style = style;
classes = classes;
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: ToggleButtonStyle, deps: null, target: i0.ɵɵFactoryTarget.Injectable });
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: ToggleButtonStyle });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: ToggleButtonStyle, decorators: [{
type: Injectable
}] });
/**
*
* ToggleButton is used to select a boolean value using a button.
*
* [Live Demo](https://www.primeng.org/togglebutton/)
*
* @module togglebuttonstyle
*
*/
var ToggleButtonClasses;
(function (ToggleButtonClasses) {
/**
* Class name of the root element
*/
ToggleButtonClasses["root"] = "p-togglebutton";
/**
* Class name of the icon element
*/
ToggleButtonClasses["icon"] = "p-togglebutton-icon";
/**
* Class name of the left icon
*/
ToggleButtonClasses["iconLeft"] = "p-togglebutton-icon-left";
/**
* Class name of the right icon
*/
ToggleButtonClasses["iconRight"] = "p-togglebutton-icon-right";
/**
* Class name of the label element
*/
ToggleButtonClasses["label"] = "p-togglebutton-label";
})(ToggleButtonClasses || (ToggleButtonClasses = {}));
const TOGGLEBUTTON_INSTANCE = new InjectionToken('TOGGLEBUTTON_INSTANCE');
const TOGGLEBUTTON_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => ToggleButton),
multi: true
};
/**
* ToggleButton is used to select a boolean value using a button.
* @group Components
*/
class ToggleButton extends BaseEditableHolder {
componentName = 'ToggleButton';
$pcToggleButton = inject(TOGGLEBUTTON_INSTANCE, { optional: true, skipSelf: true }) ?? undefined;
bindDirectiveInstance = inject(Bind, { self: true });
_componentStyle = inject(ToggleButtonStyle);
/**
* Label for the on state.
* @group Props
*/
onLabel = input('Yes', /* @ts-ignore */
...(ngDevMode ? [{ debugName: "onLabel" }] : /* istanbul ignore next */ []));
/**
* Label for the off state.
* @group Props
*/
offLabel = input('No', /* @ts-ignore */
...(ngDevMode ? [{ debugName: "offLabel" }] : /* istanbul ignore next */ []));
/**
* Icon for the on state.
* @group Props
*/
onIcon = input(/* @ts-ignore */
...(ngDevMode ? [undefined, { debugName: "onIcon" }] : /* istanbul ignore next */ []));
/**
* Icon for the off state.
* @group Props
*/
offIcon = input(/* @ts-ignore */
...(ngDevMode ? [undefined, { debugName: "offIcon" }] : /* istanbul ignore next */ []));
/**
* Defines a string that labels the input for accessibility.
* @group Props
*/
ariaLabel = input(/* @ts-ignore */
...(ngDevMode ? [undefined, { debugName: "ariaLabel" }] : /* istanbul ignore next */ []));
/**
* Establishes relationships between the component and label(s) where its value should be one or more element IDs.
* @group Props
*/
ariaLabelledBy = input(/* @ts-ignore */
...(ngDevMode ? [undefined, { debugName: "ariaLabelledBy" }] : /* istanbul ignore next */ []));
/**
* Identifier of the focus input to match a label defined for the component.
* @group Props
*/
inputId = input(/* @ts-ignore */
...(ngDevMode ? [undefined, { debugName: "inputId" }] : /* istanbul ignore next */ []));
/**
* Index of the element in tabbing order.
* @group Props
*/
tabindex = input(0, { ...(ngDevMode ? { debugName: "tabindex" } : /* istanbul ignore next */ {}), transform: numberAttribute });
/**
* Position of the icon.
* @group Props
*/
iconPos = input('left', /* @ts-ignore */
...(ngDevMode ? [{ debugName: "iconPos" }] : /* istanbul ignore next */ []));
/**
* When present, it specifies that the component should automatically get focus on load.
* @group Props
*/
autofocus = input(false, { ...(ngDevMode ? { debugName: "autofocus" } : /* istanbul ignore next */ {}), transform: booleanAttribute });
/**
* Defines the size of the component.
* @group Props
*/
size = input(/* @ts-ignore */
...(ngDevMode ? [undefined, { debugName: "size" }] : /* istanbul ignore next */ []));
/**
* Whether selection can not be cleared.
* @group Props
*/
allowEmpty = input(/* @ts-ignore */
...(ngDevMode ? [undefined, { debugName: "allowEmpty" }] : /* istanbul ignore next */ []));
/**
* Spans 100% width of the container when enabled.
* @defaultValue undefined
* @group Props
*/
fluid = input(undefined, { ...(ngDevMode ? { debugName: "fluid" } : /* istanbul ignore next */ {}), transform: booleanAttribute });
/**
* Callback to invoke on value change.
* @param {ToggleButtonChangeEvent} event - Custom change event.
* @group Emits
*/
onChange = output();
/**
* Custom icon template.
* @param {ToggleButtonIconTemplateContext} context - icon context.
* @see {@link ToggleButtonIconTemplateContext}
* @group Templates
*/
iconTemplate = contentChild('icon', { ...(ngDevMode ? { debugName: "iconTemplate" } : /* istanbul ignore next */ {}), descendants: false });
/**
* Custom content template.
* @param {ToggleButtonContentTemplateContext} context - content context.
* @see {@link ToggleButtonContentTemplateContext}
* @group Templates
*/
contentTemplate = contentChild('content', { ...(ngDevMode ? { debugName: "contentTemplate" } : /* istanbul ignore next */ {}), descendants: false });
checked = signal(false, /* @ts-ignore */
...(ngDevMode ? [{ debugName: "checked" }] : /* istanbul ignore next */ []));
hasOnLabel = computed(() => !!(this.onLabel() && this.onLabel().length > 0), /* @ts-ignore */
...(ngDevMode ? [{ debugName: "hasOnLabel" }] : /* istanbul ignore next */ []));
hasOffLabel = computed(() => !!(this.offLabel() && this.offLabel().length > 0), /* @ts-ignore */
...(ngDevMode ? [{ debugName: "hasOffLabel" }] : /* istanbul ignore next */ []));
hasIcon = computed(() => !!(this.onIcon() || this.offIcon()), /* @ts-ignore */
...(ngDevMode ? [{ debugName: "hasIcon" }] : /* istanbul ignore next */ []));
hasLabel = computed(() => (this.checked() ? this.hasOnLabel() : this.hasOffLabel()), /* @ts-ignore */
...(ngDevMode ? [{ debugName: "hasLabel" }] : /* istanbul ignore next */ []));
active = computed(() => this.checked() === true, /* @ts-ignore */
...(ngDevMode ? [{ debugName: "active" }] : /* istanbul ignore next */ []));
dataP = computed(() => this.cn({
checked: this.active(),
invalid: this.invalid(),
[this.size()]: this.size()
}), /* @ts-ignore */
...(ngDevMode ? [{ debugName: "dataP" }] : /* istanbul ignore next */ []));
$tabindex = computed(() => (this.$disabled() ? -1 : (this.tabindex() ?? 0)), /* @ts-ignore */
...(ngDevMode ? [{ debugName: "$tabindex" }] : /* istanbul ignore next */ []));
iconClass = computed(() => this.cn(this.cx('icon'), this.checked() ? this.onIcon() : this.offIcon(), this.iconPos() === 'left' ? this.cx('iconLeft') : this.cx('iconRight')), /* @ts-ignore */
...(ngDevMode ? [{ debugName: "iconClass" }] : /* istanbul ignore next */ []));
labelText = computed(() => (this.checked() ? this.onLabel() : this.offLabel()), /* @ts-ignore */
...(ngDevMode ? [{ debugName: "labelText" }] : /* istanbul ignore next */ []));
ariaPressed = computed(() => (this.checked() ? 'true' : 'false'), /* @ts-ignore */
...(ngDevMode ? [{ debugName: "ariaPressed" }] : /* istanbul ignore next */ []));
getTemplateContext() {
return { $implicit: this.checked() };
}
onAfterViewChecked() {
this.bindDirectiveInstance.setAttrs(this.ptms(['host', 'root']));
}
onKeyDown(event) {
switch (event.code) {
case 'Enter':
this.toggle(event);
event.preventDefault();
break;
case 'Space':
this.toggle(event);
event.preventDefault();
break;
}
}
toggle(event) {
if (!this.$disabled() && !(this.allowEmpty() === false && this.checked())) {
this.checked.set(!this.checked());
this.writeModelValue(this.checked());
this.onModelChange(this.checked());
this.onModelTouched();
this.onChange.emit({
originalEvent: event,
checked: this.checked()
});
}
}
onInit() {
if (this.checked() === null || this.checked() === undefined) {
this.checked.set(false);
}
}
onBlur() {
this.onModelTouched();
}
/**
* @override
*
* @see {@link BaseEditableHolder.writeControlValue}
* Writes the value to the control.
*/
writeControlValue(value, setModelValue) {
this.checked.set(value);
setModelValue(value);
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: ToggleButton, deps: null, target: i0.ɵɵFactoryTarget.Component });
static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "22.0.6", type: ToggleButton, isStandalone: true, selector: "p-togglebutton, p-toggle-button", inputs: { onLabel: { classPropertyName: "onLabel", publicName: "onLabel", isSignal: true, isRequired: false, transformFunction: null }, offLabel: { classPropertyName: "offLabel", publicName: "offLabel", isSignal: true, isRequired: false, transformFunction: null }, onIcon: { classPropertyName: "onIcon", publicName: "onIcon", isSignal: true, isRequired: false, transformFunction: null }, offIcon: { classPropertyName: "offIcon", publicName: "offIcon", isSignal: true, isRequired: false, transformFunction: null }, ariaLabel: { classPropertyName: "ariaLabel", publicName: "ariaLabel", isSignal: true, isRequired: false, transformFunction: null }, ariaLabelledBy: { classPropertyName: "ariaLabelledBy", publicName: "ariaLabelledBy", isSignal: true, isRequired: false, transformFunction: null }, inputId: { classPropertyName: "inputId", publicName: "inputId", isSignal: true, isRequired: false, transformFunction: null }, tabindex: { classPropertyName: "tabindex", publicName: "tabindex", isSignal: true, isRequired: false, transformFunction: null }, iconPos: { classPropertyName: "iconPos", publicName: "iconPos", isSignal: true, isRequired: false, transformFunction: null }, autofocus: { classPropertyName: "autofocus", publicName: "autofocus", isSignal: true, isRequired: false, transformFunction: null }, size: { classPropertyName: "size", publicName: "size", isSignal: true, isRequired: false, transformFunction: null }, allowEmpty: { classPropertyName: "allowEmpty", publicName: "allowEmpty", isSignal: true, isRequired: false, transformFunction: null }, fluid: { classPropertyName: "fluid", publicName: "fluid", isSignal: true, isRequired: false, transformFunction: null } }, outputs: { onChange: "onChange" }, host: { listeners: { "keydown": "onKeyDown($event)", "click": "toggle($event)" }, properties: { "class": "cx('root')", "attr.aria-labelledby": "ariaLabelledBy()", "attr.aria-label": "ariaLabel()", "attr.aria-pressed": "ariaPressed()", "attr.role": "\"button\"", "attr.tabindex": "$tabindex()", "attr.data-pc-name": "'togglebutton'", "attr.data-p-checked": "active()", "attr.data-p-disabled": "$disabled()", "attr.data-p": "dataP()" } }, providers: [TOGGLEBUTTON_VALUE_ACCESSOR, ToggleButtonStyle, { provide: TOGGLEBUTTON_INSTANCE, useExisting: ToggleButton }, { provide: PARENT_INSTANCE, useExisting: ToggleButton }], queries: [{ propertyName: "iconTemplate", first: true, predicate: ["icon"], isSignal: true }, { propertyName: "contentTemplate", first: true, predicate: ["content"], isSignal: true }], usesInheritance: true, hostDirectives: [{ directive: i1.Ripple }, { directive: i2.Bind }], ngImport: i0, template: `<span [class]="cx('content')" [pBind]="ptm('content')" [attr.data-p]="dataP()">
<ng-container *ngTemplateOutlet="contentTemplate(); context: getTemplateContext()"></ng-container>
(!contentTemplate()) {
(!iconTemplate()) {
(hasIcon()) {
<span [class]="iconClass()" [pBind]="ptm('icon')"></span>
}
} {
<ng-container *ngTemplateOutlet="iconTemplate(); context: getTemplateContext()"></ng-container>
}
(hasLabel()) {
<span [class]="cx('label')" [pBind]="ptm('label')">{{ labelText() }}</span>
}
}
</span>`, isInline: true, dependencies: [{ kind: "directive", type: NgTemplateOutlet, selector: "[ngTemplateOutlet]", inputs: ["ngTemplateOutletContext", "ngTemplateOutlet", "ngTemplateOutletInjector"] }, { kind: "ngmodule", type: SharedModule }, { kind: "ngmodule", type: BindModule }, { kind: "directive", type: i2.Bind, selector: "[pBind]", inputs: ["pBind"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: ToggleButton, decorators: [{
type: Component,
args: [{
selector: 'p-togglebutton, p-toggle-button',
standalone: true,
imports: [NgTemplateOutlet, SharedModule, BindModule],
hostDirectives: [{ directive: Ripple }, Bind],
host: {
'[class]': "cx('root')",
'[attr.aria-labelledby]': 'ariaLabelledBy()',
'[attr.aria-label]': 'ariaLabel()',
'[attr.aria-pressed]': 'ariaPressed()',
'[attr.role]': '"button"',
'[attr.tabindex]': '$tabindex()',
'[attr.data-pc-name]': "'togglebutton'",
'[attr.data-p-checked]': 'active()',
'[attr.data-p-disabled]': '$disabled()',
'[attr.data-p]': 'dataP()'
},
template: `<span [class]="cx('content')" [pBind]="ptm('content')" [attr.data-p]="dataP()">
<ng-container *ngTemplateOutlet="contentTemplate(); context: getTemplateContext()"></ng-container>
(!contentTemplate()) {
(!iconTemplate()) {
(hasIcon()) {
<span [class]="iconClass()" [pBind]="ptm('icon')"></span>
}
} {
<ng-container *ngTemplateOutlet="iconTemplate(); context: getTemplateContext()"></ng-container>
}
(hasLabel()) {
<span [class]="cx('label')" [pBind]="ptm('label')">{{ labelText() }}</span>
}
}
</span>`,
providers: [TOGGLEBUTTON_VALUE_ACCESSOR, ToggleButtonStyle, { provide: TOGGLEBUTTON_INSTANCE, useExisting: ToggleButton }, { provide: PARENT_INSTANCE, useExisting: ToggleButton }],
changeDetection: ChangeDetectionStrategy.OnPush
}]
}], propDecorators: { onLabel: [{ type: i0.Input, args: [{ isSignal: true, alias: "onLabel", required: false }] }], offLabel: [{ type: i0.Input, args: [{ isSignal: true, alias: "offLabel", required: false }] }], onIcon: [{ type: i0.Input, args: [{ isSignal: true, alias: "onIcon", required: false }] }], offIcon: [{ type: i0.Input, args: [{ isSignal: true, alias: "offIcon", required: false }] }], ariaLabel: [{ type: i0.Input, args: [{ isSignal: true, alias: "ariaLabel", required: false }] }], ariaLabelledBy: [{ type: i0.Input, args: [{ isSignal: true, alias: "ariaLabelledBy", required: false }] }], inputId: [{ type: i0.Input, args: [{ isSignal: true, alias: "inputId", required: false }] }], tabindex: [{ type: i0.Input, args: [{ isSignal: true, alias: "tabindex", required: false }] }], iconPos: [{ type: i0.Input, args: [{ isSignal: true, alias: "iconPos", required: false }] }], autofocus: [{ type: i0.Input, args: [{ isSignal: true, alias: "autofocus", required: false }] }], size: [{ type: i0.Input, args: [{ isSignal: true, alias: "size", required: false }] }], allowEmpty: [{ type: i0.Input, args: [{ isSignal: true, alias: "allowEmpty", required: false }] }], fluid: [{ type: i0.Input, args: [{ isSignal: true, alias: "fluid", required: false }] }], onChange: [{ type: i0.Output, args: ["onChange"] }], iconTemplate: [{ type: i0.ContentChild, args: ['icon', { ...{ descendants: false }, isSignal: true }] }], contentTemplate: [{ type: i0.ContentChild, args: ['content', { ...{ descendants: false }, isSignal: true }] }], onKeyDown: [{
type: HostListener,
args: ['keydown', ['$event']]
}], toggle: [{
type: HostListener,
args: ['click', ['$event']]
}] } });
class ToggleButtonModule {
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: ToggleButtonModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "22.0.6", ngImport: i0, type: ToggleButtonModule, imports: [ToggleButton, SharedModule], exports: [ToggleButton, SharedModule] });
static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: ToggleButtonModule, imports: [ToggleButton, SharedModule, SharedModule] });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: ToggleButtonModule, decorators: [{
type: NgModule,
args: [{
imports: [ToggleButton, SharedModule],
exports: [ToggleButton, SharedModule]
}]
}] });
/**
* Generated bundle index. Do not edit.
*/
export { TOGGLEBUTTON_VALUE_ACCESSOR, ToggleButton, ToggleButtonClasses, ToggleButtonModule, ToggleButtonStyle };
//# sourceMappingURL=primeng-togglebutton.mjs.map