ng-zorro-antd
Version:
An enterprise-class UI components based on Ant Design and Angular
176 lines (170 loc) • 8.15 kB
JavaScript
import { __decorate } from 'tslib';
import * as i0 from '@angular/core';
import { EventEmitter, Component, ChangeDetectionStrategy, ViewEncapsulation, Optional, Input, Output, NgModule } from '@angular/core';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { presetColors, statusColors, isPresetColor, isStatusColor } from 'ng-zorro-antd/core/color';
import { InputBoolean } from 'ng-zorro-antd/core/util';
import * as i1 from '@angular/cdk/bidi';
import { BidiModule } from '@angular/cdk/bidi';
import * as i2 from '@angular/common';
import { CommonModule } from '@angular/common';
import * as i3 from 'ng-zorro-antd/icon';
import { NzIconModule } from 'ng-zorro-antd/icon';
import { FormsModule } from '@angular/forms';
class NzTagComponent {
constructor(cdr, renderer, elementRef, directionality) {
this.cdr = cdr;
this.renderer = renderer;
this.elementRef = elementRef;
this.directionality = directionality;
this.isPresetColor = false;
this.nzMode = 'default';
this.nzChecked = false;
this.nzOnClose = new EventEmitter();
this.nzCheckedChange = new EventEmitter();
this.dir = 'ltr';
this.destroy$ = new Subject();
}
updateCheckedStatus() {
if (this.nzMode === 'checkable') {
this.nzChecked = !this.nzChecked;
this.nzCheckedChange.emit(this.nzChecked);
}
}
closeTag(e) {
this.nzOnClose.emit(e);
if (!e.defaultPrevented) {
this.renderer.removeChild(this.renderer.parentNode(this.elementRef.nativeElement), this.elementRef.nativeElement);
}
}
clearPresetColor() {
const hostElement = this.elementRef.nativeElement;
// /(ant-tag-(?:pink|red|...))/g
const regexp = new RegExp(`(ant-tag-(?:${[...presetColors, ...statusColors].join('|')}))`, 'g');
const classname = hostElement.classList.toString();
const matches = [];
let match = regexp.exec(classname);
while (match !== null) {
matches.push(match[1]);
match = regexp.exec(classname);
}
hostElement.classList.remove(...matches);
}
setPresetColor() {
const hostElement = this.elementRef.nativeElement;
this.clearPresetColor();
if (!this.nzColor) {
this.isPresetColor = false;
}
else {
this.isPresetColor = isPresetColor(this.nzColor) || isStatusColor(this.nzColor);
}
if (this.isPresetColor) {
hostElement.classList.add(`ant-tag-${this.nzColor}`);
}
}
ngOnInit() {
var _a;
(_a = this.directionality.change) === null || _a === void 0 ? void 0 : _a.pipe(takeUntil(this.destroy$)).subscribe((direction) => {
this.dir = direction;
this.cdr.detectChanges();
});
this.dir = this.directionality.value;
}
ngOnChanges(changes) {
const { nzColor } = changes;
if (nzColor) {
this.setPresetColor();
}
}
ngOnDestroy() {
this.destroy$.next();
this.destroy$.complete();
}
}
NzTagComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.2.5", ngImport: i0, type: NzTagComponent, deps: [{ token: i0.ChangeDetectorRef }, { token: i0.Renderer2 }, { token: i0.ElementRef }, { token: i1.Directionality, optional: true }], target: i0.ɵɵFactoryTarget.Component });
NzTagComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "13.2.5", type: NzTagComponent, selector: "nz-tag", inputs: { nzMode: "nzMode", nzColor: "nzColor", nzChecked: "nzChecked" }, outputs: { nzOnClose: "nzOnClose", nzCheckedChange: "nzCheckedChange" }, host: { listeners: { "click": "updateCheckedStatus()" }, properties: { "style.background-color": "isPresetColor ? '' : nzColor", "class.ant-tag-has-color": "nzColor && !isPresetColor", "class.ant-tag-checkable": "nzMode === 'checkable'", "class.ant-tag-checkable-checked": "nzChecked", "class.ant-tag-rtl": "dir === 'rtl'" }, classAttribute: "ant-tag" }, exportAs: ["nzTag"], usesOnChanges: true, ngImport: i0, template: `
<ng-content></ng-content>
<i
nz-icon
nzType="close"
class="ant-tag-close-icon"
*ngIf="nzMode === 'closeable'"
tabindex="-1"
(click)="closeTag($event)"
></i>
`, isInline: true, directives: [{ type: i2.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { type: i3.NzIconDirective, selector: "[nz-icon]", inputs: ["nzSpin", "nzRotate", "nzType", "nzTheme", "nzTwotoneColor", "nzIconfont"], exportAs: ["nzIcon"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None });
__decorate([
InputBoolean()
], NzTagComponent.prototype, "nzChecked", void 0);
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.2.5", ngImport: i0, type: NzTagComponent, decorators: [{
type: Component,
args: [{
selector: 'nz-tag',
exportAs: 'nzTag',
preserveWhitespaces: false,
template: `
<ng-content></ng-content>
<i
nz-icon
nzType="close"
class="ant-tag-close-icon"
*ngIf="nzMode === 'closeable'"
tabindex="-1"
(click)="closeTag($event)"
></i>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
host: {
class: 'ant-tag',
'[style.background-color]': `isPresetColor ? '' : nzColor`,
'[class.ant-tag-has-color]': `nzColor && !isPresetColor`,
'[class.ant-tag-checkable]': `nzMode === 'checkable'`,
'[class.ant-tag-checkable-checked]': `nzChecked`,
'[class.ant-tag-rtl]': `dir === 'rtl'`,
'(click)': 'updateCheckedStatus()'
}
}]
}], ctorParameters: function () {
return [{ type: i0.ChangeDetectorRef }, { type: i0.Renderer2 }, { type: i0.ElementRef }, { type: i1.Directionality, decorators: [{
type: Optional
}] }];
}, propDecorators: { nzMode: [{
type: Input
}], nzColor: [{
type: Input
}], nzChecked: [{
type: Input
}], nzOnClose: [{
type: Output
}], nzCheckedChange: [{
type: Output
}] } });
/**
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/
class NzTagModule {
}
NzTagModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.2.5", ngImport: i0, type: NzTagModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
NzTagModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "13.2.5", ngImport: i0, type: NzTagModule, declarations: [NzTagComponent], imports: [BidiModule, CommonModule, FormsModule, NzIconModule], exports: [NzTagComponent] });
NzTagModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "13.2.5", ngImport: i0, type: NzTagModule, imports: [[BidiModule, CommonModule, FormsModule, NzIconModule]] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.2.5", ngImport: i0, type: NzTagModule, decorators: [{
type: NgModule,
args: [{
imports: [BidiModule, CommonModule, FormsModule, NzIconModule],
declarations: [NzTagComponent],
exports: [NzTagComponent]
}]
}] });
/**
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/
/**
* Generated bundle index. Do not edit.
*/
export { NzTagComponent, NzTagModule };
//# sourceMappingURL=ng-zorro-antd-tag.mjs.map