dreyliky/ngx-os

Create reusable dialog box with dynamic content

Closed this issue · 7 comments

Am trying to create a reusable dialog box component with dynamic content (title, data, icon, buttons with callback functions on click).

I have failed, anyone can kindly help me out. See objective illustrated below

image

Hi.
To open window you can use DynamicWindowService, documentation:
https://ngx-os.io/components/window/documentation

Example:

class AppComponent {
    constructor(
        private readonly dynamicWindowService: DynamicWindowService
    ) {
        this.dynamicWindowService.open(MyReusableDialogBoxComponent, {
            title: 'Information',
            data: { description: 'Are you sure you want to exit application?', iconUrl: 'http://url-a' }
        }).afterClosed$.subscribe((result) => console.log(result));
        // Result with data based on which button we clicked (see code below)
    }
}

MyReusableDialogBoxComponent

@Component({
    selector: 'my-reusable-dialog-box',
    template: `
        <ng-container *ngIf="config$ | async as config">
            <img [attr.src]="config.data.iconUrl" />

            {{ config.data.description }}

            <button osButton (click)="onYesButtonClick()">Yes</button>

            <button osButton (click)="onNoButtonClick()">No</button>
        </ng-container>
    `,
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class MyReusableDialogBoxComponent {
    public readonly config$ = this.windowRef.config$;

    constructor(
        @Inject(DYNAMIC_WINDOW_REF) private readonly windowRef: DynamicWindowRef<MY_DATA_TYPE>
    ) {
        // You can update dynamically config with title & data using windowRef
        this.windowRef.updateConfig({ title: 'New title', data: { description: 'New description', iconUrl: 'http://url-b' } });
    }

    public onYesButtonClick(): void {
        this.windowRef.close('MY_YES_RESULT'); // Probably true
    }

    public onNoButtonClick(): void {
        this.windowRef.close('MY_NO_RESULT'); // Probably false
    }
}

this.dynamicWindowService.open method also returns DynamicWindowRef, so you can also updateConfig outside of your MyReusableDialogBoxComponent as well using this DynamicWindowRef.

Okay, let me try out that.. thanks

@ippezshelby I edited my answer a little. I removed the injection of SHARED_CONFIG. It was incorrect approach. Instead, we should get a concrete config of our window from the windowRef (not shared config of all windows).

@dreyliky Thanks, I checked it out and was able to make it work..

I was wondering though on how to have multi level menu structure. The current examples are for single level menus

@ippezshelby Currently library doesn't support a multi-level menu

@dreyliky how do i use this library in non-angular projects like in a native html application as a compile .js file.