Skip to content

Commit cd6048d

Browse files
committed
feat(NbDialogService): support passing values to input/model signals
This makes an exception for input and model signals in the DialogConfig context, so the typehint will show as the type of the signal's value, but internally it will create a signal with the passed value. Technically these will just be readonly signals instead of input or model signals, but since components used as a dialog do not support regular outputs anyway, it should not matter. When used in a regular context the input signal will not be overwritten and work as usual. closes #3256 (I would not classify this as a bug but a missing feature instead)
1 parent 6b53c71 commit cd6048d

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/framework/theme/components/dialog/dialog-config.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,19 @@
44
* Licensed under the MIT License. See License.txt in the project root for license information.
55
*/
66

7-
import { InjectionToken, ViewContainerRef } from '@angular/core';
7+
import { InjectionToken, InputSignal, Signal, ViewContainerRef } from '@angular/core';
88

99

1010
export const NB_DIALOG_CONFIG = new InjectionToken<NbDialogConfig>('Default dialog options');
1111

12+
type DialogData<T> = {
13+
[K in keyof T]: ExtractInputSignalType<T[K]>;
14+
};
15+
16+
type ExtractInputSignalType<Type> = Type extends InputSignal<infer X> ? X : ExcludeSignal<Type>;
17+
18+
type ExcludeSignal<Type> = Type extends Signal<unknown> ? never : Type;
19+
1220
/**
1321
* Describes all available options that may be passed to the NbDialogService.
1422
* */
@@ -56,7 +64,7 @@ export class NbDialogConfig<D = any> {
5664
*/
5765
viewContainerRef: ViewContainerRef;
5866

59-
context: D;
67+
context: DialogData<D>;
6068

6169
constructor(config: Partial<NbDialogConfig>) {
6270
Object.assign(this, config);

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Licensed under the MIT License. See License.txt in the project root for license information.
55
*/
66

7-
import { ComponentFactoryResolver, Inject, Injectable, Injector, TemplateRef, Type } from '@angular/core';
7+
import { ComponentFactoryResolver, Inject, Injectable, Injector, signal, TemplateRef, Type } from '@angular/core';
88
import { fromEvent as observableFromEvent } from 'rxjs';
99
import { filter, takeUntil } from 'rxjs/operators';
1010

@@ -21,6 +21,7 @@ import { NB_DOCUMENT } from '../../theme.options';
2121
import { NB_DIALOG_CONFIG, NbDialogConfig } from './dialog-config';
2222
import { NbDialogRef } from './dialog-ref';
2323
import { NbDialogContainerComponent } from './dialog-container';
24+
import { SIGNAL } from '@angular/core/primitives/signals';
2425

2526

2627
/**
@@ -209,7 +210,15 @@ export class NbDialogService {
209210
dialogRef.componentRef = container.attachComponentPortal(portal);
210211

211212
if (config.context) {
212-
Object.assign(dialogRef.componentRef.instance, { ...config.context })
213+
for (const [key, value] of Object.entries({...config.context})) {
214+
const instance = dialogRef.componentRef.instance;
215+
const member = instance[key];
216+
if (typeof member === 'function' && member[SIGNAL] !== undefined) {
217+
instance[key] = signal(value).asReadonly();
218+
} else {
219+
instance[key] = value;
220+
}
221+
}
213222
}
214223
}
215224
}

0 commit comments

Comments
 (0)