import { forwardRef, EventEmitter, Component, ChangeDetectionStrategy, ViewEncapsulation, ChangeDetectorRef, Input, ViewChild, Output, NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { NG_VALUE_ACCESSOR } from '@angular/forms'; const CHECKBOX_VALUE_ACCESSOR = { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => Checkbox), multi: true }; class Checkbox { constructor(cd) { this.cd = cd; this.checkboxIcon = 'pi pi-check'; this.onChange = new EventEmitter(); this.onModelChange = () => { }; this.onModelTouched = () => { }; this.focused = false; this.checked = false; } onClick(event, checkbox, focus) { event.preventDefault(); if (this.disabled || this.readonly) { return; } this.checked = !this.checked; this.updateModel(event); if (focus) { checkbox.focus(); } } updateModel(event) { if (!this.binary) { if (this.checked) this.addValue(); else this.removeValue(); this.onModelChange(this.model); if (this.formControl) { this.formControl.setValue(this.model); } } else { this.onModelChange(this.checked); } this.onChange.emit({ checked: this.checked, originalEvent: event }); } handleChange(event) { if (!this.readonly) { this.checked = event.target.checked; this.updateModel(event); } } isChecked() { if (this.binary) return this.model; else return this.model && this.model.indexOf(this.value) > -1; } removeValue() { this.model = this.model.filter(val => val !== this.value); } addValue() { if (this.model) this.model = [...this.model, this.value]; else this.model = [this.value]; } onFocus() { this.focused = true; } onBlur() { this.focused = false; this.onModelTouched(); } focus() { this.inputViewChild.nativeElement.focus(); } writeValue(model) { this.model = model; this.checked = this.isChecked(); this.cd.markForCheck(); } registerOnChange(fn) { this.onModelChange = fn; } registerOnTouched(fn) { this.onModelTouched = fn; } setDisabledState(val) { this.disabled = val; this.cd.markForCheck(); } } Checkbox.decorators = [ { type: Component, args: [{ selector: 'p-checkbox', template: `