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

Enhancement: Add Dynamic Input Values Support to DynamicDialog #17037

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
5 changes: 3 additions & 2 deletions packages/primeng/src/dynamicdialog/dialogservice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ export class DialogService {
* @returns {DynamicDialogRef} DynamicDialog instance.
* @group Method
*/
public open<T>(componentType: Type<T>, config: DynamicDialogConfig): DynamicDialogRef<T> {
public open<T, DataType = any, InputValuesType extends Record<string, any> = {}>(componentType: Type<T>, config: DynamicDialogConfig<DataType, InputValuesType>): DynamicDialogRef<T> {
if (!this.duplicationPermission(componentType, config)) {
return null;
}

const dialogRef = this.appendDialogComponentToBody<T>(config, componentType);

this.dialogComponentRefMap.get(dialogRef).instance.childComponentType = componentType;

this.dialogComponentRefMap.get(dialogRef).instance.inputValues = config.inputValues;

return dialogRef;
}
/**
Expand Down
9 changes: 7 additions & 2 deletions packages/primeng/src/dynamicdialog/dynamicdialog-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ import { Type } from '@angular/core';
* Dialogs can be created dynamically with any component as the content using a DialogService.
* @group Components
*/
export class DynamicDialogConfig<T = any> {
export class DynamicDialogConfig<DataType = any, InputValuesType extends Record<string, any> = {}> {
/**
* An object to pass to the component loaded inside the Dialog.
* @group Props
*/
data?: T;
data?: DataType;
/**
* An object to pass to the component loaded inside the Dialog.
* @group Props
*/
inputValues?: InputValuesType;
/**
* Header text of the dialog.
* @group Props
Expand Down
9 changes: 9 additions & 0 deletions packages/primeng/src/dynamicdialog/dynamicdialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ export class DynamicDialogComponent extends BaseComponent implements AfterViewIn

childComponentType: Nullable<Type<any>>;

inputValues: Record<string, any>;

container: Nullable<HTMLDivElement>;

wrapper: Nullable<HTMLElement>;
Expand Down Expand Up @@ -348,6 +350,13 @@ export class DynamicDialogComponent extends BaseComponent implements AfterViewIn
viewContainerRef?.clear();

this.componentRef = viewContainerRef?.createComponent(componentType);

if (this.inputValues) {
Object.entries(this.inputValues).forEach(([key, value]) => {
this.componentRef.setInput(key, value);
});
}

this.dialogRef.onChildComponentLoaded.next(this.componentRef!.instance);
}

Expand Down