Skip to content

Commit

Permalink
feat(NbDialogService): support passing values to input/model signals
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
junglerobba authored and junglerobba committed Feb 16, 2025
1 parent 192a0de commit 3209993
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/framework/theme/components/dialog/dialog-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*/

import { InjectionToken, ViewContainerRef } from '@angular/core';
import { InjectionToken, InputSignal, Signal, ViewContainerRef } from '@angular/core';


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

type DialogData<T> = {
[K in keyof T]: ExtractInputSignalType<T[K]>;
};

type ExtractInputSignalType<Type> = Type extends InputSignal<infer X> ? X : ExcludeSignal<Type>;

type ExcludeSignal<Type> = Type extends Signal<unknown> ? never : Type;

/**
* Describes all available options that may be passed to the NbDialogService.
* */
Expand Down Expand Up @@ -56,7 +64,7 @@ export class NbDialogConfig<D = any> {
*/
viewContainerRef: ViewContainerRef;

context: D;
context: DialogData<D>;

constructor(config: Partial<NbDialogConfig>) {
Object.assign(this, config);
Expand Down
13 changes: 11 additions & 2 deletions src/framework/theme/components/dialog/dialog.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*/

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

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


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

if (config.context) {
Object.assign(dialogRef.componentRef.instance, { ...config.context })
for (const [key, value] of Object.entries({...config.context})) {
const instance = dialogRef.componentRef.instance;
const member = instance[key];
if (typeof member === 'function' && member[SIGNAL] !== undefined) {
instance[key] = signal(value).asReadonly();
} else {
instance[key] = value;
}
}
}
}
}
Expand Down

0 comments on commit 3209993

Please sign in to comment.