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,
442 lines (437 loc) • 24.5 kB
JavaScript
export * from 'primeng/types/checkbox';
import { NgTemplateOutlet } from '@angular/common';
import * as i0 from '@angular/core';
import { Injectable, InjectionToken, forwardRef, input, booleanAttribute, output, viewChild, contentChild, signal, inject, computed, effect, ViewEncapsulation, ChangeDetectionStrategy, Component, NgModule } from '@angular/core';
import { NG_VALUE_ACCESSOR, NgControl } from '@angular/forms';
import { contains, equals } from '@primeuix/utils';
import { SharedModule } from 'primeng/api';
import { PARENT_INSTANCE } from 'primeng/basecomponent';
import { BaseEditableHolder } from 'primeng/baseeditableholder';
import * as i1 from 'primeng/bind';
import { Bind, BindModule } from 'primeng/bind';
import { Check } from '@primeicons/angular/check';
import { Minus } from '@primeicons/angular/minus';
import { style } from '@primeuix/styles/checkbox';
import { BaseStyle } from 'primeng/base';
const classes = {
root: ({ instance }) => [
'p-checkbox p-component',
{
'p-checkbox-checked': instance.checked(),
'p-disabled': instance.$disabled(),
'p-invalid': instance.invalid(),
'p-variant-filled': instance.$variant() === 'filled',
'p-checkbox-sm p-inputfield-sm': instance.size() === 'small',
'p-checkbox-lg p-inputfield-lg': instance.size() === 'large'
}
],
box: 'p-checkbox-box',
input: 'p-checkbox-input',
indicator: 'p-checkbox-indicator',
icon: 'p-checkbox-icon'
};
class CheckboxStyle extends BaseStyle {
name = 'checkbox';
style = style;
classes = classes;
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: CheckboxStyle, deps: null, target: i0.ɵɵFactoryTarget.Injectable });
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: CheckboxStyle });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: CheckboxStyle, decorators: [{
type: Injectable
}] });
/**
*
* Checkbox is an extension to standard checkbox element with theming.
*
* [Live Demo](https://www.primeng.org/checkbox/)
*
* @module checkboxstyle
*
*/
var CheckboxClasses;
(function (CheckboxClasses) {
/**
* Class name of the root element
*/
CheckboxClasses["root"] = "p-checkbox";
/**
* Class name of the box element
*/
CheckboxClasses["box"] = "p-checkbox-box";
/**
* Class name of the input element
*/
CheckboxClasses["input"] = "p-checkbox-input";
/**
* Class name of the indicator element
*/
CheckboxClasses["indicator"] = "p-checkbox-indicator";
/**
* Class name of the icon element
*/
CheckboxClasses["icon"] = "p-checkbox-icon";
})(CheckboxClasses || (CheckboxClasses = {}));
const CHECKBOX_INSTANCE = new InjectionToken('CHECKBOX_INSTANCE');
const CHECKBOX_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => Checkbox),
multi: true
};
/**
* Checkbox is an extension to standard checkbox element with theming.
* @group Components
*/
class Checkbox extends BaseEditableHolder {
componentName = 'Checkbox';
/**
* Value of the checkbox.
* @group Props
*/
value = input(/* @ts-ignore */
...(ngDevMode ? [undefined, { debugName: "value" }] : /* istanbul ignore next */ []));
/**
* Allows to select a boolean value instead of multiple values.
* @group Props
*/
binary = input(false, { ...(ngDevMode ? { debugName: "binary" } : /* istanbul ignore next */ {}), transform: booleanAttribute });
/**
* 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 */ []));
/**
* Used to define a string that labels the input element.
* @group Props
*/
ariaLabel = input(/* @ts-ignore */
...(ngDevMode ? [undefined, { debugName: "ariaLabel" }] : /* istanbul ignore next */ []));
/**
* Index of the element in tabbing order.
* @group Props
*/
tabindex = input(/* @ts-ignore */
...(ngDevMode ? [undefined, { debugName: "tabindex" }] : /* 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 */ []));
/**
* Inline style of the input element.
* @group Props
*/
inputStyle = input(/* @ts-ignore */
...(ngDevMode ? [undefined, { debugName: "inputStyle" }] : /* istanbul ignore next */ []));
/**
* Style class of the input element.
* @group Props
*/
inputClass = input(/* @ts-ignore */
...(ngDevMode ? [undefined, { debugName: "inputClass" }] : /* istanbul ignore next */ []));
/**
* When present, it specifies input state as indeterminate.
* @group Props
*/
indeterminate = input(false, { ...(ngDevMode ? { debugName: "indeterminate" } : /* istanbul ignore next */ {}), transform: booleanAttribute });
/**
* Form control value.
* @group Props
*/
formControl = input(/* @ts-ignore */
...(ngDevMode ? [undefined, { debugName: "formControl" }] : /* istanbul ignore next */ []));
/**
* Icon class of the checkbox icon.
* @group Props
*/
checkboxIcon = input(/* @ts-ignore */
...(ngDevMode ? [undefined, { debugName: "checkboxIcon" }] : /* istanbul ignore next */ []));
/**
* When present, it specifies that the component cannot be edited.
* @group Props
*/
readonly = input(false, { ...(ngDevMode ? { debugName: "readonly" } : /* istanbul ignore next */ {}), transform: booleanAttribute });
/**
* 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 });
/**
* Value in checked state.
* @group Props
*/
trueValue = input(true, /* @ts-ignore */
...(ngDevMode ? [{ debugName: "trueValue" }] : /* istanbul ignore next */ []));
/**
* Value in unchecked state.
* @group Props
*/
falseValue = input(false, /* @ts-ignore */
...(ngDevMode ? [{ debugName: "falseValue" }] : /* istanbul ignore next */ []));
/**
* Specifies the input variant of the component.
* @defaultValue undefined
* @group Props
*/
variant = input(/* @ts-ignore */
...(ngDevMode ? [undefined, { debugName: "variant" }] : /* istanbul ignore next */ []));
/**
* Specifies the size of the component.
* @defaultValue undefined
* @group Props
*/
size = input(/* @ts-ignore */
...(ngDevMode ? [undefined, { debugName: "size" }] : /* istanbul ignore next */ []));
/**
* Callback to invoke on value change.
* @param {CheckboxChangeEvent} event - Custom value change event.
* @group Emits
*/
onChange = output();
/**
* Callback to invoke when the receives focus.
* @param {Event} event - Browser event.
* @group Emits
*/
onFocus = output();
/**
* Callback to invoke when the loses focus.
* @param {Event} event - Browser event.
* @group Emits
*/
onBlur = output();
inputViewChild = viewChild('input', /* @ts-ignore */
...(ngDevMode ? [{ debugName: "inputViewChild" }] : /* istanbul ignore next */ []));
/**
* Custom checkbox icon template.
* @group Templates
*/
iconTemplate = contentChild('icon', { ...(ngDevMode ? { debugName: "iconTemplate" } : /* istanbul ignore next */ {}), descendants: false });
_indeterminate = signal(false, /* @ts-ignore */
...(ngDevMode ? [{ debugName: "_indeterminate" }] : /* istanbul ignore next */ []));
focused = signal(false, /* @ts-ignore */
...(ngDevMode ? [{ debugName: "focused" }] : /* istanbul ignore next */ []));
_componentStyle = inject(CheckboxStyle);
bindDirectiveInstance = inject(Bind, { self: true });
$pcCheckbox = inject(CHECKBOX_INSTANCE, { optional: true, skipSelf: true }) ?? undefined;
$variant = computed(() => this.variant() || this.config.inputVariant(), /* @ts-ignore */
...(ngDevMode ? [{ debugName: "$variant" }] : /* istanbul ignore next */ []));
requiredAttr = computed(() => (this.required() ? '' : undefined), /* @ts-ignore */
...(ngDevMode ? [{ debugName: "requiredAttr" }] : /* istanbul ignore next */ []));
readonlyAttr = computed(() => (this.readonly() ? '' : undefined), /* @ts-ignore */
...(ngDevMode ? [{ debugName: "readonlyAttr" }] : /* istanbul ignore next */ []));
disabledAttr = computed(() => (this.$disabled() ? '' : undefined), /* @ts-ignore */
...(ngDevMode ? [{ debugName: "disabledAttr" }] : /* istanbul ignore next */ []));
checked = computed(() => {
if (this._indeterminate())
return false;
return this.binary() ? this.modelValue() === this.trueValue() : contains(this.value(), this.modelValue());
}, /* @ts-ignore */
...(ngDevMode ? [{ debugName: "checked" }] : /* istanbul ignore next */ []));
iconTemplateContext = computed(() => ({
checked: this.checked(),
class: this.cx('icon'),
dataP: this.dataP()
}), /* @ts-ignore */
...(ngDevMode ? [{ debugName: "iconTemplateContext" }] : /* istanbul ignore next */ []));
dataP = computed(() => {
return this.cn({
invalid: this.invalid(),
checked: this.checked(),
disabled: this.$disabled(),
filled: this.$variant() === 'filled',
[this.size()]: this.size()
});
}, /* @ts-ignore */
...(ngDevMode ? [{ debugName: "dataP" }] : /* istanbul ignore next */ []));
constructor() {
super();
effect(() => {
const indeterminate = this.indeterminate();
this._indeterminate.set(indeterminate);
});
}
onAfterViewChecked() {
this.bindDirectiveInstance.setAttrs(this.ptms(['host', 'root']));
}
updateModel(event) {
let newModelValue;
/*
* When `formControlName` or `formControl` is used - `writeValue` is not called after control changes.
* Otherwise it is causing multiple references to the actual value: there is one array reference inside the component and another one in the control value.
* `selfControl` is the source of truth of references, it is made to avoid reference loss.
* */
const selfControl = this.injector.get(NgControl, null, { optional: true, self: true });
const currentModelValue = selfControl && !this.formControl() ? selfControl.value : this.modelValue();
if (!this.binary()) {
if (this.checked() || this._indeterminate())
newModelValue = currentModelValue.filter((val) => !equals(val, this.value()));
else
newModelValue = currentModelValue ? [...currentModelValue, this.value()] : [this.value()];
this.onModelChange(newModelValue);
this.writeModelValue(newModelValue);
const formControl = this.formControl();
if (formControl) {
formControl.setValue(newModelValue);
}
}
else {
newModelValue = this._indeterminate() ? this.trueValue() : this.checked() ? this.falseValue() : this.trueValue();
this.writeModelValue(newModelValue);
this.onModelChange(newModelValue);
}
if (this._indeterminate()) {
this._indeterminate.set(false);
}
this.onChange.emit({ checked: newModelValue, originalEvent: event });
}
handleChange(event) {
if (!this.readonly()) {
this.updateModel(event);
}
}
onInputFocus(event) {
this.focused.set(true);
this.onFocus.emit(event);
}
onInputBlur(event) {
this.focused.set(false);
this.onBlur.emit(event);
this.onModelTouched();
}
focus() {
this.inputViewChild()?.nativeElement.focus();
}
/**
* @override
*
* @see {@link BaseEditableHolder.writeControlValue}
* Writes the value to the control.
*/
writeControlValue(value, setModelValue) {
setModelValue(value);
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: Checkbox, deps: [], target: i0.ɵɵFactoryTarget.Component });
static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "22.0.6", type: Checkbox, isStandalone: true, selector: "p-checkbox, p-check-box", inputs: { value: { classPropertyName: "value", publicName: "value", isSignal: true, isRequired: false, transformFunction: null }, binary: { classPropertyName: "binary", publicName: "binary", isSignal: true, isRequired: false, transformFunction: null }, ariaLabelledBy: { classPropertyName: "ariaLabelledBy", publicName: "ariaLabelledBy", isSignal: true, isRequired: false, transformFunction: null }, ariaLabel: { classPropertyName: "ariaLabel", publicName: "ariaLabel", isSignal: true, isRequired: false, transformFunction: null }, tabindex: { classPropertyName: "tabindex", publicName: "tabindex", isSignal: true, isRequired: false, transformFunction: null }, inputId: { classPropertyName: "inputId", publicName: "inputId", isSignal: true, isRequired: false, transformFunction: null }, inputStyle: { classPropertyName: "inputStyle", publicName: "inputStyle", isSignal: true, isRequired: false, transformFunction: null }, inputClass: { classPropertyName: "inputClass", publicName: "inputClass", isSignal: true, isRequired: false, transformFunction: null }, indeterminate: { classPropertyName: "indeterminate", publicName: "indeterminate", isSignal: true, isRequired: false, transformFunction: null }, formControl: { classPropertyName: "formControl", publicName: "formControl", isSignal: true, isRequired: false, transformFunction: null }, checkboxIcon: { classPropertyName: "checkboxIcon", publicName: "checkboxIcon", isSignal: true, isRequired: false, transformFunction: null }, readonly: { classPropertyName: "readonly", publicName: "readonly", isSignal: true, isRequired: false, transformFunction: null }, autofocus: { classPropertyName: "autofocus", publicName: "autofocus", isSignal: true, isRequired: false, transformFunction: null }, trueValue: { classPropertyName: "trueValue", publicName: "trueValue", isSignal: true, isRequired: false, transformFunction: null }, falseValue: { classPropertyName: "falseValue", publicName: "falseValue", isSignal: true, isRequired: false, transformFunction: null }, variant: { classPropertyName: "variant", publicName: "variant", isSignal: true, isRequired: false, transformFunction: null }, size: { classPropertyName: "size", publicName: "size", isSignal: true, isRequired: false, transformFunction: null } }, outputs: { onChange: "onChange", onFocus: "onFocus", onBlur: "onBlur" }, host: { properties: { "class": "cx('root')", "attr.data-p-highlight": "checked()", "attr.data-p-checked": "checked()", "attr.data-p-disabled": "$disabled()", "attr.data-p": "dataP()" } }, providers: [CHECKBOX_VALUE_ACCESSOR, CheckboxStyle, { provide: CHECKBOX_INSTANCE, useExisting: Checkbox }, { provide: PARENT_INSTANCE, useExisting: Checkbox }], queries: [{ propertyName: "iconTemplate", first: true, predicate: ["icon"], isSignal: true }], viewQueries: [{ propertyName: "inputViewChild", first: true, predicate: ["input"], descendants: true, isSignal: true }], usesInheritance: true, hostDirectives: [{ directive: i1.Bind }], ngImport: i0, template: `
<input
#input
[attr.id]="inputId()"
type="checkbox"
[attr.value]="value()"
[attr.name]="name()"
[checked]="checked()"
[attr.tabindex]="tabindex()"
[attr.required]="requiredAttr()"
[attr.readonly]="readonlyAttr()"
[attr.disabled]="disabledAttr()"
[attr.aria-labelledby]="ariaLabelledBy()"
[attr.aria-label]="ariaLabel()"
[style]="inputStyle()"
[class]="cn(cx('input'), inputClass())"
[pBind]="ptm('input')"
(focus)="onInputFocus($event)"
(blur)="onInputBlur($event)"
(change)="handleChange($event)"
/>
<div [class]="cx('box')" [pBind]="ptm('box')" [attr.data-p]="dataP()">
(!iconTemplate()) {
(checked()) {
<span [class]="cx('indicator')" [pBind]="ptm('indicator')">
(checkboxIcon()) {
<span [class]="cn(cx('icon'), checkboxIcon())" [pBind]="ptm('icon')" [attr.data-p]="dataP()"></span>
} {
<svg data-p-icon="check" [class]="cx('icon')" [pBind]="ptm('icon')" [attr.data-p]="dataP()" />
}
</span>
}
(_indeterminate()) {
<span [class]="cx('indicator')" [pBind]="ptm('indicator')">
<svg data-p-icon="minus" [class]="cx('icon')" [pBind]="ptm('icon')" [attr.data-p]="dataP()" />
</span>
}
} {
<ng-container *ngTemplateOutlet="iconTemplate(); context: iconTemplateContext()"></ng-container>
}
</div>
`, isInline: true, dependencies: [{ kind: "directive", type: NgTemplateOutlet, selector: "[ngTemplateOutlet]", inputs: ["ngTemplateOutletContext", "ngTemplateOutlet", "ngTemplateOutletInjector"] }, { kind: "ngmodule", type: SharedModule }, { kind: "component", type: Check, selector: "svg[data-p-icon=\"check\"]" }, { kind: "component", type: Minus, selector: "svg[data-p-icon=\"minus\"]" }, { kind: "ngmodule", type: BindModule }, { kind: "directive", type: i1.Bind, selector: "[pBind]", inputs: ["pBind"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: Checkbox, decorators: [{
type: Component,
args: [{
selector: 'p-checkbox, p-check-box',
standalone: true,
imports: [NgTemplateOutlet, SharedModule, Check, Minus, BindModule],
template: `
<input
#input
[attr.id]="inputId()"
type="checkbox"
[attr.value]="value()"
[attr.name]="name()"
[checked]="checked()"
[attr.tabindex]="tabindex()"
[attr.required]="requiredAttr()"
[attr.readonly]="readonlyAttr()"
[attr.disabled]="disabledAttr()"
[attr.aria-labelledby]="ariaLabelledBy()"
[attr.aria-label]="ariaLabel()"
[style]="inputStyle()"
[class]="cn(cx('input'), inputClass())"
[pBind]="ptm('input')"
(focus)="onInputFocus($event)"
(blur)="onInputBlur($event)"
(change)="handleChange($event)"
/>
<div [class]="cx('box')" [pBind]="ptm('box')" [attr.data-p]="dataP()">
(!iconTemplate()) {
(checked()) {
<span [class]="cx('indicator')" [pBind]="ptm('indicator')">
(checkboxIcon()) {
<span [class]="cn(cx('icon'), checkboxIcon())" [pBind]="ptm('icon')" [attr.data-p]="dataP()"></span>
} {
<svg data-p-icon="check" [class]="cx('icon')" [pBind]="ptm('icon')" [attr.data-p]="dataP()" />
}
</span>
}
(_indeterminate()) {
<span [class]="cx('indicator')" [pBind]="ptm('indicator')">
<svg data-p-icon="minus" [class]="cx('icon')" [pBind]="ptm('icon')" [attr.data-p]="dataP()" />
</span>
}
} {
<ng-container *ngTemplateOutlet="iconTemplate(); context: iconTemplateContext()"></ng-container>
}
</div>
`,
providers: [CHECKBOX_VALUE_ACCESSOR, CheckboxStyle, { provide: CHECKBOX_INSTANCE, useExisting: Checkbox }, { provide: PARENT_INSTANCE, useExisting: Checkbox }],
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
host: {
'[class]': "cx('root')",
'[attr.data-p-highlight]': 'checked()',
'[attr.data-p-checked]': 'checked()',
'[attr.data-p-disabled]': '$disabled()',
'[attr.data-p]': 'dataP()'
},
hostDirectives: [Bind]
}]
}], ctorParameters: () => [], propDecorators: { value: [{ type: i0.Input, args: [{ isSignal: true, alias: "value", required: false }] }], binary: [{ type: i0.Input, args: [{ isSignal: true, alias: "binary", required: false }] }], ariaLabelledBy: [{ type: i0.Input, args: [{ isSignal: true, alias: "ariaLabelledBy", required: false }] }], ariaLabel: [{ type: i0.Input, args: [{ isSignal: true, alias: "ariaLabel", required: false }] }], tabindex: [{ type: i0.Input, args: [{ isSignal: true, alias: "tabindex", required: false }] }], inputId: [{ type: i0.Input, args: [{ isSignal: true, alias: "inputId", required: false }] }], inputStyle: [{ type: i0.Input, args: [{ isSignal: true, alias: "inputStyle", required: false }] }], inputClass: [{ type: i0.Input, args: [{ isSignal: true, alias: "inputClass", required: false }] }], indeterminate: [{ type: i0.Input, args: [{ isSignal: true, alias: "indeterminate", required: false }] }], formControl: [{ type: i0.Input, args: [{ isSignal: true, alias: "formControl", required: false }] }], checkboxIcon: [{ type: i0.Input, args: [{ isSignal: true, alias: "checkboxIcon", required: false }] }], readonly: [{ type: i0.Input, args: [{ isSignal: true, alias: "readonly", required: false }] }], autofocus: [{ type: i0.Input, args: [{ isSignal: true, alias: "autofocus", required: false }] }], trueValue: [{ type: i0.Input, args: [{ isSignal: true, alias: "trueValue", required: false }] }], falseValue: [{ type: i0.Input, args: [{ isSignal: true, alias: "falseValue", required: false }] }], variant: [{ type: i0.Input, args: [{ isSignal: true, alias: "variant", required: false }] }], size: [{ type: i0.Input, args: [{ isSignal: true, alias: "size", required: false }] }], onChange: [{ type: i0.Output, args: ["onChange"] }], onFocus: [{ type: i0.Output, args: ["onFocus"] }], onBlur: [{ type: i0.Output, args: ["onBlur"] }], inputViewChild: [{ type: i0.ViewChild, args: ['input', { isSignal: true }] }], iconTemplate: [{ type: i0.ContentChild, args: ['icon', { ...{ descendants: false }, isSignal: true }] }] } });
class CheckboxModule {
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: CheckboxModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "22.0.6", ngImport: i0, type: CheckboxModule, imports: [Checkbox, SharedModule], exports: [Checkbox, SharedModule] });
static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: CheckboxModule, imports: [Checkbox, SharedModule, SharedModule] });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: CheckboxModule, decorators: [{
type: NgModule,
args: [{
imports: [Checkbox, SharedModule],
exports: [Checkbox, SharedModule]
}]
}] });
/**
* Generated bundle index. Do not edit.
*/
export { CHECKBOX_VALUE_ACCESSOR, Checkbox, CheckboxClasses, CheckboxModule, CheckboxStyle };
//# sourceMappingURL=primeng-checkbox.mjs.map