Skip to content

Commit 30f4a5d

Browse files
aefoxyggg
authored andcommitted
fix(window): Fixed window.component.ts creating incorrect context (#1266)
1 parent 8db4b29 commit 30f4a5d

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

src/framework/theme/components/window/window.component.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,8 @@ export class NbWindowComponent implements OnInit, AfterViewChecked, OnDestroy {
144144
}
145145

146146
protected attachTemplate() {
147-
this.overlayContainer.attachTemplatePortal(new NbTemplatePortal(this.content as TemplateRef<any>, null, {
148-
$implicit: this.context,
149-
}));
147+
this.overlayContainer
148+
.attachTemplatePortal(new NbTemplatePortal(this.content as TemplateRef<any>, null, this.context));
150149
}
151150

152151
protected attachComponent() {

src/framework/theme/components/window/window.service.spec.ts

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, ElementRef, NgModule } from '@angular/core';
1+
import { Component, ElementRef, NgModule, ViewChild, TemplateRef } from '@angular/core';
22
import { TestBed } from '@angular/core/testing';
33
import { NB_DOCUMENT } from '../../theme.options';
44
import { NbThemeModule } from '../../theme.module';
@@ -14,9 +14,36 @@ const WINDOW_CONTENT = 'window content';
1414
})
1515
class NbTestWindowComponent {}
1616

17+
@Component({
18+
selector: 'nb-test-window-with-template',
19+
template: `
20+
<ng-template #contentTemplate let-data>
21+
<p>Static text: {{ data.text }}</p>
22+
</ng-template>
23+
`,
24+
})
25+
class NbTestWindowWithTemplateComponent {
26+
@ViewChild('contentTemplate') contentTemplate: TemplateRef<any>;
27+
28+
constructor(private ws: NbWindowService) {}
29+
30+
openWindow() {
31+
return this.ws.open(
32+
this.contentTemplate,
33+
{ title: 'Window content from template', context: { text: 'hello world' } },
34+
);
35+
}
36+
}
37+
38+
@Component({
39+
selector: 'nb-test-window-with-component',
40+
template: `<p>window content {{ componentInput }}<p>`,
41+
})
42+
export class TestWindowComponent {}
43+
1744
@NgModule({
18-
declarations: [NbTestWindowComponent],
19-
entryComponents: [NbTestWindowComponent],
45+
declarations: [NbTestWindowComponent, NbTestWindowWithTemplateComponent, TestWindowComponent],
46+
entryComponents: [NbTestWindowComponent, NbTestWindowWithTemplateComponent, TestWindowComponent],
2047
})
2148
class NbTestWindowModule {}
2249

@@ -203,4 +230,32 @@ describe('window-service', () => {
203230
const windowElement: HTMLElement = windowRef.componentRef.location.nativeElement;
204231
expect(windowElement.querySelector('nb-card-body')).not.toBeNull();
205232
});
233+
234+
it(`should render window content from template with context`, function() {
235+
const fixture = TestBed.createComponent(NbTestWindowWithTemplateComponent);
236+
fixture.detectChanges();
237+
238+
const windowRef = fixture.componentInstance.openWindow();
239+
windowRef.componentRef.changeDetectorRef.detectChanges();
240+
expect(windowRef.componentRef).toBeDefined();
241+
242+
const windowElement: ElementRef<HTMLElement> = windowRef.componentRef.injector.get(ElementRef);
243+
expect(windowElement.nativeElement.innerText).toContain('Static text: hello world');
244+
});
245+
246+
it(`should render window content from component without context`, function() {
247+
const windowRef = windowService.open(TestWindowComponent);
248+
windowRef.componentRef.changeDetectorRef.detectChanges();
249+
250+
const windowElement: ElementRef<HTMLElement> = windowRef.componentRef.injector.get(ElementRef);
251+
expect(windowElement.nativeElement.innerText).toEqual('window content');
252+
});
253+
254+
it(`should render window content from component with context`, function() {
255+
const windowRef = windowService.open(TestWindowComponent, { context: { componentInput: 'hello world' }});
256+
windowRef.componentRef.changeDetectorRef.detectChanges();
257+
258+
const windowElement: ElementRef<HTMLElement> = windowRef.componentRef.injector.get(ElementRef);
259+
expect(windowElement.nativeElement.innerText).toEqual('window content hello world');
260+
});
206261
});

src/framework/theme/components/window/window.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ export class NbWindowService {
173173
const ref = this.windowsContainerViewRef.createComponent(windowFactory, null, injector);
174174
ref.instance.cfr = this.cfr;
175175
ref.changeDetectorRef.detectChanges();
176-
return ref
176+
return ref;
177177
}
178178

179179
protected subscribeToEvents(windowRef: NbWindowRef) {

0 commit comments

Comments
 (0)