Skip to content

Commit 479778a

Browse files
IraErshovavalorkin
authored andcommitted
fix(alerts): after dismissing alert are not removing from DOM #3608 (#3622)
Fix removing alert from DOM after its dismissing Close #3608
1 parent 7201fcd commit 479778a

File tree

6 files changed

+30
-14
lines changed

6 files changed

+30
-14
lines changed

cypress/integration/alerts_page_spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,10 @@ describe('Alerts page test suite', () => {
7272
});
7373

7474
it('alerts in dismissible example can all be closed and then resetting to default state', () => {
75-
cy.get(alertsDemos[3]).find('alert').as('dismissAlert').each(($alert, i) => {
76-
cy.get('@dismissAlert').eq(i).find('.close').click();
75+
cy.get(alertsDemos[3]).find('alert').as('dismissAlert').each(($alert) => {
76+
$alert.find('.close').click();
7777
});
78+
7879
cy.get('@dismissAlert')
7980
.should('not.to.have.descendants', 'div');
8081

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
<alert type="success" dismissOnTimeout="5000">
2-
<strong>Well done!</strong> You successfully read this important alert message.
3-
</alert>
4-
5-
<p>If you missed alert on top of me, just press <code>Add more</code> button</p>
1+
<p>If you missed alert under me, just press <code>Add more</code> button</p>
62
<div *ngFor="let alert of alerts">
7-
<alert [type]="alert.type" [dismissOnTimeout]="alert.timeout">{{ alert.msg }}</alert>
3+
<alert [type]="alert.type" [dismissOnTimeout]="alert.timeout" (onClosed)="onClosed(alert)">{{ alert.msg }}</alert>
84
</div>
95
<button type="button" class="btn btn-primary" (click)="add()">Add more</button>
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import { Component } from '@angular/core';
2+
import { AlertComponent } from 'ngx-bootstrap/alert/alert.component';
23

34
@Component({
45
selector: 'demo-alert-timeout',
56
templateUrl: './dismiss-on-timeout.html'
67
})
78
export class DemoAlertTimeoutComponent {
8-
alerts: any = [];
9+
alerts: any[] = [{
10+
type: 'success',
11+
msg: `Well done! You successfully read this important alert message. (added: ${new Date().toLocaleTimeString()})`,
12+
timeout: 5000
13+
}];
914

1015
add(): void {
1116
this.alerts.push({
@@ -14,4 +19,8 @@ export class DemoAlertTimeoutComponent {
1419
timeout: 5000
1520
});
1621
}
22+
23+
onClosed(dismissedAlert: AlertComponent): void {
24+
this.alerts = this.alerts.filter(alert => alert !== dismissedAlert);
25+
}
1726
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div *ngFor="let alert of alerts">
2-
<alert [type]="alert.type" [dismissible]="dismissible">{{ alert.msg }}</alert>
2+
<alert [type]="alert.type" [dismissible]="dismissible" (onClosed)="onClosed(alert)">{{ alert.msg }}</alert>
33
</div>
44
<button type="button" class="btn btn-primary" (click)="dismissible = !dismissible">Toggle dismissible</button>
55
<button type="button" class="btn btn-primary" (click)="reset()">Reset</button>

demo/src/app/components/+alerts/demos/dismiss/dismiss.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Component } from '@angular/core';
66
})
77
export class DemoAlertDismissComponent {
88
dismissible = true;
9-
alerts: any = [
9+
defaultAlerts: any[] = [
1010
{
1111
type: 'success',
1212
msg: `You successfully read this important alert message.`
@@ -20,8 +20,13 @@ export class DemoAlertDismissComponent {
2020
msg: `Better check yourself, you're not looking too good.`
2121
}
2222
];
23+
alerts = this.defaultAlerts;
2324

2425
reset(): void {
25-
this.alerts = this.alerts.map((alert: any) => Object.assign({}, alert));
26+
this.alerts = this.defaultAlerts;
27+
}
28+
29+
onClosed(dismissedAlert: any): void {
30+
this.alerts = this.alerts.filter(alert => alert !== dismissedAlert);
2631
}
2732
}

src/alert/alert.component.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import {
2-
ChangeDetectionStrategy, ChangeDetectorRef, Component, EventEmitter, Input, OnInit,
2+
ChangeDetectionStrategy,
3+
ChangeDetectorRef,
4+
Component,
5+
EventEmitter,
6+
Input,
7+
OnInit,
38
Output
49
} from '@angular/core';
510
import { AlertConfig } from './alert.config';
@@ -33,7 +38,7 @@ export class AlertComponent implements OnInit {
3338

3439

3540
classes = '';
36-
dismissibleChange: EventEmitter<boolean> = new EventEmitter<boolean>();
41+
dismissibleChange = new EventEmitter<boolean>();
3742

3843
constructor(_config: AlertConfig, private changeDetection: ChangeDetectorRef) {
3944
Object.assign(this, _config);

0 commit comments

Comments
 (0)