UNPKG

ng-zorro-antd

Version:

An enterprise-class UI components based on Ant Design and Angular

546 lines (545 loc) • 15.6 kB
import { FocusMonitor } from "@angular/cdk/a11y"; import { Directionality } from "@angular/cdk/bidi"; import * as i0 from "@angular/core"; import { ChangeDetectorRef, Component, DestroyRef, ElementRef, EventEmitter, InjectionToken, Input, NgModule, NgZone, Output, ViewChild, ViewEncapsulation, afterNextRender, booleanAttribute, computed, effect, forwardRef, inject, input, linkedSignal, signal, untracked } from "@angular/core"; import { takeUntilDestroyed } from "@angular/core/rxjs-interop"; import * as i1 from "@angular/forms"; import { FormsModule, NG_VALUE_ACCESSOR } from "@angular/forms"; import { NzFormStatusService } from "ng-zorro-antd/core/form"; import { fromEventOutsideAngular } from "ng-zorro-antd/core/util"; const NZ_CHECKBOX_GROUP = new InjectionToken(typeof ngDevMode !== "undefined" && ngDevMode ? "nz-checkbox-group" : ""); var NzCheckboxComponent = class NzCheckboxComponent { ngZone = inject(NgZone); elementRef = inject(ElementRef); cdr = inject(ChangeDetectorRef); focusMonitor = inject(FocusMonitor); dir = inject(Directionality).valueSignal; destroyRef = inject(DestroyRef); checkboxGroupComponent = inject(NZ_CHECKBOX_GROUP, { optional: true }); nzFormStatusService = inject(NzFormStatusService, { optional: true }); isNzDisableFirstChange = true; onChange = () => {}; onTouched = () => {}; inputElement; nzCheckedChange = new EventEmitter(); nzValue = null; nzAutoFocus = false; nzDisabled = false; nzIndeterminate = false; nzChecked = false; nzId = null; nzName = null; innerCheckedChange(checked) { if (!this.nzDisabled && !this.checkboxGroupComponent?.finalDisabled()) { this.setValue(checked); this.checkboxGroupComponent?.onCheckedChange(this.nzValue, checked); } } writeValue(value) { this.nzChecked = value; this.cdr.markForCheck(); } registerOnChange(fn) { this.onChange = fn; } registerOnTouched(fn) { this.onTouched = fn; } setDisabledState(disabled) { this.nzDisabled = this.isNzDisableFirstChange && this.nzDisabled || disabled; this.isNzDisableFirstChange = false; this.cdr.markForCheck(); } focus() { this.focusMonitor.focusVia(this.inputElement, "keyboard"); } blur() { this.inputElement.nativeElement.blur(); } constructor() { this.destroyRef.onDestroy(() => { this.focusMonitor.stopMonitoring(this.elementRef); }); if (this.checkboxGroupComponent) effect(() => { const values = this.checkboxGroupComponent.value() || []; this.setValue(values.includes(this.nzValue)); this.cdr.markForCheck(); }); } ngOnInit() { this.focusMonitor.monitor(this.elementRef, true).pipe(takeUntilDestroyed(this.destroyRef)).subscribe((focusOrigin) => { if (!focusOrigin) Promise.resolve().then(() => this.onTouched()); }); fromEventOutsideAngular(this.elementRef.nativeElement, "click").pipe(takeUntilDestroyed(this.destroyRef)).subscribe((event) => { event.preventDefault(); this.focus(); if (this.nzDisabled) return; this.ngZone.run(() => { this.innerCheckedChange(!this.nzChecked); this.cdr.markForCheck(); }); }); fromEventOutsideAngular(this.inputElement.nativeElement, "click").pipe(takeUntilDestroyed(this.destroyRef)).subscribe((event) => event.stopPropagation()); } ngAfterViewInit() { if (this.nzAutoFocus) this.focus(); } setValue(value) { this.nzChecked = value; this.onChange(value); this.nzCheckedChange.emit(value); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzCheckboxComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "16.1.0", version: "22.2.2", type: NzCheckboxComponent, isStandalone: true, selector: "[nz-checkbox]", inputs: { nzValue: "nzValue", nzAutoFocus: [ "nzAutoFocus", "nzAutoFocus", booleanAttribute ], nzDisabled: [ "nzDisabled", "nzDisabled", booleanAttribute ], nzIndeterminate: [ "nzIndeterminate", "nzIndeterminate", booleanAttribute ], nzChecked: [ "nzChecked", "nzChecked", booleanAttribute ], nzId: "nzId", nzName: "nzName" }, outputs: { nzCheckedChange: "nzCheckedChange" }, host: { properties: { "class.ant-checkbox-group-item": "!!checkboxGroupComponent", "class.ant-checkbox-wrapper-in-form-item": "!!nzFormStatusService", "class.ant-checkbox-wrapper-checked": "nzChecked", "class.ant-checkbox-wrapper-disabled": "nzDisabled || checkboxGroupComponent?.finalDisabled()", "class.ant-checkbox-rtl": "dir() === 'rtl'" }, classAttribute: "ant-checkbox-wrapper" }, providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => NzCheckboxComponent), multi: true }], viewQueries: [{ propertyName: "inputElement", first: true, predicate: ["inputElement"], descendants: true, static: true }], exportAs: ["nzCheckbox"], ngImport: i0, template: ` <span class="ant-checkbox" [class.ant-checkbox-checked]="nzChecked && !nzIndeterminate" [class.ant-checkbox-disabled]="nzDisabled || checkboxGroupComponent?.finalDisabled()" [class.ant-checkbox-indeterminate]="nzIndeterminate" > <input #inputElement type="checkbox" class="ant-checkbox-input" [attr.autofocus]="nzAutoFocus ? 'autofocus' : null" [attr.id]="nzId" [attr.name]="nzName || checkboxGroupComponent?.nzName()" [checked]="nzChecked" [ngModel]="nzChecked" [ngModelOptions]="{ standalone: true }" [disabled]="nzDisabled || (checkboxGroupComponent?.finalDisabled() ?? false)" (ngModelChange)="innerCheckedChange($event)" /> <span class="ant-checkbox-inner"></span> </span> <span><ng-content /></span> `, isInline: true, dependencies: [ { kind: "ngmodule", type: FormsModule }, { kind: "directive", type: i1.CheckboxControlValueAccessor, selector: "input[type=checkbox]:not([ngNoCva])[formControlName],input[type=checkbox]:not([ngNoCva])[formControl],input[type=checkbox]:not([ngNoCva])[ngModel]" }, { kind: "directive", type: i1.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i1.NgModel, selector: "[ngModel]:not([formControlName]):not([formControl])", inputs: [ "name", "disabled", "ngModel", "ngModelOptions" ], outputs: ["ngModelChange"], exportAs: ["ngModel"] } ], encapsulation: i0.ViewEncapsulation.None }); }; i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzCheckboxComponent, decorators: [{ type: Component, args: [{ selector: "[nz-checkbox]", exportAs: "nzCheckbox", encapsulation: ViewEncapsulation.None, template: ` <span class="ant-checkbox" [class.ant-checkbox-checked]="nzChecked && !nzIndeterminate" [class.ant-checkbox-disabled]="nzDisabled || checkboxGroupComponent?.finalDisabled()" [class.ant-checkbox-indeterminate]="nzIndeterminate" > <input #inputElement type="checkbox" class="ant-checkbox-input" [attr.autofocus]="nzAutoFocus ? 'autofocus' : null" [attr.id]="nzId" [attr.name]="nzName || checkboxGroupComponent?.nzName()" [checked]="nzChecked" [ngModel]="nzChecked" [ngModelOptions]="{ standalone: true }" [disabled]="nzDisabled || (checkboxGroupComponent?.finalDisabled() ?? false)" (ngModelChange)="innerCheckedChange($event)" /> <span class="ant-checkbox-inner"></span> </span> <span><ng-content /></span> `, providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => NzCheckboxComponent), multi: true }], host: { class: "ant-checkbox-wrapper", "[class.ant-checkbox-group-item]": "!!checkboxGroupComponent", "[class.ant-checkbox-wrapper-in-form-item]": "!!nzFormStatusService", "[class.ant-checkbox-wrapper-checked]": "nzChecked", "[class.ant-checkbox-wrapper-disabled]": "nzDisabled || checkboxGroupComponent?.finalDisabled()", "[class.ant-checkbox-rtl]": `dir() === 'rtl'` }, imports: [FormsModule] }] }], ctorParameters: () => [], propDecorators: { inputElement: [{ type: ViewChild, args: ["inputElement", { static: true }] }], nzCheckedChange: [{ type: Output }], nzValue: [{ type: Input }], nzAutoFocus: [{ type: Input, args: [{ transform: booleanAttribute }] }], nzDisabled: [{ type: Input, args: [{ transform: booleanAttribute }] }], nzIndeterminate: [{ type: Input, args: [{ transform: booleanAttribute }] }], nzChecked: [{ type: Input, args: [{ transform: booleanAttribute }] }], nzId: [{ type: Input }], nzName: [{ type: Input }] } }); var NzCheckboxGroupComponent = class NzCheckboxGroupComponent { onChange = () => {}; onTouched = () => {}; isDisabledFirstChange = true; dir = inject(Directionality).valueSignal; nzName = input(null, ...ngDevMode ? [{ debugName: "nzName" }] : /* istanbul ignore next */ []); nzDisabled = input(false, { ...ngDevMode ? { debugName: "nzDisabled" } : /* istanbul ignore next */ {}, transform: booleanAttribute }); nzOptions = input([], ...ngDevMode ? [{ debugName: "nzOptions" }] : /* istanbul ignore next */ []); value = signal(null, ...ngDevMode ? [{ debugName: "value" }] : /* istanbul ignore next */ []); finalDisabled = linkedSignal(() => this.nzDisabled(), ...ngDevMode ? [{ debugName: "finalDisabled" }] : /* istanbul ignore next */ []); normalizedOptions = computed(() => normalizeOptions(this.nzOptions()), ...ngDevMode ? [{ debugName: "normalizedOptions" }] : /* istanbul ignore next */ []); constructor() { const elementRef = inject(ElementRef); const focusMonitor = inject(FocusMonitor); const destroyRef = inject(DestroyRef); afterNextRender(() => { focusMonitor.monitor(elementRef, true).pipe(takeUntilDestroyed(destroyRef)).subscribe((focusOrigin) => { if (!focusOrigin) this.onTouched(); }); destroyRef.onDestroy(() => { focusMonitor.stopMonitoring(elementRef); }); }); } writeValue(value) { untracked(() => { this.value.set(value); }); } registerOnChange(fn) { this.onChange = fn; } registerOnTouched(fn) { this.onTouched = fn; } setDisabledState(disabled) { untracked(() => { this.finalDisabled.set(this.isDisabledFirstChange && this.nzDisabled() || disabled); }); this.isDisabledFirstChange = false; } onCheckedChange(optionValue, checked) { if (this.finalDisabled()) return; this.value.update((value) => { if (checked) return value?.concat(optionValue) || [optionValue]; else return value?.filter((val) => val !== optionValue) || []; }); this.onChange(this.value()); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzCheckboxGroupComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "22.2.2", type: NzCheckboxGroupComponent, isStandalone: true, selector: "nz-checkbox-group", inputs: { nzName: { classPropertyName: "nzName", publicName: "nzName", isSignal: true, isRequired: false, transformFunction: null }, nzDisabled: { classPropertyName: "nzDisabled", publicName: "nzDisabled", isSignal: true, isRequired: false, transformFunction: null }, nzOptions: { classPropertyName: "nzOptions", publicName: "nzOptions", isSignal: true, isRequired: false, transformFunction: null } }, host: { properties: { "class.ant-checkbox-group-rtl": "dir() === 'rtl'" }, classAttribute: "ant-checkbox-group" }, providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => NzCheckboxGroupComponent), multi: true }, { provide: NZ_CHECKBOX_GROUP, useExisting: forwardRef(() => NzCheckboxGroupComponent) }], exportAs: ["nzCheckboxGroup"], ngImport: i0, template: ` <ng-content> @for (option of normalizedOptions(); track option.value) { <label nz-checkbox [nzValue]="option.value" [nzName]="nzName()" [nzDisabled]="option.disabled || finalDisabled()" > {{ option.label }} </label> } </ng-content> `, isInline: true, dependencies: [{ kind: "component", type: NzCheckboxComponent, selector: "[nz-checkbox]", inputs: [ "nzValue", "nzAutoFocus", "nzDisabled", "nzIndeterminate", "nzChecked", "nzId", "nzName" ], outputs: ["nzCheckedChange"], exportAs: ["nzCheckbox"] }], encapsulation: i0.ViewEncapsulation.None }); }; i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzCheckboxGroupComponent, decorators: [{ type: Component, args: [{ selector: "nz-checkbox-group", exportAs: "nzCheckboxGroup", imports: [NzCheckboxComponent], template: ` <ng-content> @for (option of normalizedOptions(); track option.value) { <label nz-checkbox [nzValue]="option.value" [nzName]="nzName()" [nzDisabled]="option.disabled || finalDisabled()" > {{ option.label }} </label> } </ng-content> `, providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => NzCheckboxGroupComponent), multi: true }, { provide: NZ_CHECKBOX_GROUP, useExisting: forwardRef(() => NzCheckboxGroupComponent) }], host: { class: "ant-checkbox-group", "[class.ant-checkbox-group-rtl]": `dir() === 'rtl'` }, encapsulation: ViewEncapsulation.None }] }], ctorParameters: () => [], propDecorators: { nzName: [{ type: i0.Input, args: [{ isSignal: true, alias: "nzName", required: false }] }], nzDisabled: [{ type: i0.Input, args: [{ isSignal: true, alias: "nzDisabled", required: false }] }], nzOptions: [{ type: i0.Input, args: [{ isSignal: true, alias: "nzOptions", required: false }] }] } }); function normalizeOptions(value) { return value.map((item) => { if (typeof item === "string" || typeof item === "number") return { label: `${item}`, value: item }; return item; }); } var NzCheckboxModule = class NzCheckboxModule { static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzCheckboxModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "22.2.2", ngImport: i0, type: NzCheckboxModule, imports: [NzCheckboxComponent, NzCheckboxGroupComponent], exports: [NzCheckboxComponent, NzCheckboxGroupComponent] }); static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzCheckboxModule, imports: [NzCheckboxComponent, NzCheckboxGroupComponent] }); }; i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzCheckboxModule, decorators: [{ type: NgModule, args: [{ imports: [NzCheckboxComponent, NzCheckboxGroupComponent], exports: [NzCheckboxComponent, NzCheckboxGroupComponent] }] }] }); export { NZ_CHECKBOX_GROUP, NzCheckboxComponent, NzCheckboxGroupComponent, NzCheckboxModule }; //# sourceMappingURL=ng-zorro-antd-checkbox.mjs.map