Skip to content

Commit f6aeb96

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 0609554 commit f6aeb96

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

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

Lines changed: 10 additions & 2 deletions
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

Lines changed: 11 additions & 2 deletions
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 { Inject, Injectable, Injector, TemplateRef, Type } from '@angular/core';
7+
import { Inject, Injectable, Injector, signal, TemplateRef, Type } from '@angular/core';
88
import { fromEvent as observableFromEvent } from 'rxjs';
99
import { filter, takeUntil } from 'rxjs/operators';
1010

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

2425

2526
/**
@@ -211,7 +212,15 @@ export class NbDialogService {
211212
dialogRef.componentRef = container.attachComponentPortal(portal);
212213

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

0 commit comments

Comments
 (0)