Skip to content

Commit 2000b1e

Browse files
committed
fix(alert): prevent both click and enter keyup from firing
1 parent 5844703 commit 2000b1e

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

ionic/components/alert/alert.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ class AlertCmp {
327327
private msgId: string;
328328
private inputType: string;
329329
private created: number;
330+
private lastClick: number;
330331

331332
constructor(
332333
private _viewCtrl: ViewController,
@@ -350,6 +351,7 @@ class AlertCmp {
350351
this.msgId = 'alert-msg-' + this.id;
351352
this.activeId = '';
352353
this.created = Date.now();
354+
this.lastClick = 0;
353355

354356
if (this.d.message) {
355357
this.descId = this.msgId;
@@ -418,9 +420,15 @@ class AlertCmp {
418420
private _keyUp(ev: KeyboardEvent) {
419421
if (this.isEnabled() && this._viewCtrl.isLast()) {
420422
if (ev.keyCode === 13) {
421-
console.debug('alert, enter button');
422-
let button = this.d.buttons[this.d.buttons.length - 1];
423-
this.btnClick(button);
423+
if (this.lastClick + 1000 < Date.now()) {
424+
// do not fire this click if there recently was already a click
425+
// this can happen when the button has focus and used the enter
426+
// key to click the button. However, both the click handler and
427+
// this keyup event will fire, so only allow one of them to go.
428+
console.debug('alert, enter button');
429+
let button = this.d.buttons[this.d.buttons.length - 1];
430+
this.btnClick(button);
431+
}
424432

425433
} else if (ev.keyCode === 27) {
426434
console.debug('alert, escape button');
@@ -446,6 +454,9 @@ class AlertCmp {
446454
return;
447455
}
448456

457+
// keep the time of the most recent button click
458+
this.lastClick = Date.now();
459+
449460
let shouldDismiss = true;
450461

451462
if (button.handler) {

0 commit comments

Comments
 (0)