Skip to content

Commit

Permalink
feat(module:notification): support nzData as context in template
Browse files Browse the repository at this point in the history
close #2755
  • Loading branch information
Wendell committed Feb 19, 2019
1 parent abbf3b1 commit 0734cd1
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 9 deletions.
16 changes: 16 additions & 0 deletions components/notification/demo/template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
order: 8
title:
zh-CN: 使用模板
en-US: Use a template
---

## zh-CN

通过模板来实现更加复杂的效果和交互。

## en-US

Use template to implement more complex interactions.


36 changes: 36 additions & 0 deletions components/notification/demo/template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Component, TemplateRef, ViewChild } from '@angular/core';
import { NzNotificationService } from 'ng-zorro-antd';

@Component({
selector: 'nz-demo-notification-template',
template: `
<button nz-button [nzType]="'primary'" (click)="ninja()">Open the notification box</button>
<ng-template let-fruit="data">
It's a <nz-tag [nzColor]="fruit.color">{{ fruit.name }}</nz-tag>
<button nz-button nzType="small">Cut It!</button>
</ng-template>
`,
styles : [
`button {
margin-top: 8px;
}`
]
})
export class NzDemoNotificationTemplateComponent {
@ViewChild(TemplateRef) template: TemplateRef<{}>;

ninja(): void {
const fruits = [
{ name: 'Apple', color: 'red' },
{ name: 'Orange', color: 'orange' },
{ name: 'Watermelon', color: 'green' }
];

fruits.forEach(fruit => {
this.notificationService.template(this.template, { nzData: fruit });
});
}

constructor(private notificationService: NzNotificationService) {
}
}
1 change: 1 addition & 0 deletions components/notification/doc/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ The parameters that are set by the `options` support are as follows:
| nzAnimate | Whether to turn on animation | `boolean` |
| nzStyle | Custom inline style | `object` |
| nzClass | Custom CSS class | `object` |
| nzData | Anything that would be used as template context | `any` |

Methods for destruction are also provided:

Expand Down
2 changes: 2 additions & 0 deletions components/notification/doc/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ subtitle: 通知提醒框
| nzAnimate | 开关动画效果 | `boolean` |
| nzStyle | 自定义内联样式 | `object` |
| nzClass | 自定义 CSS class | `object` |
| nzData | 任何想要在模板中作为上下文的数据 | `any` |


还提供了全局销毁方法:

Expand Down
6 changes: 5 additions & 1 deletion components/notification/nz-notification.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
</div>
</div>
</div>
<ng-template [ngIf]="nzMessage.template" [ngTemplateOutlet]="nzMessage.template" [ngTemplateOutletContext]="{ $implicit: this }"></ng-template>
<ng-template
[ngIf]="nzMessage.template"
[ngTemplateOutlet]="nzMessage.template"
[ngTemplateOutletContext]="{ $implicit: this, data: nzMessage.options?.nzData }">
</ng-template>
<a tabindex="0" class="ant-notification-notice-close" (click)="close()">
<span class="ant-notification-notice-close-x">
<i nz-icon type="close" class="ant-notification-close-icon"></i>
Expand Down
11 changes: 6 additions & 5 deletions components/notification/nz-notification.definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ export interface NzNotificationData extends NzMessageData {
title?: string;
}

export interface NzNotificationDataOptions extends NzMessageDataOptions {
export interface NzNotificationDataOptions<T = {}> extends NzMessageDataOptions {
nzKey?: string;
/* tslint:disable-next-line:no-any */
nzStyle?: any;
/* tslint:disable-next-line:no-any */
nzClass?: any;
nzStyle?: any; // tslint:disable-line:no-any
nzClass?: any; // tslint:disable-line:no-any

/** Anything user wants renderer into a template. */
nzData?: T;
}

// Filled version of NzMessageData (includes more private properties)
Expand Down
7 changes: 4 additions & 3 deletions components/notification/nz-notification.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,11 @@ describe('NzNotification', () => {
expect(overlayContainerElement.querySelector('.ant-notification-topLeft')).not.toBeNull();
});

// Should support nzData as context.
it('should open a message box with template ref', () => {
messageService.template(demoAppFixture.componentInstance.demoTemplateRef);
messageService.template(demoAppFixture.componentInstance.demoTemplateRef, { nzData: 'data' });
demoAppFixture.detectChanges();
expect(overlayContainerElement.textContent).toContain('test template content');
expect(overlayContainerElement.textContent).toContain('test template contentdata');
});

it('should update an existing notification when keys are matched', () => {
Expand All @@ -179,7 +180,7 @@ describe('NzNotification', () => {
@Component({
selector: 'nz-demo-app-component',
template: `
<ng-template>{{ 'test template content' }}</ng-template>
<ng-template let-data="data">{{ 'test template content' }}{{ data }}</ng-template>
`
})
export class DemoAppComponent {
Expand Down

0 comments on commit 0734cd1

Please sign in to comment.