Skip to content

Commit 95247a4

Browse files
authored
feat(window): window may return result (#2869)
1 parent e8fa95b commit 95247a4

File tree

8 files changed

+102
-4
lines changed

8 files changed

+102
-4
lines changed

src/app/playground-components.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,6 +1521,12 @@ export const PLAYGROUND_COMPONENTS: ComponentLink[] = [
15211521
component: 'WindowTemplateTitleComponent',
15221522
name: 'Window Template Title',
15231523
},
1524+
{
1525+
path: 'window-result.component',
1526+
link: '/window/window-result.component',
1527+
component: 'WindowResultComponent',
1528+
name: 'Window Result',
1529+
},
15241530
],
15251531
},
15261532
{

src/framework/theme/components/window/window-ref.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { NbWindowConfig, NbWindowState, NbWindowStateChange } from './window.opt
99
* You can access rendered component as `componentRef` property of the windowRef.
1010
* Property `contentInstance` contains the instance of the component opened in the window.
1111
*/
12-
export class NbWindowRef<T = any> {
12+
export class NbWindowRef<T = any, R = any> {
1313
componentRef: ComponentRef<NbWindowComponent>;
1414
componentInstance: T;
1515

@@ -38,7 +38,7 @@ export class NbWindowRef<T = any> {
3838
}
3939

4040
protected _closed = false;
41-
protected closed$ = new Subject<void>();
41+
protected closed$ = new Subject<R>();
4242
/**
4343
* Emits when window was closed.
4444
*/
@@ -78,7 +78,7 @@ export class NbWindowRef<T = any> {
7878
/**
7979
* Closes window.
8080
* */
81-
close() {
81+
close(res?: R) {
8282
if (this._closed) {
8383
return;
8484
}
@@ -87,7 +87,7 @@ export class NbWindowRef<T = any> {
8787
this.componentRef.destroy();
8888
this.componentInstance = null;
8989
this.stateChange$.complete();
90-
this.closed$.next();
90+
this.closed$.next(res);
9191
this.closed$.complete();
9292
}
9393
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ import { NB_DOCUMENT } from '../../theme.options';
8686
*
8787
* @stacked-example(Window content from TemplateRef, window/template-window.component)
8888
*
89+
* You could pass the optional window return value to the `NbWindowRef.close` method.
90+
* The passed value would be emitted to the `NbWindowRef.onClose` listeners.
91+
*
92+
* @stacked-example(Result, window/window-result.component)
93+
*
8994
* ### Configuration
9095
*
9196
* As mentioned above, `open` method of the `NbWindowService` may receive optional configuration options.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* @license
3+
* Copyright Akveo. All Rights Reserved.
4+
* Licensed under the MIT License. See License.txt in the project root for license information.
5+
*/
6+
7+
import { Component } from '@angular/core';
8+
import { FormBuilder, FormGroup } from '@angular/forms';
9+
import { NbWindowRef } from '@nebular/theme';
10+
11+
@Component({
12+
template: `
13+
<form [formGroup]="form" (ngSubmit)="onSubmit()" class="form">
14+
<label for="name">Enter your name:</label>
15+
<input nbInput id="name" type="text" formControlName="name" />
16+
17+
<button nbButton type="submit" status="success">Submit</button>
18+
</form>
19+
`,
20+
})
21+
export class VisitorsFormComponent {
22+
form: FormGroup;
23+
24+
constructor(private fb: FormBuilder, public windowRef: NbWindowRef) {
25+
this.form = fb.group({
26+
name: null,
27+
});
28+
}
29+
30+
onSubmit() {
31+
this.windowRef.close(this.form.value.name);
32+
}
33+
34+
close() {
35+
this.windowRef.close();
36+
}
37+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* @license
3+
* Copyright Akveo. All Rights Reserved.
4+
* Licensed under the MIT License. See License.txt in the project root for license information.
5+
*/
6+
7+
import { Component } from '@angular/core';
8+
import { NbWindowService } from '@nebular/theme';
9+
import { VisitorsFormComponent } from './components/visitors-form.component';
10+
11+
@Component({
12+
template: `
13+
<button nbButton status="primary" (click)="openWindow()">Open window</button>
14+
<br />
15+
<h3 class="h5">Window visitors:</h3>
16+
<ul>
17+
<li *ngFor="let visitor of visitors">{{ visitor }}</li>
18+
</ul>
19+
`,
20+
styleUrls: ['./window.scss'],
21+
})
22+
export class WindowResultComponent {
23+
visitors: string[] = [];
24+
25+
constructor(private windowService: NbWindowService) {}
26+
27+
openWindow() {
28+
const windowRef = this.windowService.open(VisitorsFormComponent, { title: `Window` });
29+
30+
windowRef.onClose.subscribe((visitor) => this.visitors.push(visitor));
31+
}
32+
}

src/playground/with-layout/window/window-routing.module.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { WindowShowcaseComponent } from './window-showcase.component';
1111
import { WindowsBackdropComponent } from './windows-backdrop.component';
1212
import { WindowControlsComponent } from './window-controls.component';
1313
import { WindowTemplateTitleComponent } from './window-template-title.component';
14+
import { WindowResultComponent } from './window-result.component';
1415

1516
const routes: Route[] = [
1617
{
@@ -33,6 +34,10 @@ const routes: Route[] = [
3334
path: 'window-template-title.component',
3435
component: WindowTemplateTitleComponent,
3536
},
37+
{
38+
path: 'window-result.component',
39+
component: WindowResultComponent,
40+
},
3641
];
3742

3843
@NgModule({

src/playground/with-layout/window/window.module.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66

77
import { NgModule } from '@angular/core';
8+
import { CommonModule } from '@angular/common';
9+
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
810
import { NbButtonModule, NbCardModule, NbCheckboxModule, NbInputModule, NbWindowModule } from '@nebular/theme';
911

1012
import { WindowRoutingModule } from './window-routing.module';
@@ -14,6 +16,8 @@ import { WindowsBackdropComponent } from './windows-backdrop.component';
1416
import { FormComponent } from './components/form.component';
1517
import { WindowControlsComponent } from './window-controls.component';
1618
import { WindowTemplateTitleComponent } from './window-template-title.component';
19+
import { WindowResultComponent } from './window-result.component';
20+
import { VisitorsFormComponent } from './components/visitors-form.component';
1721

1822
@NgModule({
1923
declarations: [
@@ -23,8 +27,13 @@ import { WindowTemplateTitleComponent } from './window-template-title.component'
2327
FormComponent,
2428
WindowControlsComponent,
2529
WindowTemplateTitleComponent,
30+
WindowResultComponent,
31+
VisitorsFormComponent,
2632
],
2733
imports: [
34+
CommonModule,
35+
FormsModule,
36+
ReactiveFormsModule,
2837
NbWindowModule.forRoot(),
2938
NbButtonModule,
3039
NbInputModule,

src/playground/with-layout/window/window.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
.text-label {
1010
margin-top: 1.5rem;
1111
}
12+
13+
button {
14+
margin-top: 1.5rem;
15+
}
1216
}
1317

1418
button + button {

0 commit comments

Comments
 (0)