diff --git a/packages/core/src/components/alert-controller/alert-controller.tsx b/packages/core/src/components/alert-controller/alert-controller.tsx index 26b01b40d4..2a35c5f3b9 100644 --- a/packages/core/src/components/alert-controller/alert-controller.tsx +++ b/packages/core/src/components/alert-controller/alert-controller.tsx @@ -1,5 +1,5 @@ import { Component, Listen, Method } from '@stencil/core'; -import { Alert, AlertEvent, AlertOptions } from '../../index'; +import { AlertEvent, AlertOptions } from '../../index'; @Component({ @@ -8,10 +8,10 @@ import { Alert, AlertEvent, AlertOptions } from '../../index'; export class AlertController { private ids = 0; private alertResolves: { [alertId: string]: Function } = {}; - private alerts: Alert[] = []; + private alerts: HTMLIonAlertElement[] = []; @Method() - create(opts?: AlertOptions): Promise { + create(opts?: AlertOptions): Promise { // create ionic's wrapping ion-alert component const alert = document.createElement('ion-alert'); @@ -30,14 +30,14 @@ export class AlertController { appRoot.appendChild(alert as any); // store the resolve function to be called later up when the action sheet loads - return new Promise(resolve => { + return new Promise((resolve) => { this.alertResolves[alert.alertId] = resolve; }); } @Listen('body:ionAlertDidLoad') protected didLoad(ev: AlertEvent) { - const alert = ev.detail.alert; + const alert = ev.target as HTMLIonAlertElement; const alertResolve = this.alertResolves[alert.alertId]; if (alertResolve) { alertResolve(alert); @@ -46,13 +46,15 @@ export class AlertController { } @Listen('body:ionAlertWillPresent') - protected willPresent(ev: AlertEvent) { - this.alerts.push(ev.detail.alert); + protected willPresent(event: AlertEvent) { + console.log('willPresent: ', event); + this.alerts.push(event.target as HTMLIonAlertElement); } @Listen('body:ionAlertWillDismiss, body:ionAlertDidUnload') - protected willDismiss(ev: AlertEvent) { - const index = this.alerts.indexOf(ev.detail.alert); + protected willDismiss(event: AlertEvent) { + console.log('willDismiss: ', event); + const index = this.alerts.indexOf(event.target as HTMLIonAlertElement); if (index > -1) { this.alerts.splice(index, 1); } diff --git a/packages/core/src/components/alert/alert.tsx b/packages/core/src/components/alert/alert.tsx index 2f811c40e9..72cbb583ff 100644 --- a/packages/core/src/components/alert/alert.tsx +++ b/packages/core/src/components/alert/alert.tsx @@ -1,6 +1,7 @@ -import { Component, CssClassMap, Element, Event, EventEmitter, Listen, Prop } from '@stencil/core'; +import { Component, CssClassMap, Element, Event, EventEmitter, Listen, Method, Prop } from '@stencil/core'; import { Animation, AnimationBuilder, AnimationController, Config } from '../../index'; +import { playAnimationAsync } from '../../utils/helpers'; import iOSEnterAnimation from './animations/ios.enter'; import iOSLeaveAnimation from './animations/ios.leave'; @@ -68,22 +69,15 @@ export class Alert { @Prop() exitAnimation: AnimationBuilder; @Prop() alertId: string; - present() { - return new Promise(resolve => { - this._present(resolve); - }); - } - - private _present(resolve: Function) { + @Method() present() { if (this.animation) { this.animation.destroy(); this.animation = null; } - this.ionAlertWillPresent.emit({ alert: this }); + this.ionAlertWillPresent.emit(); // get the user's animation fn if one was provided let animationBuilder = this.enterAnimation; - if (!animationBuilder) { // user did not provide a custom animation fn // decide from the config which animation to use @@ -91,53 +85,44 @@ export class Alert { } // build the animation and kick it off - this.animationCtrl.create(animationBuilder, this.el).then(animation => { + return this.animationCtrl.create(animationBuilder, this.el).then(animation => { this.animation = animation; + return playAnimationAsync(animation); + }).then((animation) => { + animation.destroy(); + const firstInput = this.el.querySelector('[tabindex]') as HTMLElement; + if (firstInput) { + firstInput.focus(); + } - animation.onFinish((a: any) => { - a.destroy(); - - const firstInput = this.el.querySelector('[tabindex]') as HTMLElement; - if (firstInput) { - firstInput.focus(); - } - - this.componentDidEnter(); - resolve(); - }).play(); + this.ionAlertDidPresent.emit(); }); } - dismiss() { + @Method() dismiss() { if (this.animation) { this.animation.destroy(); this.animation = null; } - return new Promise(resolve => { - this.ionAlertWillDismiss.emit({ alert: this }); + this.ionAlertWillDismiss.emit(); - // get the user's animation fn if one was provided - let animationBuilder = this.exitAnimation; - if (!animationBuilder) { - // user did not provide a custom animation fn - // decide from the config which animation to use - animationBuilder = iOSLeaveAnimation; - } + // get the user's animation fn if one was provided + let animationBuilder = this.exitAnimation; + if (!animationBuilder) { + // user did not provide a custom animation fn + // decide from the config which animation to use + animationBuilder = iOSLeaveAnimation; + } - // build the animation and kick it off - this.animationCtrl.create(animationBuilder, this.el).then(animation => { - this.animation = animation; + return this.animationCtrl.create(animationBuilder, this.el).then(animation => { + this.animation = animation; + return playAnimationAsync(animation); + }).then((animation) => { + animation.destroy(); + this.ionAlertDidDismiss.emit(); - animation.onFinish((a: any) => { - a.destroy(); - this.ionAlertDidDismiss.emit({ alert: this }); - - Context.dom.write(() => { - this.el.parentNode.removeChild(this.el); - }); - - resolve(); - }).play(); + Context.dom.write(() => { + this.el.parentNode.removeChild(this.el); }); }); } @@ -161,7 +146,7 @@ export class Alert { this.dismiss(); } - + protected backdropClick() { if (this.enableBackdropDismiss) { // const opts: NavOptions = { @@ -470,9 +455,6 @@ export interface AlertButton { } export interface AlertEvent extends Event { - detail: { - alert: Alert; - }; } export { iOSEnterAnimation, iOSLeaveAnimation }; diff --git a/packages/core/src/components/alert/test/basic/index.html b/packages/core/src/components/alert/test/basic/index.html index a0e6b8def0..9fe741fddc 100644 --- a/packages/core/src/components/alert/test/basic/index.html +++ b/packages/core/src/components/alert/test/basic/index.html @@ -33,55 +33,57 @@