UNPKG

ng-zorro-antd

Version:

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

379 lines (378 loc) • 11.1 kB
import { __esDecorate, __runInitializers } from "tslib"; import { FocusMonitor } from "@angular/cdk/a11y"; import { Directionality } from "@angular/cdk/bidi"; import { ENTER, LEFT_ARROW, RIGHT_ARROW, SPACE } from "@angular/cdk/keycodes"; import * as i0 from "@angular/core"; import { ChangeDetectorRef, Component, DestroyRef, ElementRef, Input, NgModule, NgZone, ViewChild, ViewEncapsulation, booleanAttribute, computed, forwardRef, inject, signal } from "@angular/core"; import { takeUntilDestroyed } from "@angular/core/rxjs-interop"; import { NG_VALUE_ACCESSOR } from "@angular/forms"; import { NzConfigService, WithConfig } from "ng-zorro-antd/core/config"; import { NZ_FORM_SIZE } from "ng-zorro-antd/core/form"; import * as i3 from "ng-zorro-antd/core/outlet"; import { NzOutletModule } from "ng-zorro-antd/core/outlet"; import { fromEventOutsideAngular } from "ng-zorro-antd/core/util"; import * as i1 from "ng-zorro-antd/core/wave"; import { NzWaveModule } from "ng-zorro-antd/core/wave"; import * as i2 from "ng-zorro-antd/icon"; import { NzIconModule } from "ng-zorro-antd/icon"; const NZ_CONFIG_MODULE_NAME = "switch"; let NzSwitchComponent = (() => { let _nzSize_decorators; let _nzSize_initializers = []; let _nzSize_extraInitializers = []; return class NzSwitchComponent { static { const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0; _nzSize_decorators = [WithConfig()]; __esDecorate(null, null, _nzSize_decorators, { kind: "field", name: "nzSize", static: false, private: false, access: { has: (obj) => "nzSize" in obj, get: (obj) => obj.nzSize, set: (obj, value) => { obj.nzSize = value; } }, metadata: _metadata }, _nzSize_initializers, _nzSize_extraInitializers); if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata }); } _nzModuleName = NZ_CONFIG_MODULE_NAME; nzConfigService = inject(NzConfigService); el = inject(ElementRef).nativeElement; ngZone = inject(NgZone); cdr = inject(ChangeDetectorRef); focusMonitor = inject(FocusMonitor); destroyRef = inject(DestroyRef); isChecked = false; onChange = () => {}; onTouched = () => {}; switchElement; nzLoading = false; nzDisabled = false; nzControl = false; nzCheckedChildren = null; nzUnCheckedChildren = null; nzSize = __runInitializers(this, _nzSize_initializers, "default"); nzId = (__runInitializers(this, _nzSize_extraInitializers), null); dir = inject(Directionality).valueSignal; isNzDisableFirstChange = true; size = signal(this.nzSize, ...ngDevMode ? [{ debugName: "size" }] : /* istanbul ignore next */ []); formSize = inject(NZ_FORM_SIZE, { optional: true }); finalSize = computed(() => this.formSize?.() || this.size(), ...ngDevMode ? [{ debugName: "finalSize" }] : /* istanbul ignore next */ []); updateValue(value) { if (this.isChecked !== value) { this.isChecked = value; this.onChange(this.isChecked); } } focus() { this.focusMonitor.focusVia(this.switchElement.nativeElement, "keyboard"); } blur() { this.switchElement.nativeElement.blur(); } constructor() { this.destroyRef.onDestroy(() => { this.focusMonitor.stopMonitoring(this.switchElement.nativeElement); }); } ngOnChanges(changes) { const { nzSize } = changes; if (nzSize) this.size.set(nzSize.currentValue); } ngOnInit() { fromEventOutsideAngular(this.el, "click").pipe(takeUntilDestroyed(this.destroyRef)).subscribe((event) => { event.preventDefault(); if (this.nzControl || this.nzDisabled || this.nzLoading) return; this.ngZone.run(() => { this.updateValue(!this.isChecked); this.cdr.markForCheck(); }); }); fromEventOutsideAngular(this.switchElement.nativeElement, "keydown").pipe(takeUntilDestroyed(this.destroyRef)).subscribe((event) => { if (this.nzControl || this.nzDisabled || this.nzLoading) return; const { keyCode } = event; if (keyCode !== LEFT_ARROW && keyCode !== RIGHT_ARROW && keyCode !== SPACE && keyCode !== ENTER) return; event.preventDefault(); this.ngZone.run(() => { if (keyCode === LEFT_ARROW) this.updateValue(false); else if (keyCode === RIGHT_ARROW) this.updateValue(true); else if (keyCode === SPACE || keyCode === ENTER) this.updateValue(!this.isChecked); this.cdr.markForCheck(); }); }); } ngAfterViewInit() { this.focusMonitor.monitor(this.switchElement.nativeElement, true).pipe(takeUntilDestroyed(this.destroyRef)).subscribe((focusOrigin) => { if (!focusOrigin) Promise.resolve().then(() => this.onTouched()); }); } writeValue(value) { this.isChecked = 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(); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzSwitchComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "22.2.2", type: NzSwitchComponent, isStandalone: true, selector: "nz-switch", inputs: { nzLoading: [ "nzLoading", "nzLoading", booleanAttribute ], nzDisabled: [ "nzDisabled", "nzDisabled", booleanAttribute ], nzControl: [ "nzControl", "nzControl", booleanAttribute ], nzCheckedChildren: "nzCheckedChildren", nzUnCheckedChildren: "nzUnCheckedChildren", nzSize: "nzSize", nzId: "nzId" }, providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => NzSwitchComponent), multi: true }], viewQueries: [{ propertyName: "switchElement", first: true, predicate: ["switchElement"], descendants: true, static: true }], exportAs: ["nzSwitch"], usesOnChanges: true, ngImport: i0, template: ` <button nz-wave type="button" class="ant-switch" #switchElement [attr.id]="nzId" [disabled]="nzDisabled" [class.ant-switch-checked]="isChecked" [class.ant-switch-loading]="nzLoading" [class.ant-switch-disabled]="nzDisabled" [class.ant-switch-small]="finalSize() === 'small'" [class.ant-switch-rtl]="dir() === 'rtl'" [nzWaveExtraNode]="true" > <span class="ant-switch-handle"> @if (nzLoading) { <nz-icon nzType="loading" class="ant-switch-loading-icon" /> } </span> <span class="ant-switch-inner"> @if (isChecked) { <ng-container *nzStringTemplateOutlet="nzCheckedChildren">{{ nzCheckedChildren }}</ng-container> } @else { <ng-container *nzStringTemplateOutlet="nzUnCheckedChildren">{{ nzUnCheckedChildren }}</ng-container> } </span> <div class="ant-click-animating-node"></div> </button> `, isInline: true, dependencies: [ { kind: "ngmodule", type: NzWaveModule }, { kind: "directive", type: i1.NzWaveDirective, selector: "[nz-wave],button[nz-button]:not([nzType=\"link\"]):not([nzType=\"text\"])", inputs: ["nzWaveExtraNode"], exportAs: ["nzWave"] }, { kind: "ngmodule", type: NzIconModule }, { kind: "directive", type: i2.NzIconDirective, selector: "nz-icon,[nz-icon]", inputs: [ "nzType", "nzTheme", "nzTwotoneColor", "nzSpin", "nzRotate", "nzIconfont", "aria-label" ], exportAs: ["nzIcon"] }, { kind: "ngmodule", type: NzOutletModule }, { kind: "directive", type: i3.NzStringTemplateOutletDirective, selector: "[nzStringTemplateOutlet]", inputs: ["nzStringTemplateOutletContext", "nzStringTemplateOutlet"], exportAs: ["nzStringTemplateOutlet"] } ], encapsulation: i0.ViewEncapsulation.None }); }; })(); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzSwitchComponent, decorators: [{ type: Component, args: [{ selector: "nz-switch", exportAs: "nzSwitch", encapsulation: ViewEncapsulation.None, providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => NzSwitchComponent), multi: true }], template: ` <button nz-wave type="button" class="ant-switch" #switchElement [attr.id]="nzId" [disabled]="nzDisabled" [class.ant-switch-checked]="isChecked" [class.ant-switch-loading]="nzLoading" [class.ant-switch-disabled]="nzDisabled" [class.ant-switch-small]="finalSize() === 'small'" [class.ant-switch-rtl]="dir() === 'rtl'" [nzWaveExtraNode]="true" > <span class="ant-switch-handle"> @if (nzLoading) { <nz-icon nzType="loading" class="ant-switch-loading-icon" /> } </span> <span class="ant-switch-inner"> @if (isChecked) { <ng-container *nzStringTemplateOutlet="nzCheckedChildren">{{ nzCheckedChildren }}</ng-container> } @else { <ng-container *nzStringTemplateOutlet="nzUnCheckedChildren">{{ nzUnCheckedChildren }}</ng-container> } </span> <div class="ant-click-animating-node"></div> </button> `, imports: [ NzWaveModule, NzIconModule, NzOutletModule ] }] }], ctorParameters: () => [], propDecorators: { switchElement: [{ type: ViewChild, args: ["switchElement", { static: true }] }], nzLoading: [{ type: Input, args: [{ transform: booleanAttribute }] }], nzDisabled: [{ type: Input, args: [{ transform: booleanAttribute }] }], nzControl: [{ type: Input, args: [{ transform: booleanAttribute }] }], nzCheckedChildren: [{ type: Input }], nzUnCheckedChildren: [{ type: Input }], nzSize: [{ type: Input }], nzId: [{ type: Input }] } }); var NzSwitchModule = class NzSwitchModule { static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzSwitchModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "22.2.2", ngImport: i0, type: NzSwitchModule, imports: [NzSwitchComponent], exports: [NzSwitchComponent] }); static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzSwitchModule, imports: [NzSwitchComponent] }); }; i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzSwitchModule, decorators: [{ type: NgModule, args: [{ imports: [NzSwitchComponent], exports: [NzSwitchComponent] }] }] }); export { NzSwitchComponent, NzSwitchModule }; //# sourceMappingURL=ng-zorro-antd-switch.mjs.map