Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(NbDialogService): support passing values to input/model signals #3278

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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