scttcper / ngx-toastr

🍞 Angular Toastr
https://ngx-toastr.vercel.app
MIT License
2.52k stars 358 forks source link

Enhancement request: use generics with IndividualConfig #878

Open yl-endress opened 3 years ago

yl-endress commented 3 years ago

Hey guys,

thank you for this awesome work!

Currently, we need a custom toast with custom event handlers... So far i found here an approach: https://medium.com/@chen.reuven/ngx-toastr-with-custom-buttons-a23b90ab5a08 (here the mentioned sample: https://stackblitz.com/edit/ngx-custom-toastr-dynamic-buttons?embed=1&file=app/app.component.ts).

To my question: What do you think about using the IndividualConfig as a generic?

export declare class Toast<IC extends IndividualConfig = IndividualConfig> implements OnDestroy {
...
    options: IC;
...
}

In the stackblitz sample

    this.toastRef = this.toastr
      .show<MyCustomConfig>("Test", null, {
....
        // @ts-ignore <- wouldn't be needed
        buttons: this.toastButtons
      })
      .onAction.subscribe(x => {
        alert(`${x.title} is pressed`);
        console.log(x);
      });

the ts-ignore wouldn't be needed (app.component.ts line 36) and we would be able to add custom properties, too! Also, the origin functionality should work like this.

Cause of the default genneric assignment, the current behavior wouldn't be touched => following is still possible:

    this.toastr.show(...)
scttcper commented 3 years ago

if you're willing to make a pr, open to it