Skip to content

Commit ace7359

Browse files
committed
feat: allow to define config in scope of the dialog component class instance
1 parent 3e412ea commit ace7359

File tree

5 files changed

+52
-6
lines changed

5 files changed

+52
-6
lines changed

projects/ngneat/dialog/src/lib/dialog.component.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CommonModule, DOCUMENT } from '@angular/common';
2-
import { Component, ElementRef, inject, Inject, OnDestroy, OnInit, ViewChild, ViewEncapsulation } from '@angular/core';
2+
import { Component, ElementRef, inject, OnDestroy, OnInit, ViewChild, ViewEncapsulation } from '@angular/core';
33
import { fromEvent, merge, Subject } from 'rxjs';
44
import { filter, takeUntil } from 'rxjs/operators';
55
import { InternalDialogRef } from './dialog-ref';

projects/ngneat/dialog/src/lib/dialog.service.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import {
1010
Type,
1111
ViewRef,
1212
} from '@angular/core';
13-
import { BehaviorSubject, startWith, Subject } from 'rxjs';
13+
import { BehaviorSubject, Subject } from 'rxjs';
1414
import { DialogRef, InternalDialogRef } from './dialog-ref';
1515
import { DialogComponent } from './dialog.component';
1616
import { DragOffset } from './draggable.directive';
1717
import { DIALOG_CONFIG, DIALOG_DOCUMENT_REF, GLOBAL_DIALOG_CONFIG, NODES_TO_INSERT } from './providers';
1818
import { AttachOptions, DialogConfig, ExtractData, ExtractResult, GlobalDialogConfig, OpenParams } from './types';
19-
import { map } from 'rxjs/operators';
19+
import { isDialogWithConfig } from './dialog.utils';
2020

2121
const OVERFLOW_HIDDEN_CLASS = 'ngneat-dialog-hidden';
2222

@@ -56,8 +56,9 @@ export class DialogService {
5656
component: C,
5757
config?: Partial<DialogConfig<ExtractData<InstanceType<C>>>>
5858
): DialogRef<ExtractData<InstanceType<C>>, ExtractResult<InstanceType<C>>>;
59-
open(componentOrTemplate: any, config: Partial<DialogConfig<any>> = {}): DialogRef {
60-
const mergedConfig = this.mergeConfig(config);
59+
open(componentOrTemplate: Type<unknown> | any, config: Partial<DialogConfig<any>> = {}): DialogRef {
60+
const componentConfig = isDialogWithConfig(componentOrTemplate) ? componentOrTemplate.getModalConfig() : {};
61+
const mergedConfig = this.mergeConfig(config, componentConfig);
6162
mergedConfig.onOpen?.();
6263

6364
const dialogRef = new InternalDialogRef({
@@ -216,11 +217,15 @@ export class DialogService {
216217
});
217218
}
218219

219-
private mergeConfig(inlineConfig: Partial<DialogConfig>): DialogConfig & GlobalDialogConfig {
220+
private mergeConfig(
221+
inlineConfig: Partial<DialogConfig>,
222+
componentBasedConfig: Partial<DialogConfig>
223+
): DialogConfig & GlobalDialogConfig {
220224
return {
221225
...this.globalConfig,
222226
id: nanoid(),
223227
...inlineConfig,
228+
...componentBasedConfig,
224229
sizes: this.globalConfig?.sizes,
225230
} as DialogConfig & GlobalDialogConfig;
226231
}

projects/ngneat/dialog/src/lib/dialog.utils.ts

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { DialogWithConfig } from './types';
2+
import { Type } from '@angular/core';
3+
14
function isNil(value: unknown): value is undefined | null {
25
return value === undefined || value === null;
36
}
@@ -13,3 +16,7 @@ export function coerceCssPixelValue(value: any): string {
1316

1417
return isString(value) ? value : `${value}px`;
1518
}
19+
20+
export function isDialogWithConfig(value: Type<unknown> | any): value is typeof DialogWithConfig {
21+
return value.prototype instanceof DialogWithConfig;
22+
}

projects/ngneat/dialog/src/lib/specs/dialog.service.spec.ts

+23
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { mapTo, timer } from 'rxjs';
55
import { InternalDialogRef } from '../dialog-ref';
66
import { DialogService } from '../dialog.service';
77
import { DIALOG_DOCUMENT_REF, GLOBAL_DIALOG_CONFIG, provideDialogConfig } from '../providers';
8+
import { DialogWithConfig } from '../types';
89

910
class FakeFactoryResolver {
1011
componentOne = {
@@ -236,6 +237,28 @@ describe('DialogService', () => {
236237
const attachSpyCalls = (fakeAppRef.attachView as jasmine.Spy).calls.allArgs();
237238
expect(fakeAppRef.attachView).toHaveBeenCalledTimes(2);
238239
});
240+
241+
it('should respect dialog config with the highest priority', () => {
242+
@Component({ selector: '' })
243+
class FakeComponentWithConfig extends DialogWithConfig {
244+
constructor() {
245+
super({
246+
data: {
247+
testProperty: true,
248+
},
249+
});
250+
}
251+
}
252+
253+
service.open(FakeComponentWithConfig, {
254+
data: {
255+
testProperty: false,
256+
},
257+
});
258+
259+
const dialog = service.open(FakeComponentWithConfig);
260+
expect(dialog.data.testProperty).toEqual(true);
261+
});
239262
});
240263

241264
describe('on close', () => {

projects/ngneat/dialog/src/lib/types.ts

+11
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,14 @@ export interface AttachOptions {
7777
attachToApp: boolean;
7878
config: DialogConfig;
7979
}
80+
81+
export class DialogWithConfig {
82+
private static _dialogConfig?: Partial<DialogConfig>;
83+
static getModalConfig = () => {
84+
return DialogWithConfig._dialogConfig;
85+
};
86+
87+
constructor(dialogConfig: Partial<DialogConfig>) {
88+
DialogWithConfig._dialogConfig = dialogConfig;
89+
}
90+
}

0 commit comments

Comments
 (0)