Skip to content

Commit 30b35a5

Browse files
authored
Merge pull request #60 from cal-smith/master
fix(toast): add toast to banner.service and enable the close event
2 parents e0c261c + 97132d9 commit 30b35a5

File tree

7 files changed

+63
-35
lines changed

7 files changed

+63
-35
lines changed

src/banner/banner.component.spec.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import { TranslateModule, TranslateLoader, TranslateFakeLoader } from "@ngx-tran
33

44
import { StaticIconModule } from "./../icon/static-icon.module";
55

6-
import { Banner } from "./banner.component";
7-
import { BannerService } from "./banner.service";
6+
import { Banner, BannerService } from "./banner.module";
87

98

109
describe("Banner", () => {
@@ -32,6 +31,7 @@ describe("Banner", () => {
3231
const fixture = TestBed.createComponent(Banner);
3332
fixture.componentInstance.bannerObj = {
3433
type: "info",
34+
title: "sample",
3535
message: "sample message"
3636
};
3737
fixture.detectChanges();
@@ -44,6 +44,7 @@ describe("Banner", () => {
4444
const fixture = TestBed.createComponent(Banner);
4545
fixture.componentInstance.bannerObj = {
4646
type: "danger",
47+
title: "sample",
4748
message: "sample message"
4849
};
4950
fixture.detectChanges();
@@ -56,6 +57,7 @@ describe("Banner", () => {
5657
const fixture = TestBed.createComponent(Banner);
5758
fixture.componentInstance.bannerObj = {
5859
type: "warning",
60+
title: "sample",
5961
message: "sample message"
6062
};
6163
fixture.detectChanges();
@@ -68,6 +70,7 @@ describe("Banner", () => {
6870
const fixture = TestBed.createComponent(Banner);
6971
fixture.componentInstance.bannerObj = {
7072
type: "success",
73+
title: "sample",
7174
message: "sample message"
7275
};
7376
fixture.detectChanges();
@@ -80,6 +83,7 @@ describe("Banner", () => {
8083
const fixture = TestBed.createComponent(Banner);
8184
fixture.componentInstance.bannerObj = {
8285
type: "success",
86+
title: "sample",
8387
message: "sample message"
8488
};
8589
fixture.detectChanges();
@@ -93,6 +97,7 @@ describe("Banner", () => {
9397
const fixture = TestBed.createComponent(Banner);
9498
fixture.componentInstance.bannerObj = {
9599
type: "success",
100+
title: "sample",
96101
message: "sample message"
97102
};
98103
fixture.detectChanges();
@@ -109,6 +114,7 @@ describe("Banner", () => {
109114
const fixture = TestBed.createComponent(Banner);
110115
fixture.componentInstance.bannerObj = {
111116
type: "info",
117+
title: "sample",
112118
message: "sample message"
113119
};
114120
fixture.detectChanges();

src/banner/banner.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class Banner {
7373

7474
@ViewChild("banner") banner;
7575

76-
constructor(private bannerService: BannerService) {}
76+
constructor(protected bannerService: BannerService) {}
7777

7878
/**
7979
* Emits close event.

src/banner/banner.module.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,25 @@ import { TranslateModule } from "@ngx-translate/core";
44

55
import { StaticIconModule } from "./../icon/static-icon.module";
66

7-
import { Banner } from "./banner.component";
87
import { Toast } from "./toast.component";
8+
import { Banner } from "./banner.component";
9+
import { BannerService } from "./banner.service";
910

1011
export { BannerService } from "./banner.service";
1112
export { Banner } from "./banner.component";
1213
export { Toast } from "./toast.component";
1314

1415
@NgModule({
1516
declarations: [
16-
Banner,
17-
Toast
17+
Toast,
18+
Banner
1819
],
1920
exports: [
2021
Banner,
2122
Toast
2223
],
2324
entryComponents: [Banner, Toast],
24-
imports: [CommonModule, TranslateModule, StaticIconModule]
25+
imports: [CommonModule, TranslateModule, StaticIconModule],
26+
providers: [BannerService]
2527
})
2628
export class BannerModule {}

src/banner/banner.service.ts

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import {
99
OnDestroy
1010
} from "@angular/core";
1111

12-
import { Banner } from "./banner.component";
1312
import { BannerContent, NotificationContent, ToastContent } from "./banner-content.interface";
13+
import { Banner, Toast } from "./banner.module";
1414

1515
/**
1616
* Provides a way to use the banner component.
@@ -31,15 +31,6 @@ export class BannerService implements OnDestroy {
3131
public bannerRefs = new Array<ComponentRef<any>>();
3232
public onClose: EventEmitter<any> = new EventEmitter();
3333

34-
/**
35-
* Used to create banners.
36-
*
37-
* @private
38-
* @type {ComponentFactory<any>}
39-
* @memberof BannerService
40-
*/
41-
private componentFactory: ComponentFactory<any>;
42-
4334
/**
4435
* Constructs BannerService.
4536
*
@@ -82,18 +73,14 @@ export class BannerService implements OnDestroy {
8273
* }
8374
* ```
8475
*
85-
* @param {any} [bannerComp=null] If provided, used to resolve component factory
76+
* @param {any} [bannerComp=Banner] If provided, used to resolve component factory
8677
* @memberof BannerService
8778
*/
88-
showBanner(bannerObj: BannerContent | NotificationContent | ToastContent, bannerComp = null): Banner {
89-
if (!bannerComp) {
90-
this.componentFactory = this.componentFactoryResolver.resolveComponentFactory(Banner);
91-
} else {
92-
this.componentFactory = this.componentFactoryResolver.resolveComponentFactory(bannerComp);
93-
}
79+
showBanner(bannerObj: BannerContent | NotificationContent | ToastContent, bannerComp = Banner) {
80+
const componentFactory = this.componentFactoryResolver.resolveComponentFactory(bannerComp);
9481

95-
let bannerRef = this.componentFactory.create(this.injector);
96-
bannerRef.instance.bannerObj = bannerObj;
82+
let bannerRef = componentFactory.create(this.injector);
83+
bannerRef.instance.bannerObj = bannerObj as any; // typescript isn't being very smart here, so we type to any
9784
this.bannerRefs.push(bannerRef);
9885

9986
this.onClose = bannerRef.instance.close;
@@ -106,7 +93,7 @@ export class BannerService implements OnDestroy {
10693

10794
// get or create a container for alert list
10895
let bannerClassName = "banner-overlay";
109-
let bannerList = body.querySelector("." + bannerClassName);
96+
let bannerList = body.querySelector(`.${bannerClassName}`);
11097
if (!bannerList) {
11198
bannerList = document.createElement("div");
11299
bannerList.className = bannerClassName;
@@ -142,6 +129,10 @@ export class BannerService implements OnDestroy {
142129
return bannerRef.instance;
143130
}
144131

132+
showToast(bannerObj: BannerContent | NotificationContent | ToastContent, bannerComp = Toast) {
133+
return this.showBanner(bannerObj, bannerComp);
134+
}
135+
145136
/**
146137
* Programatically closes banner based on `bannerRef`.
147138
*

src/banner/banner.stories.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import { Component } from "@angular/core";
77

88
import { TranslateModule } from "@ngx-translate/core";
99

10-
import { BannerModule, BannerService } from "../";
11-
10+
import { BannerModule, BannerService } from "./banner.module";
1211

1312
@Component({
1413
selector: "app-banner-story",
@@ -19,7 +18,6 @@ import { BannerModule, BannerService } from "../";
1918
providers: [BannerService]
2019
})
2120
class BannnerStory {
22-
2321
constructor(private bannerService: BannerService) { }
2422

2523
showBanner() {
@@ -32,11 +30,34 @@ class BannnerStory {
3230
}
3331
}
3432

33+
@Component({
34+
selector: "app-toast-story",
35+
template: `
36+
<button class="bx--btn bx--btn--primary" (click)="showToast()">Show info toast</button>
37+
<div class="banner-container"></div>
38+
`,
39+
providers: [BannerService]
40+
})
41+
class ToastStory {
42+
constructor(private bannerService: BannerService) { }
43+
44+
showToast() {
45+
this.bannerService.showToast({
46+
type: "info",
47+
title: "Sample toast",
48+
subtitle: "Sample subtitle message",
49+
caption: "Sample caption",
50+
target: ".banner-container"
51+
});
52+
}
53+
}
54+
3555
storiesOf("Banner", module)
3656
.addDecorator(
3757
moduleMetadata({
3858
declarations: [
39-
BannnerStory
59+
BannnerStory,
60+
ToastStory
4061
],
4162
imports: [
4263
BannerModule,
@@ -62,9 +83,14 @@ storiesOf("Banner", module)
6283
template: `
6384
<ibm-toast [bannerObj]="{
6485
type: 'error',
65-
title: 'Sample banner',
86+
title: 'Sample toast',
6687
subtitle: 'Sample subtitle message',
6788
caption: 'Sample caption'
6889
}"></ibm-toast>
6990
`
91+
}))
92+
.add("Dynamic toast", () => ({
93+
template: `
94+
<app-toast-story></app-toast-story>
95+
`
7096
}));

src/banner/toast.component.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Component, Input } from "@angular/core";
22

3-
import { Banner } from "./banner.component";
43
import { BannerService } from "./banner.service";
54
import { ToastContent } from "./banner-content.interface";
5+
import { Banner } from "./banner.component";
66

77
/**
88
* Banner messages are displayed toward the top of the UI and do not interrupt user’s work.
@@ -22,7 +22,10 @@ import { ToastContent } from "./banner-content.interface";
2222
<p class="bx--toast-notification__subtitle" [innerHTML]="bannerObj.subtitle"></p>
2323
<p class="bx--toast-notification__caption" [innerHTML]="bannerObj.caption"></p>
2424
</div>
25-
<button class="bx--toast-notification__close-button" type="button">
25+
<button
26+
class="bx--toast-notification__close-button"
27+
type="button"
28+
(click)="onClose()">
2629
<svg
2730
class="bx--toast-notification-icon"
2831
aria-label="close"

webpack.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module.exports = [{
3030
},
3131
plugins: [
3232
new HtmlWebpackPlugin({
33-
template: '<html><body></body></html>'
33+
templateContent: '<html><body></body></html>'
3434
})
3535
],
3636
}];

0 commit comments

Comments
 (0)