From 778949c9595a262e60f6c88450a3b6d3b40a9825 Mon Sep 17 00:00:00 2001 From: Morgan Touverey Quilling Date: Thu, 19 Dec 2019 22:55:56 +0100 Subject: [PATCH] feat(SwalComponent): add [swalVisible] input to reactively control modal visibility --- .../src/app/app.component.html | 21 +++++++++++++++++++ .../src/app/app.component.ts | 2 ++ .../ngx-sweetalert2/src/lib/swal.component.ts | 13 ++++++++++-- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/projects/ngx-sweetalert2-demo/src/app/app.component.html b/projects/ngx-sweetalert2-demo/src/app/app.component.html index 3da936a..90d436d 100644 --- a/projects/ngx-sweetalert2-demo/src/app/app.component.html +++ b/projects/ngx-sweetalert2-demo/src/app/app.component.html @@ -40,5 +40,26 @@

<swal [swalFireOnInit]="true"></swal>


+

<swal [swalVisible]="isSwalVisible"></swal>

+ + + +

+ [swalVisible] provides a reactive way to change the modal visibility, if you do not want to use + SwalComponent.fire() or SwalComponent.dismiss().
+ This example uses a simple variable, but another common use case could be using an observable, binding it like this: + <swal [swalVisible]="currentError$ | async" [title]="currentError$ | async">. Don't forget to reset + currentError$ after though! (maybe [swalFireOnInit] with an *ngIf would be + simpler here). +

+ + + + +
+ Load submodule diff --git a/projects/ngx-sweetalert2-demo/src/app/app.component.ts b/projects/ngx-sweetalert2-demo/src/app/app.component.ts index 20ef054..6768d84 100644 --- a/projects/ngx-sweetalert2-demo/src/app/app.component.ts +++ b/projects/ngx-sweetalert2-demo/src/app/app.component.ts @@ -7,4 +7,6 @@ import { Component } from '@angular/core'; }) export class AppComponent { public modalFireCondition = false; + + public isSwalVisible = false; } diff --git a/projects/ngx-sweetalert2/src/lib/swal.component.ts b/projects/ngx-sweetalert2/src/lib/swal.component.ts index ee0bfd5..2840422 100644 --- a/projects/ngx-sweetalert2/src/lib/swal.component.ts +++ b/projects/ngx-sweetalert2/src/lib/swal.component.ts @@ -139,7 +139,7 @@ export class SwalComponent implements OnInit, AfterViewInit, OnChanges, OnDestro * When left undefined (default), the value will be inherited from the module configuration, which is `false`. * * Example: - * + * */ @Input() public swalFireOnInit?: boolean; @@ -151,6 +151,15 @@ export class SwalComponent implements OnInit, AfterViewInit, OnChanges, OnDestro @Input() public swalDismissOnDestroy?: boolean; + @Input() + public set swalVisible(visible: boolean) { + visible ? this.fire() : this.dismiss(); + } + + public get swalVisible(): boolean { + return this.isCurrentlyShown; + } + /** * Emits an event when the modal DOM element has been created. * Useful to perform DOM mutations before the modal is shown. @@ -295,7 +304,7 @@ export class SwalComponent implements OnInit, AfterViewInit, OnChanges, OnDestro * Returns the SweetAlert2 promise for convenience and use in code behind templates. * Otherwise, (confirm)="myHandler($event)" and (cancel)="myHandler($event)" can be used in templates. */ - public async fire(): Promise { + public async fire(): Promise { const swal = await this.sweetAlert2Loader.swal; //=> Build the SweetAlert2 options