UNPKG

ng-zorro-antd

Version:

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

153 lines (147 loc) 7.4 kB
import { Directionality } from '@angular/cdk/bidi'; import * as i0 from '@angular/core'; import { inject, ChangeDetectorRef, Renderer2, ElementRef, DestroyRef, EventEmitter, booleanAttribute, Output, Input, ViewEncapsulation, ChangeDetectionStrategy, Component, NgModule } from '@angular/core'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { presetColors, statusColors, isPresetColor, isStatusColor } from 'ng-zorro-antd/core/color'; import * as i1 from 'ng-zorro-antd/icon'; import { NzIconModule } from 'ng-zorro-antd/icon'; /** * 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 NzTagComponent { cdr = inject(ChangeDetectorRef); renderer = inject(Renderer2); el = inject((ElementRef)).nativeElement; directionality = inject(Directionality); destroyRef = inject(DestroyRef); nzMode = 'default'; nzColor; nzChecked = false; nzBordered = true; nzOnClose = new EventEmitter(); nzCheckedChange = new EventEmitter(); dir = 'ltr'; isPresetColor = false; 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.el), this.el); } } clearPresetColor() { // /(ant-tag-(?:pink|red|...))/g const regexp = new RegExp(`(ant-tag-(?:${[...presetColors, ...statusColors].join('|')}))`, 'g'); const classname = this.el.classList.toString(); const matches = []; let match = regexp.exec(classname); while (match !== null) { matches.push(match[1]); match = regexp.exec(classname); } this.el.classList.remove(...matches); } setPresetColor() { this.clearPresetColor(); if (!this.nzColor) { this.isPresetColor = false; } else { this.isPresetColor = isPresetColor(this.nzColor) || isStatusColor(this.nzColor); } if (this.isPresetColor) { this.el.classList.add(`ant-tag-${this.nzColor}`); } } ngOnInit() { this.directionality.change?.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(direction => { this.dir = direction; this.cdr.detectChanges(); }); this.dir = this.directionality.value; } ngOnChanges(changes) { const { nzColor } = changes; if (nzColor) { this.setPresetColor(); } } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: NzTagComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "20.0.0", type: NzTagComponent, isStandalone: true, selector: "nz-tag", inputs: { nzMode: "nzMode", nzColor: "nzColor", nzChecked: ["nzChecked", "nzChecked", booleanAttribute], nzBordered: ["nzBordered", "nzBordered", booleanAttribute] }, 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'", "class.ant-tag-borderless": "!nzBordered" }, classAttribute: "ant-tag" }, exportAs: ["nzTag"], usesOnChanges: true, ngImport: i0, template: ` <ng-content></ng-content> @if (nzMode === 'closeable') { <nz-icon nzType="close" class="ant-tag-close-icon" tabindex="-1" (click)="closeTag($event)" /> } `, isInline: true, dependencies: [{ kind: "ngmodule", type: NzIconModule }, { kind: "directive", type: i1.NzIconDirective, selector: "nz-icon,[nz-icon]", inputs: ["nzSpin", "nzRotate", "nzType", "nzTheme", "nzTwotoneColor", "nzIconfont"], exportAs: ["nzIcon"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: NzTagComponent, decorators: [{ type: Component, args: [{ selector: 'nz-tag', exportAs: 'nzTag', template: ` <ng-content></ng-content> @if (nzMode === 'closeable') { <nz-icon nzType="close" class="ant-tag-close-icon" tabindex="-1" (click)="closeTag($event)" /> } `, 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'`, '[class.ant-tag-borderless]': `!nzBordered`, '(click)': 'updateCheckedStatus()' }, imports: [NzIconModule] }] }], propDecorators: { nzMode: [{ type: Input }], nzColor: [{ type: Input }], nzChecked: [{ type: Input, args: [{ transform: booleanAttribute }] }], nzBordered: [{ type: Input, args: [{ transform: booleanAttribute }] }], 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 { static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: NzTagModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "20.0.0", ngImport: i0, type: NzTagModule, imports: [NzTagComponent], exports: [NzTagComponent] }); static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: NzTagModule, imports: [NzTagComponent] }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.0.0", ngImport: i0, type: NzTagModule, decorators: [{ type: NgModule, args: [{ imports: [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