-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(module:notification,message): refactor (#2613)
* refactor(module:notification,message): refactor * feat(module:notification): support update by key * docs: update notification docs * fix: fix lint
- Loading branch information
Showing
11 changed files
with
132 additions
and
42 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
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
order: 7 | ||
title: | ||
zh-CN: 更新消息内容 | ||
en-US: Update Notification Content | ||
--- | ||
|
||
## zh-CN | ||
|
||
可以通过唯一的 `nzKey` 来更新内容。 | ||
|
||
## en-US | ||
|
||
Update content with unique `nzKey`. | ||
|
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,28 @@ | ||
import { Component } from '@angular/core'; | ||
import { NzNotificationService } from 'ng-zorro-antd'; | ||
|
||
@Component({ | ||
selector: 'nz-demo-notification-update', | ||
template: ` | ||
<button nz-button [nzType]="'primary'" (click)="createAutoUpdatingNotifications()">Open the notification box</button> | ||
`, | ||
styles : [] | ||
}) | ||
export class NzDemoNotificationUpdateComponent { | ||
|
||
constructor(private notification: NzNotificationService) { | ||
} | ||
|
||
createAutoUpdatingNotifications(): void { | ||
this.notification.blank('Notification Title', 'Description.', { | ||
nzKey: 'key' | ||
} | ||
); | ||
|
||
setTimeout(() => { | ||
this.notification.blank('New Title', 'New description', { | ||
nzKey: 'key' | ||
}); | ||
}, 1000); | ||
} | ||
} |
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
47 changes: 42 additions & 5 deletions
47
components/notification/nz-notification-container.component.ts
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,19 +1,56 @@ | ||
import { Component, Inject, Optional } from '@angular/core'; | ||
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Inject, Optional, ViewEncapsulation } from '@angular/core'; | ||
|
||
import { NzMessageContainerComponent } from '../message/nz-message-container.component'; | ||
|
||
import { NzNotificationConfig, NZ_NOTIFICATION_CONFIG, NZ_NOTIFICATION_DEFAULT_CONFIG } from './nz-notification-config'; | ||
import { NzNotificationDataFilled } from './nz-notification.definitions'; | ||
|
||
@Component({ | ||
changeDetection : ChangeDetectionStrategy.OnPush, | ||
encapsulation : ViewEncapsulation.None, | ||
selector : 'nz-notification-container', | ||
preserveWhitespaces: false, | ||
templateUrl : './nz-notification-container.component.html' | ||
}) | ||
export class NzNotificationContainerComponent extends NzMessageContainerComponent { | ||
constructor( | ||
cdr: ChangeDetectorRef, | ||
@Optional() @Inject(NZ_NOTIFICATION_DEFAULT_CONFIG) defaultConfig: NzNotificationConfig, | ||
@Optional() @Inject(NZ_NOTIFICATION_CONFIG) config: NzNotificationConfig | ||
) { | ||
super(cdr, defaultConfig, config); | ||
} | ||
|
||
constructor(@Optional() @Inject(NZ_NOTIFICATION_DEFAULT_CONFIG) defaultConfig: NzNotificationConfig, | ||
@Optional() @Inject(NZ_NOTIFICATION_CONFIG) config: NzNotificationConfig) { | ||
super(defaultConfig, config); | ||
/** | ||
* A list of notifications displayed on the screen. | ||
* @override | ||
*/ | ||
messages: NzNotificationDataFilled[] = []; | ||
|
||
/** | ||
* Create a new notification. | ||
* If there's a notification whose `nzKey` is same with `nzKey` in `NzNotificationDataFilled`, replace its content instead of create a new one. | ||
* @override | ||
* @param notification | ||
*/ | ||
createMessage(notification: NzNotificationDataFilled): void { | ||
notification.options = this._mergeMessageOptions(notification.options); | ||
const key = notification.options.nzKey; | ||
const notificationWithSameKey = this.messages.find(msg => msg.options.nzKey === notification.options.nzKey); | ||
if (key && notificationWithSameKey) { | ||
this.replaceNotification(notificationWithSameKey, notification); | ||
} else { | ||
if (this.messages.length >= this.config.nzMaxStack) { | ||
this.messages.splice(0, 1); | ||
} | ||
this.messages.push(notification); | ||
} | ||
this.cdr.detectChanges(); | ||
} | ||
|
||
private replaceNotification(old: NzNotificationDataFilled, _new: NzNotificationDataFilled): void { | ||
old.title = _new.title; | ||
old.content = _new.content; | ||
old.template = _new.template; | ||
old.type = _new.type; | ||
} | ||
} |
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