diff --git a/projects/ngneat/dialog/src/lib/dialog.component.ts b/projects/ngneat/dialog/src/lib/dialog.component.ts index 5b60492..6610c1b 100644 --- a/projects/ngneat/dialog/src/lib/dialog.component.ts +++ b/projects/ngneat/dialog/src/lib/dialog.component.ts @@ -1,5 +1,5 @@ import { CommonModule, DOCUMENT } from '@angular/common'; -import { Component, ElementRef, inject, Inject, OnDestroy, OnInit, ViewChild, ViewEncapsulation } from '@angular/core'; +import { Component, ElementRef, inject, OnDestroy, OnInit, ViewChild, ViewEncapsulation } from '@angular/core'; import { fromEvent, merge, Subject } from 'rxjs'; import { filter, takeUntil } from 'rxjs/operators'; import { InternalDialogRef } from './dialog-ref'; diff --git a/projects/ngneat/dialog/src/lib/dialog.service.ts b/projects/ngneat/dialog/src/lib/dialog.service.ts index fbd706f..2cc9bde 100644 --- a/projects/ngneat/dialog/src/lib/dialog.service.ts +++ b/projects/ngneat/dialog/src/lib/dialog.service.ts @@ -10,13 +10,13 @@ import { Type, ViewRef, } from '@angular/core'; -import { BehaviorSubject, startWith, Subject } from 'rxjs'; +import { BehaviorSubject, Subject } from 'rxjs'; import { DialogRef, InternalDialogRef } from './dialog-ref'; import { DialogComponent } from './dialog.component'; import { DragOffset } from './draggable.directive'; import { DIALOG_CONFIG, DIALOG_DOCUMENT_REF, GLOBAL_DIALOG_CONFIG, NODES_TO_INSERT } from './providers'; import { AttachOptions, DialogConfig, ExtractData, ExtractResult, GlobalDialogConfig, OpenParams } from './types'; -import { map } from 'rxjs/operators'; +import { isDialogWithConfig } from './dialog.utils'; const OVERFLOW_HIDDEN_CLASS = 'ngneat-dialog-hidden'; @@ -56,8 +56,9 @@ export class DialogService { component: C, config?: Partial>>> ): DialogRef>, ExtractResult>>; - open(componentOrTemplate: any, config: Partial> = {}): DialogRef { - const mergedConfig = this.mergeConfig(config); + open(componentOrTemplate: Type | any, config: Partial> = {}): DialogRef { + const componentConfig = isDialogWithConfig(componentOrTemplate) ? componentOrTemplate.getModalConfig() : {}; + const mergedConfig = this.mergeConfig(config, componentConfig); mergedConfig.onOpen?.(); const dialogRef = new InternalDialogRef({ @@ -216,11 +217,15 @@ export class DialogService { }); } - private mergeConfig(inlineConfig: Partial): DialogConfig & GlobalDialogConfig { + private mergeConfig( + inlineConfig: Partial, + componentBasedConfig: Partial + ): DialogConfig & GlobalDialogConfig { return { ...this.globalConfig, id: nanoid(), ...inlineConfig, + ...componentBasedConfig, sizes: this.globalConfig?.sizes, } as DialogConfig & GlobalDialogConfig; } diff --git a/projects/ngneat/dialog/src/lib/dialog.utils.ts b/projects/ngneat/dialog/src/lib/dialog.utils.ts index 139effc..6150d85 100644 --- a/projects/ngneat/dialog/src/lib/dialog.utils.ts +++ b/projects/ngneat/dialog/src/lib/dialog.utils.ts @@ -1,3 +1,6 @@ +import { DialogWithConfig } from './types'; +import { Type } from '@angular/core'; + function isNil(value: unknown): value is undefined | null { return value === undefined || value === null; } @@ -13,3 +16,7 @@ export function coerceCssPixelValue(value: any): string { return isString(value) ? value : `${value}px`; } + +export function isDialogWithConfig(value: Type | any): value is typeof DialogWithConfig { + return value.prototype instanceof DialogWithConfig; +} diff --git a/projects/ngneat/dialog/src/lib/specs/dialog.service.spec.ts b/projects/ngneat/dialog/src/lib/specs/dialog.service.spec.ts index 4b1500a..a4e6372 100644 --- a/projects/ngneat/dialog/src/lib/specs/dialog.service.spec.ts +++ b/projects/ngneat/dialog/src/lib/specs/dialog.service.spec.ts @@ -5,6 +5,7 @@ import { mapTo, timer } from 'rxjs'; import { InternalDialogRef } from '../dialog-ref'; import { DialogService } from '../dialog.service'; import { DIALOG_DOCUMENT_REF, GLOBAL_DIALOG_CONFIG, provideDialogConfig } from '../providers'; +import { DialogWithConfig } from '../types'; class FakeFactoryResolver { componentOne = { @@ -236,6 +237,28 @@ describe('DialogService', () => { const attachSpyCalls = (fakeAppRef.attachView as jasmine.Spy).calls.allArgs(); expect(fakeAppRef.attachView).toHaveBeenCalledTimes(2); }); + + it('should respect dialog config with the highest priority', () => { + @Component({ selector: '' }) + class FakeComponentWithConfig extends DialogWithConfig { + constructor() { + super({ + data: { + testProperty: true, + }, + }); + } + } + + service.open(FakeComponentWithConfig, { + data: { + testProperty: false, + }, + }); + + const dialog = service.open(FakeComponentWithConfig); + expect(dialog.data.testProperty).toEqual(true); + }); }); describe('on close', () => { diff --git a/projects/ngneat/dialog/src/lib/types.ts b/projects/ngneat/dialog/src/lib/types.ts index ab42e92..31c6daf 100644 --- a/projects/ngneat/dialog/src/lib/types.ts +++ b/projects/ngneat/dialog/src/lib/types.ts @@ -77,3 +77,14 @@ export interface AttachOptions { attachToApp: boolean; config: DialogConfig; } + +export class DialogWithConfig { + private static _dialogConfig?: Partial; + static getModalConfig = () => { + return DialogWithConfig._dialogConfig; + }; + + constructor(dialogConfig: Partial) { + DialogWithConfig._dialogConfig = dialogConfig; + } +}