Skip to content

Commit cc014a1

Browse files
authored
feat(module: alert): support custom icon (#7691)
1 parent 616f59f commit cc014a1

File tree

7 files changed

+94
-6
lines changed

7 files changed

+94
-6
lines changed

components/alert/alert.component.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,14 @@ const NZ_CONFIG_MODULE_NAME: NzConfigKey = 'alert';
5050
[@slideAlertMotion]
5151
(@slideAlertMotion.done)="onFadeAnimationDone()"
5252
>
53-
<ng-container *ngIf="nzShowIcon">
54-
<span nz-icon class="ant-alert-icon" [nzType]="nzIconType || inferredIconType" [nzTheme]="iconTheme"></span>
55-
</ng-container>
53+
<div *ngIf="nzShowIcon" class="ant-alert-icon">
54+
<ng-container *ngIf="nzIcon; else iconDefaultTemplate">
55+
<ng-container *nzStringTemplateOutlet="nzIcon"></ng-container>
56+
</ng-container>
57+
<ng-template #iconDefaultTemplate>
58+
<span nz-icon [nzType]="nzIconType || inferredIconType" [nzTheme]="iconTheme"></span>
59+
</ng-template>
60+
</div>
5661
<div class="ant-alert-content" *ngIf="nzMessage || nzDescription">
5762
<span class="ant-alert-message" *ngIf="nzMessage">
5863
<ng-container *nzStringTemplateOutlet="nzMessage">{{ nzMessage }}</ng-container>
@@ -103,6 +108,7 @@ export class NzAlertComponent implements OnChanges, OnDestroy, OnInit {
103108
@Input() @WithConfig() @InputBoolean() nzShowIcon: boolean = false;
104109
@Input() @InputBoolean() nzBanner = false;
105110
@Input() @InputBoolean() nzNoAnimation = false;
111+
@Input() nzIcon: string | TemplateRef<void> | null = null;
106112
@Output() readonly nzOnClose = new EventEmitter<boolean>();
107113
closed = false;
108114
iconTheme: 'outline' | 'fill' = 'fill';

components/alert/alert.spec.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ describe('alert', () => {
1414
waitForAsync(() => {
1515
TestBed.configureTestingModule({
1616
imports: [BidiModule, NzAlertModule, NoopAnimationsModule, NzIconTestModule],
17-
declarations: [NzDemoTestBasicComponent, NzDemoTestBannerComponent, NzTestAlertRtlComponent]
17+
declarations: [
18+
NzDemoTestBasicComponent,
19+
NzDemoTestBannerComponent,
20+
NzTestAlertRtlComponent,
21+
NzTestAlertCustomIconComponent
22+
]
1823
});
1924
TestBed.compileComponents();
2025
})
@@ -93,8 +98,10 @@ describe('alert', () => {
9398
testComponent.showIcon = true;
9499
testComponent.iconType = 'lock';
95100
fixture.detectChanges();
96-
expect(alert.nativeElement.querySelector('.ant-alert-icon').classList).toContain('anticon');
97-
expect(alert.nativeElement.querySelector('.ant-alert-icon').classList).toContain('anticon-lock');
101+
expect(alert.nativeElement.querySelector('.ant-alert-icon').firstElementChild.classList).toContain('anticon');
102+
expect(alert.nativeElement.querySelector('.ant-alert-icon').firstElementChild.classList).toContain(
103+
'anticon-lock'
104+
);
98105
});
99106
it('should type work', () => {
100107
const listOfType = ['success', 'info', 'warning', 'error'];
@@ -139,6 +146,15 @@ describe('alert', () => {
139146
expect(alert.nativeElement.firstElementChild!.classList).not.toContain('ant-alert-rtl');
140147
});
141148
});
149+
describe('custom icon', () => {
150+
it('should custom icon work', () => {
151+
const fixture = TestBed.createComponent(NzTestAlertCustomIconComponent);
152+
const alert = fixture.debugElement.query(By.directive(NzAlertComponent));
153+
fixture.detectChanges();
154+
expect(alert.nativeElement.querySelector('.ant-alert-icon')).toBeDefined();
155+
expect(alert.nativeElement.querySelector('.ant-alert-icon').firstElementChild).not.toContain('anticon');
156+
});
157+
});
142158
});
143159

144160
@Component({
@@ -190,3 +206,20 @@ export class NzTestAlertRtlComponent {
190206
@ViewChild(Dir) dir!: Dir;
191207
direction = 'rtl';
192208
}
209+
210+
@Component({
211+
template: `
212+
<nz-alert
213+
nzType="success"
214+
nzMessage="Success Tips"
215+
nzDescription="Detailed description and advices about successful copywriting."
216+
[nzIcon]="customIconTemplate"
217+
nzShowIcon
218+
></nz-alert>
219+
220+
<ng-template #customIconTemplate>
221+
<div> S </div>
222+
</ng-template>
223+
`
224+
})
225+
export class NzTestAlertCustomIconComponent {}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
order: 8
3+
title:
4+
zh-CN: 自定义图标
5+
en-US: Custom icon
6+
---
7+
8+
## zh-CN
9+
10+
让信息类型更加醒目, 满足定制化需求。
11+
12+
13+
## en-US
14+
15+
Make information more clear and more friendly and meet customized needs.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Component } from '@angular/core';
2+
3+
@Component({
4+
selector: 'nz-demo-alert-custom-icon',
5+
template: `
6+
<nz-alert
7+
nzType="success"
8+
nzMessage="Success Tips"
9+
nzDescription="Detailed description and advices about successful copywriting."
10+
[nzIcon]="customIconTemplate"
11+
nzShowIcon
12+
></nz-alert>
13+
14+
<ng-template #customIconTemplate>
15+
<div> S </div>
16+
</ng-template>
17+
`,
18+
styles: [
19+
`
20+
nz-alert {
21+
margin-bottom: 16px;
22+
}
23+
`
24+
]
25+
})
26+
export class NzDemoAlertCustomIconComponent {}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ import { NzAlertModule } from 'ng-zorro-antd/alert';
3131
| `[nzShowIcon]` | Whether to show icon, in `nzBanner` mode default is `true` | `boolean` | `false` ||
3232
| `[nzIconType]` | Icon type, effective when `nzShowIcon` is `true` | `string` | - |
3333
| `[nzType]` | Type of Alert styles, in `nzBanner` mode default is `'warning'` | `'success' \| 'info' \| 'warning' \| 'error'` | `'info'` |
34+
| `[nzIcon]` | Custom icon, effective when showIcon is true | `string \| TemplateRef<void>` | - |
3435
| `(nzOnClose)` | Callback when Alert is closed | `EventEmitter<void>` | - |

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@ import { NzAlertModule } from 'ng-zorro-antd/alert';
3232
| `[nzShowIcon]` | 是否显示辅助图标,`nzBanner` 模式下默认值为 `true` | `boolean` | `false` ||
3333
| `[nzIconType]` | 自定义图标类型,`nzShowIcon``true` 时有效 | `string` | - |
3434
| `[nzType]` | 指定警告提示的样式,`nzBanner` 模式下默认值为 `'warning'` | `'success' \| 'info' \| 'warning' \| 'error'` | `'info'` |
35+
| `[nzIcon]` | 自定义图标,showIcon 为 true 时有效 | `string \| TemplateRef<void>` | - |
3536
| `(nzOnClose)` | 关闭时触发的回调函数 | `EventEmitter<void>` | - |

components/alert/style/patch.less

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
nz-alert {
22
display: block;
33
}
4+
5+
.@{alert-prefix-cls} {
6+
&-icon {
7+
line-height: 1;
8+
}
9+
}

0 commit comments

Comments
 (0)