Skip to content

Commit eb3b1ba

Browse files
authored
feat(module:alert): support nzAction for customizing actions (#7246)
1 parent aeff540 commit eb3b1ba

File tree

7 files changed

+85
-1
lines changed

7 files changed

+85
-1
lines changed

components/alert/alert.component.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ const NZ_CONFIG_MODULE_NAME: NzConfigKey = 'alert';
6161
<ng-container *nzStringTemplateOutlet="nzDescription">{{ nzDescription }}</ng-container>
6262
</span>
6363
</div>
64+
<div class="ant-alert-action" *ngIf="nzAction">
65+
<ng-container *nzStringTemplateOutlet="nzAction">{{ nzAction }}</ng-container>
66+
</div>
6467
<button
6568
type="button"
6669
tabindex="0"
@@ -90,6 +93,7 @@ export class NzAlertComponent implements OnChanges, OnDestroy, OnInit {
9093
static ngAcceptInputType_nzBanner: BooleanInput;
9194
static ngAcceptInputType_nzNoAnimation: BooleanInput;
9295

96+
@Input() nzAction: string | TemplateRef<void> | null = null;
9397
@Input() nzCloseText: string | TemplateRef<void> | null = null;
9498
@Input() nzIconType: string | null = null;
9599
@Input() nzMessage: string | TemplateRef<void> | null = null;

components/alert/alert.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ describe('alert', () => {
104104
expect(alert.nativeElement.querySelector('.ant-alert').classList).toContain(`ant-alert-${type}`);
105105
});
106106
});
107+
it('should action work', () => {
108+
fixture.detectChanges();
109+
testComponent.action = testComponent.template;
110+
fixture.detectChanges();
111+
expect(alert.nativeElement.querySelector('.ant-alert-action').classList).not.toBeNull();
112+
});
107113
});
108114
describe('banner alert', () => {
109115
let fixture: ComponentFixture<NzDemoTestBannerComponent>;
@@ -149,12 +155,14 @@ describe('alert', () => {
149155
[nzShowIcon]="showIcon"
150156
[nzIconType]="iconType"
151157
[nzType]="type"
158+
[nzAction]="action"
152159
(nzOnClose)="onClose($event)"
153160
></nz-alert>
154161
`
155162
})
156163
export class NzDemoTestBasicComponent {
157164
@ViewChild('template', { static: false }) template!: TemplateRef<void>;
165+
action?: string | TemplateRef<void>;
158166
banner = false;
159167
closeable = false;
160168
closeText?: string | TemplateRef<void>;

components/alert/demo/action.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
order: 7
3+
title:
4+
zh-CN: 操作
5+
en-US: Custom action
6+
---
7+
8+
## zh-CN
9+
10+
可以在右上角自定义操作项。
11+
12+
## en-US
13+
14+
Custom action.

components/alert/demo/action.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { Component } from '@angular/core';
2+
3+
@Component({
4+
selector: 'nz-demo-alert-action',
5+
template: `
6+
<nz-alert nzShowIcon nzType="success" nzMessage="Success Text" [nzAction]="actionTemplate1"></nz-alert>
7+
<ng-template #actionTemplate1>
8+
<button nz-button nzSize="small" nzType="text" (click)="doAction('undo')">Undo</button></ng-template
9+
>
10+
<br />
11+
<nz-alert
12+
nzShowIcon
13+
nzType="error"
14+
nzMessage="Error Text"
15+
[nzDescription]="descriptionTemplate1"
16+
[nzAction]="actionTemplate2"
17+
></nz-alert>
18+
<ng-template #descriptionTemplate1>
19+
<p>Error Description Error Description Error Description Error Description</p>
20+
</ng-template>
21+
<ng-template #actionTemplate2>
22+
<button nz-button nzSize="small" nzType="default" nzDanger (click)="doAction('detail')">Detail</button>
23+
</ng-template>
24+
<br />
25+
<nz-alert nzCloseable nzType="warning" nzMessage="Warning Text" [nzAction]="actionTemplate3"></nz-alert>
26+
<ng-template #actionTemplate3>
27+
<button nz-button nzSize="small" nzType="primary" nzGhost (click)="doAction('ignore')">Ignore</button>
28+
</ng-template>
29+
<br />
30+
<nz-alert
31+
nzShowIcon
32+
nzType="info"
33+
nzMessage="Info Text"
34+
[nzDescription]="descriptionTemplate2"
35+
[nzAction]="actionTemplate4"
36+
></nz-alert>
37+
<ng-template #descriptionTemplate2>
38+
<p>Info Description Info Description Info Description Info Description</p>
39+
</ng-template>
40+
<ng-template #actionTemplate4>
41+
<nz-space nzDirection="vertical">
42+
<button *nzSpaceItem nz-button nzSize="small" nzType="primary" (click)="doAction('accept')">Accept</button>
43+
<button *nzSpaceItem nz-button nzSize="small" nzType="default" nzDanger (click)="doAction('decline')"
44+
>Decline</button
45+
>
46+
</nz-space>
47+
</ng-template>
48+
`
49+
})
50+
export class NzDemoAlertActionComponent {
51+
doAction(action: string): void {
52+
console.log(`Do alert's action: ${action}`);
53+
}
54+
}

components/alert/demo/module

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
import { NzAlertModule } from 'ng-zorro-antd/alert';
2+
import { NzButtonModule } from 'ng-zorro-antd/button';
3+
import { NzSpaceModule } from 'ng-zorro-antd/space';
24

3-
export const moduleList = [ NzAlertModule ];
5+
export const moduleList = [ NzAlertModule, NzButtonModule, NzSpaceModule ];

components/alert/doc/index.en-US.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { NzAlertModule } from 'ng-zorro-antd/alert';
2323
| Property | Description | Type | Default | Global Config |
2424
| -------- | ----------- | ---- | ------- | ------------- |
2525
| `[nzBanner]` | Whether to show as banner | `boolean` | `false` |
26+
| `[nzAction]` | Customized alert's action | `string \| TemplateRef<void>` | - |
2627
| `[nzCloseable]` | Whether Alert can be closed | `boolean` | - ||
2728
| `[nzCloseText]` | Close text to show | `string \| TemplateRef<void>` | - |
2829
| `[nzDescription]` | Additional content of Alert | `string \| TemplateRef<void>` | - |

components/alert/doc/index.zh-CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { NzAlertModule } from 'ng-zorro-antd/alert';
2424
| 参数 | 说明 | 类型 | 默认值 | 全局配置 |
2525
| --- | --- | --- | --- | --- |
2626
| `[nzBanner]` | 是否用作顶部公告 | `boolean` | `false` |
27+
| `[nzAction]` | 自定义操作项 | `string \| TemplateRef<void>` | - |
2728
| `[nzCloseable]` | 默认不显示关闭按钮 | `boolean` | - ||
2829
| `[nzCloseText]` | 自定义关闭按钮 | `string \| TemplateRef<void>` | - |
2930
| `[nzDescription]` | 警告提示的辅助性文字介绍 | `string \| TemplateRef<void>` | - |

0 commit comments

Comments
 (0)