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
614 lines (588 loc) • 23.4 kB
JavaScript
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, ViewChild, Output, Input, ViewEncapsulation, ChangeDetectionStrategy, Component, NgModule } from '@angular/core';
import { NG_VALUE_ACCESSOR, NgControl } from '@angular/forms';
import { contains, equals } from '@primeuix/utils';
import { SharedModule, PrimeTemplate } from 'primeng/api';
import { BaseComponent } from 'primeng/basecomponent';
import { CheckIcon, MinusIcon } from 'primeng/icons';
import { BaseStyle } from 'primeng/base';
const theme = ({ dt }) => `
.p-checkbox {
position: relative;
display: inline-flex;
user-select: none;
vertical-align: bottom;
width: ${dt('checkbox.width')};
height: ${dt('checkbox.height')};
}
.p-checkbox-input {
cursor: pointer;
appearance: none;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
padding: 0;
margin: 0;
opacity: 0;
z-index: 1;
outline: 0 none;
border: 1px solid transparent;
border-radius: ${dt('checkbox.border.radius')};
}
.p-checkbox-box {
display: flex;
justify-content: center;
align-items: center;
border-radius: ${dt('checkbox.border.radius')};
border: 1px solid ${dt('checkbox.border.color')};
background: ${dt('checkbox.background')};
width: ${dt('checkbox.width')};
height: ${dt('checkbox.height')};
transition: background ${dt('checkbox.transition.duration')}, color ${dt('checkbox.transition.duration')}, border-color ${dt('checkbox.transition.duration')}, box-shadow ${dt('checkbox.transition.duration')}, outline-color ${dt('checkbox.transition.duration')};
outline-color: transparent;
box-shadow: ${dt('checkbox.shadow')};
}
.p-checkbox-icon {
transition-duration: ${dt('checkbox.transition.duration')};
color: ${dt('checkbox.icon.color')};
font-size: ${dt('checkbox.icon.size')};
width: ${dt('checkbox.icon.size')};
height: ${dt('checkbox.icon.size')};
}
.p-checkbox:not(.p-disabled):has(.p-checkbox-input:hover) .p-checkbox-box {
border-color: ${dt('checkbox.hover.border.color')};
}
.p-checkbox-checked .p-checkbox-box {
border-color: ${dt('checkbox.checked.border.color')};
background: ${dt('checkbox.checked.background')};
}
.p-checkbox-checked .p-checkbox-icon {
color: ${dt('checkbox.icon.checked.color')};
}
.p-checkbox-checked:not(.p-disabled):has(.p-checkbox-input:hover) .p-checkbox-box {
background: ${dt('checkbox.checked.hover.background')};
border-color: ${dt('checkbox.checked.hover.border.color')};
}
.p-checkbox-checked:not(.p-disabled):has(.p-checkbox-input:hover) .p-checkbox-icon {
color: ${dt('checkbox.icon.checked.hover.color')};
}
.p-checkbox:not(.p-disabled):has(.p-checkbox-input:focus-visible) .p-checkbox-box {
border-color: ${dt('checkbox.focus.border.color')};
box-shadow: ${dt('checkbox.focus.ring.shadow')};
outline: ${dt('checkbox.focus.ring.width')} ${dt('checkbox.focus.ring.style')} ${dt('checkbox.focus.ring.color')};
outline-offset: ${dt('checkbox.focus.ring.offset')};
}
.p-checkbox-checked:not(.p-disabled):has(.p-checkbox-input:focus-visible) .p-checkbox-box {
border-color: ${dt('checkbox.checked.focus.border.color')};
}
p-checkBox.ng-invalid.ng-dirty .p-checkbox-box,
p-check-box.ng-invalid.ng-dirty .p-checkbox-box,
p-checkbox.ng-invalid.ng-dirty .p-checkbox-box {
border-color: ${dt('checkbox.invalid.border.color')};
}
.p-checkbox.p-variant-filled .p-checkbox-box {
background: ${dt('checkbox.filled.background')};
}
.p-checkbox-checked.p-variant-filled .p-checkbox-box {
background: ${dt('checkbox.checked.background')};
}
.p-checkbox-checked.p-variant-filled:not(.p-disabled):has(.p-checkbox-input:hover) .p-checkbox-box {
background: ${dt('checkbox.checked.hover.background')};
}
.p-checkbox.p-disabled {
opacity: 1;
}
.p-checkbox.p-disabled .p-checkbox-box {
background: ${dt('checkbox.disabled.background')};
border-color: ${dt('checkbox.checked.disabled.border.color')};
}
.p-checkbox.p-disabled .p-checkbox-box .p-checkbox-icon {
color: ${dt('checkbox.icon.disabled.color')};
}
.p-checkbox-sm,
.p-checkbox-sm .p-checkbox-box {
width: ${dt('checkbox.sm.width')};
height: ${dt('checkbox.sm.height')};
}
.p-checkbox-sm .p-checkbox-icon {
font-size: ${dt('checkbox.icon.sm.size')};
width: ${dt('checkbox.icon.sm.size')};
height: ${dt('checkbox.icon.sm.size')};
}
.p-checkbox-lg,
.p-checkbox-lg .p-checkbox-box {
width: ${dt('checkbox.lg.width')};
height: ${dt('checkbox.lg.height')};
}
.p-checkbox-lg .p-checkbox-icon {
font-size: ${dt('checkbox.icon.lg.size')};
width: ${dt('checkbox.icon.lg.size')};
height: ${dt('checkbox.icon.lg.size')};
}
`;
const classes = {
root: ({ instance, props }) => [
'p-checkbox p-component',
{
'p-checkbox-checked': instance.checked,
'p-disabled': props.disabled,
'p-invalid': props.invalid,
'p-variant-filled': props.variant ? props.variant === 'filled' : instance.config.inputStyle === 'filled' || instance.config.inputVariant === 'filled'
}
],
box: 'p-checkbox-box',
input: 'p-checkbox-input',
icon: 'p-checkbox-icon'
};
class CheckboxStyle extends BaseStyle {
name = 'checkbox';
theme = theme;
classes = classes;
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.5", ngImport: i0, type: CheckboxStyle, deps: null, target: i0.ɵɵFactoryTarget.Injectable });
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.5", ngImport: i0, type: CheckboxStyle });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.5", 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 icon element
*/
CheckboxClasses["icon"] = "p-checkbox-icon";
})(CheckboxClasses || (CheckboxClasses = {}));
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 BaseComponent {
/**
* Value of the checkbox.
* @group Props
*/
value;
/**
* Name of the checkbox group.
* @group Props
*/
name;
/**
* When present, it specifies that the element should be disabled.
* @group Props
*/
disabled;
/**
* Allows to select a boolean value instead of multiple values.
* @group Props
*/
binary;
/**
* Establishes relationships between the component and label(s) where its value should be one or more element IDs.
* @group Props
*/
ariaLabelledBy;
/**
* Used to define a string that labels the input element.
* @group Props
*/
ariaLabel;
/**
* Index of the element in tabbing order.
* @group Props
*/
tabindex;
/**
* Identifier of the focus input to match a label defined for the component.
* @group Props
*/
inputId;
/**
* Inline style of the component.
* @group Props
*/
style;
/**
* Inline style of the input element.
* @group Props
*/
inputStyle;
/**
* Style class of the component.
* @group Props
*/
styleClass;
/**
* Style class of the input element.
* @group Props
*/
inputClass;
/**
* When present, it specifies input state as indeterminate.
* @group Props
*/
indeterminate = false;
/**
* Defines the size of the component.
* @group Props
*/
size;
/**
* Form control value.
* @group Props
*/
formControl;
/**
* Icon class of the checkbox icon.
* @group Props
*/
checkboxIcon;
/**
* When present, it specifies that the component cannot be edited.
* @group Props
*/
readonly;
/**
* When present, it specifies that checkbox must be checked before submitting the form.
* @group Props
*/
required;
/**
* When present, it specifies that the component should automatically get focus on load.
* @group Props
*/
autofocus;
/**
* Value in checked state.
* @group Props
*/
trueValue = true;
/**
* Value in unchecked state.
* @group Props
*/
falseValue = false;
/**
* Specifies the input variant of the component.
* @group Props
*/
variant;
/**
* Callback to invoke on value change.
* @param {CheckboxChangeEvent} event - Custom value change event.
* @group Emits
*/
onChange = new EventEmitter();
/**
* Callback to invoke when the receives focus.
* @param {Event} event - Browser event.
* @group Emits
*/
onFocus = new EventEmitter();
/**
* Callback to invoke when the loses focus.
* @param {Event} event - Browser event.
* @group Emits
*/
onBlur = new EventEmitter();
inputViewChild;
get checked() {
return this._indeterminate() ? false : this.binary ? this.model === this.trueValue : contains(this.value, this.model);
}
get containerClass() {
return {
'p-checkbox p-component': true,
'p-checkbox-checked p-highlight': this.checked,
'p-disabled': this.disabled,
'p-variant-filled': this.variant === 'filled' || this.config.inputStyle() === 'filled' || this.config.inputVariant() === 'filled',
'p-checkbox-sm p-inputfield-sm': this.size === 'small',
'p-checkbox-lg p-inputfield-lg': this.size === 'large'
};
}
_indeterminate = signal(undefined);
/**
* The template of the checkbox icon.
* @group Templates
*/
checkboxIconTemplate;
templates;
_checkboxIconTemplate;
model;
onModelChange = () => { };
onModelTouched = () => { };
focused = false;
_componentStyle = inject(CheckboxStyle);
ngAfterContentInit() {
this.templates.forEach((item) => {
switch (item.getType()) {
case 'icon':
this._checkboxIconTemplate = item.template;
break;
case 'checkboxicon':
this._checkboxIconTemplate = item.template;
break;
}
});
}
ngOnChanges(changes) {
super.ngOnChanges(changes);
if (changes.indeterminate) {
this._indeterminate.set(changes.indeterminate.currentValue);
}
}
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.model;
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.model = newModelValue;
if (this.formControl) {
this.formControl.setValue(newModelValue);
}
}
else {
newModelValue = this._indeterminate() ? this.trueValue : this.checked ? this.falseValue : this.trueValue;
this.model = 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 = true;
this.onFocus.emit(event);
}
onInputBlur(event) {
this.focused = false;
this.onBlur.emit(event);
this.onModelTouched();
}
focus() {
this.inputViewChild.nativeElement.focus();
}
writeValue(model) {
this.model = model;
this.cd.markForCheck();
}
registerOnChange(fn) {
this.onModelChange = fn;
}
registerOnTouched(fn) {
this.onModelTouched = fn;
}
setDisabledState(val) {
setTimeout(() => {
this.disabled = val;
this.cd.markForCheck();
});
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.5", ngImport: i0, type: Checkbox, deps: null, target: i0.ɵɵFactoryTarget.Component });
static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "16.1.0", version: "19.2.5", type: Checkbox, isStandalone: true, selector: "p-checkbox, p-checkBox, p-check-box", inputs: { value: "value", name: "name", disabled: ["disabled", "disabled", booleanAttribute], binary: ["binary", "binary", booleanAttribute], ariaLabelledBy: "ariaLabelledBy", ariaLabel: "ariaLabel", tabindex: ["tabindex", "tabindex", numberAttribute], inputId: "inputId", style: "style", inputStyle: "inputStyle", styleClass: "styleClass", inputClass: "inputClass", indeterminate: ["indeterminate", "indeterminate", booleanAttribute], size: "size", formControl: "formControl", checkboxIcon: "checkboxIcon", readonly: ["readonly", "readonly", booleanAttribute], required: ["required", "required", booleanAttribute], autofocus: ["autofocus", "autofocus", booleanAttribute], trueValue: "trueValue", falseValue: "falseValue", variant: "variant" }, outputs: { onChange: "onChange", onFocus: "onFocus", onBlur: "onBlur" }, providers: [CHECKBOX_VALUE_ACCESSOR, CheckboxStyle], queries: [{ propertyName: "checkboxIconTemplate", first: true, predicate: ["checkboxicon"] }, { propertyName: "templates", predicate: PrimeTemplate }], viewQueries: [{ propertyName: "inputViewChild", first: true, predicate: ["input"], descendants: true }], usesInheritance: true, usesOnChanges: true, ngImport: i0, template: `
<div [style]="style" [class]="styleClass" [ngClass]="containerClass" [attr.data-p-highlight]="checked" [attr.data-p-checked]="checked" [attr.data-p-disabled]="disabled">
<input
#input
[attr.id]="inputId"
type="checkbox"
[value]="value"
[attr.name]="name"
[checked]="checked"
[attr.tabindex]="tabindex"
[disabled]="disabled"
[readonly]="readonly"
[attr.required]="required ? true : null"
[attr.aria-labelledby]="ariaLabelledBy"
[attr.aria-label]="ariaLabel"
[style]="inputStyle"
[class]="inputClass"
[ngClass]="{ 'p-checkbox-input': true }"
(focus)="onInputFocus($event)"
(blur)="onInputBlur($event)"
(change)="handleChange($event)"
/>
<div class="p-checkbox-box">
<ng-container *ngIf="!checkboxIconTemplate && !_checkboxIconTemplate">
<ng-container *ngIf="checked">
<span *ngIf="checkboxIcon" class="p-checkbox-icon" [ngClass]="checkboxIcon" [attr.data-pc-section]="'icon'"></span>
<CheckIcon *ngIf="!checkboxIcon" [styleClass]="'p-checkbox-icon'" [attr.data-pc-section]="'icon'" />
</ng-container>
<MinusIcon *ngIf="_indeterminate()" [styleClass]="'p-checkbox-icon'" [attr.data-pc-section]="'icon'" />
</ng-container>
<ng-template *ngTemplateOutlet="checkboxIconTemplate || _checkboxIconTemplate; context: { checked: checked, class: 'p-checkbox-icon' }"></ng-template>
</div>
</div>
`, isInline: true, dependencies: [{ kind: "ngmodule", type: CommonModule }, { kind: "directive", type: i1.NgClass, selector: "[ngClass]", inputs: ["class", "ngClass"] }, { kind: "directive", type: i1.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: i1.NgTemplateOutlet, selector: "[ngTemplateOutlet]", inputs: ["ngTemplateOutletContext", "ngTemplateOutlet", "ngTemplateOutletInjector"] }, { kind: "component", type: CheckIcon, selector: "CheckIcon" }, { kind: "component", type: MinusIcon, selector: "MinusIcon" }, { kind: "ngmodule", type: SharedModule }], changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.5", ngImport: i0, type: Checkbox, decorators: [{
type: Component,
args: [{
selector: 'p-checkbox, p-checkBox, p-check-box',
standalone: true,
imports: [CommonModule, CheckIcon, MinusIcon, SharedModule],
template: `
<div [style]="style" [class]="styleClass" [ngClass]="containerClass" [attr.data-p-highlight]="checked" [attr.data-p-checked]="checked" [attr.data-p-disabled]="disabled">
<input
#input
[attr.id]="inputId"
type="checkbox"
[value]="value"
[attr.name]="name"
[checked]="checked"
[attr.tabindex]="tabindex"
[disabled]="disabled"
[readonly]="readonly"
[attr.required]="required ? true : null"
[attr.aria-labelledby]="ariaLabelledBy"
[attr.aria-label]="ariaLabel"
[style]="inputStyle"
[class]="inputClass"
[ngClass]="{ 'p-checkbox-input': true }"
(focus)="onInputFocus($event)"
(blur)="onInputBlur($event)"
(change)="handleChange($event)"
/>
<div class="p-checkbox-box">
<ng-container *ngIf="!checkboxIconTemplate && !_checkboxIconTemplate">
<ng-container *ngIf="checked">
<span *ngIf="checkboxIcon" class="p-checkbox-icon" [ngClass]="checkboxIcon" [attr.data-pc-section]="'icon'"></span>
<CheckIcon *ngIf="!checkboxIcon" [styleClass]="'p-checkbox-icon'" [attr.data-pc-section]="'icon'" />
</ng-container>
<MinusIcon *ngIf="_indeterminate()" [styleClass]="'p-checkbox-icon'" [attr.data-pc-section]="'icon'" />
</ng-container>
<ng-template *ngTemplateOutlet="checkboxIconTemplate || _checkboxIconTemplate; context: { checked: checked, class: 'p-checkbox-icon' }"></ng-template>
</div>
</div>
`,
providers: [CHECKBOX_VALUE_ACCESSOR, CheckboxStyle],
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None
}]
}], propDecorators: { value: [{
type: Input
}], name: [{
type: Input
}], disabled: [{
type: Input,
args: [{ transform: booleanAttribute }]
}], binary: [{
type: Input,
args: [{ transform: booleanAttribute }]
}], ariaLabelledBy: [{
type: Input
}], ariaLabel: [{
type: Input
}], tabindex: [{
type: Input,
args: [{ transform: numberAttribute }]
}], inputId: [{
type: Input
}], style: [{
type: Input
}], inputStyle: [{
type: Input
}], styleClass: [{
type: Input
}], inputClass: [{
type: Input
}], indeterminate: [{
type: Input,
args: [{ transform: booleanAttribute }]
}], size: [{
type: Input
}], formControl: [{
type: Input
}], checkboxIcon: [{
type: Input
}], readonly: [{
type: Input,
args: [{ transform: booleanAttribute }]
}], required: [{
type: Input,
args: [{ transform: booleanAttribute }]
}], autofocus: [{
type: Input,
args: [{ transform: booleanAttribute }]
}], trueValue: [{
type: Input
}], falseValue: [{
type: Input
}], variant: [{
type: Input
}], onChange: [{
type: Output
}], onFocus: [{
type: Output
}], onBlur: [{
type: Output
}], inputViewChild: [{
type: ViewChild,
args: ['input']
}], checkboxIconTemplate: [{
type: ContentChild,
args: ['checkboxicon', { descendants: false }]
}], templates: [{
type: ContentChildren,
args: [PrimeTemplate]
}] } });
class CheckboxModule {
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.5", ngImport: i0, type: CheckboxModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "19.2.5", ngImport: i0, type: CheckboxModule, imports: [Checkbox, SharedModule], exports: [Checkbox, SharedModule] });
static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "19.2.5", ngImport: i0, type: CheckboxModule, imports: [Checkbox, SharedModule, SharedModule] });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.5", 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