forked from NG-ZORRO/ng-zorro-antd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(module:alert): refactor alert (NG-ZORRO#4779)
- Loading branch information
Showing
10 changed files
with
157 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/** | ||
* @license | ||
* Copyright Alibaba.com All Rights Reserved. | ||
* | ||
* 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 | ||
*/ | ||
|
||
import { | ||
ChangeDetectionStrategy, | ||
ChangeDetectorRef, | ||
Component, | ||
EventEmitter, | ||
Input, | ||
OnChanges, | ||
OnDestroy, | ||
Output, | ||
SimpleChanges, | ||
TemplateRef, | ||
ViewEncapsulation | ||
} from '@angular/core'; | ||
import { InputBoolean, NzConfigService, slideAlertMotion, WithConfig } from 'ng-zorro-antd/core'; | ||
import { Subject } from 'rxjs'; | ||
import { takeUntil } from 'rxjs/operators'; | ||
|
||
const NZ_CONFIG_COMPONENT_NAME = 'alert'; | ||
|
||
@Component({ | ||
selector: 'nz-alert', | ||
exportAs: 'nzAlert', | ||
animations: [slideAlertMotion], | ||
template: ` | ||
<div | ||
*ngIf="!closed" | ||
class="ant-alert" | ||
[class.ant-alert-success]="nzType === 'success'" | ||
[class.ant-alert-info]="nzType === 'info'" | ||
[class.ant-alert-warning]="nzType === 'warning'" | ||
[class.ant-alert-error]="nzType === 'error'" | ||
[class.ant-alert-no-icon]="!nzShowIcon" | ||
[class.ant-alert-banner]="nzBanner" | ||
[class.ant-alert-closable]="nzCloseable" | ||
[class.ant-alert-with-description]="!!nzDescription" | ||
[@slideAlertMotion] | ||
(@slideAlertMotion.done)="onFadeAnimationDone()" | ||
> | ||
<ng-container *ngIf="nzShowIcon"> | ||
<i nz-icon class="ant-alert-icon" [nzType]="nzIconType || inferredIconType" [nzTheme]="iconTheme"></i> | ||
</ng-container> | ||
<span class="ant-alert-message" *ngIf="nzMessage"> | ||
<ng-container *nzStringTemplateOutlet="nzMessage">{{ nzMessage }}</ng-container> | ||
</span> | ||
<span class="ant-alert-description" *ngIf="nzDescription"> | ||
<ng-container *nzStringTemplateOutlet="nzDescription">{{ nzDescription }}</ng-container> | ||
</span> | ||
<button type="button" tabindex="0" *ngIf="nzCloseable || nzCloseText" class="ant-alert-close-icon" (click)="closeAlert()"> | ||
<ng-template #closeDefaultTemplate> | ||
<i nz-icon nzType="close"></i> | ||
</ng-template> | ||
<ng-container *ngIf="nzCloseText; else closeDefaultTemplate"> | ||
<ng-container *nzStringTemplateOutlet="nzCloseText"> | ||
<span class="ant-alert-close-text">{{ nzCloseText }}</span> | ||
</ng-container> | ||
</ng-container> | ||
</button> | ||
</div> | ||
`, | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
encapsulation: ViewEncapsulation.None, | ||
preserveWhitespaces: false | ||
}) | ||
export class NzAlertComponent implements OnChanges, OnDestroy { | ||
@Input() nzCloseText: string | TemplateRef<void> | null = null; | ||
@Input() nzIconType: string | null = null; | ||
@Input() nzMessage: string | TemplateRef<void> | null = null; | ||
@Input() nzDescription: string | TemplateRef<void> | null = null; | ||
@Input() nzType: 'success' | 'info' | 'warning' | 'error' = 'info'; | ||
@Input() @WithConfig(NZ_CONFIG_COMPONENT_NAME, false) @InputBoolean() nzCloseable: boolean; | ||
@Input() @WithConfig(NZ_CONFIG_COMPONENT_NAME, false) @InputBoolean() nzShowIcon: boolean; | ||
@Input() @InputBoolean() nzBanner = false; | ||
@Output() readonly nzOnClose = new EventEmitter<boolean>(); | ||
closed = false; | ||
iconTheme: 'outline' | 'fill' = 'fill'; | ||
inferredIconType: string = 'info-circle'; | ||
private isTypeSet = false; | ||
private isShowIconSet = false; | ||
private destroy$ = new Subject(); | ||
|
||
constructor(public nzConfigService: NzConfigService, private cdr: ChangeDetectorRef) { | ||
this.nzConfigService | ||
.getConfigChangeEventForComponent(NZ_CONFIG_COMPONENT_NAME) | ||
.pipe(takeUntil(this.destroy$)) | ||
.subscribe(() => { | ||
this.cdr.markForCheck(); | ||
}); | ||
} | ||
|
||
closeAlert(): void { | ||
this.closed = true; | ||
} | ||
|
||
onFadeAnimationDone(): void { | ||
if (this.closed) { | ||
this.nzOnClose.emit(true); | ||
} | ||
} | ||
|
||
ngOnChanges(changes: SimpleChanges): void { | ||
const { nzShowIcon, nzDescription, nzType, nzBanner } = changes; | ||
if (nzShowIcon) { | ||
this.isShowIconSet = true; | ||
} | ||
if (nzType) { | ||
this.isTypeSet = true; | ||
switch (this.nzType) { | ||
case 'error': | ||
this.inferredIconType = 'close-circle'; | ||
break; | ||
case 'success': | ||
this.inferredIconType = 'check-circle'; | ||
break; | ||
case 'info': | ||
this.inferredIconType = 'info-circle'; | ||
break; | ||
case 'warning': | ||
this.inferredIconType = 'exclamation-circle'; | ||
break; | ||
} | ||
} | ||
if (nzDescription) { | ||
this.iconTheme = this.nzDescription ? 'outline' : 'fill'; | ||
} | ||
if (nzBanner) { | ||
if (!this.isTypeSet) { | ||
this.nzType = 'warning'; | ||
} | ||
if (!this.isShowIconSet) { | ||
this.nzShowIcon = true; | ||
} | ||
} | ||
} | ||
ngOnDestroy(): void { | ||
this.destroy$.next(); | ||
this.destroy$.complete(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
@import './index.less'; | ||
@import './patch.less'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
nz-alert { | ||
display: block; | ||
} |