Skip to content

Commit

Permalink
Fix _handleKeyEvent firing twice for paste events
Browse files Browse the repository at this point in the history
  • Loading branch information
jackocnr committed Mar 17, 2024
1 parent 2ea955e commit ae29c47
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 131 deletions.
12 changes: 4 additions & 8 deletions build/js/intlTelInput-jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@
userOverrideFormatting = false;
}
// handle FAYT, unless userOverrideFormatting or it's a paste event
if (_this6.options.formatAsYouType && !userOverrideFormatting && e.inputType !== "insertFromPaste") {
if (_this6.options.formatAsYouType && !userOverrideFormatting && !isPaste) {
// maintain caret position after reformatting
var currentCaretPos = _this6.telInput.selectionStart;
var valueBeforeCaret = _this6.telInput.value.substring(0, currentCaretPos);
Expand All @@ -844,14 +844,10 @@
_this6.telInput.setSelectionRange(newCaretPos, newCaretPos);
}
};
// this handles individual key presses as well as cut/paste events
// the advantage of the "input" event over "keyup" etc is that "input" only fires when the value changes,
// whereas "keyup" fires even for arrow key presses etc
this.telInput.addEventListener("input", this._handleKeyEvent);
// update flag on cut/paste events (now supported in all major browsers)
this._handleClipboardEvent = function() {
// hack because "paste" event is fired before input is updated
setTimeout(_this6._handleKeyEvent);
};
this.telInput.addEventListener("cut", this._handleClipboardEvent);
this.telInput.addEventListener("paste", this._handleClipboardEvent);
}
}, {
key: "_translateCursorPosition",
Expand Down
4 changes: 2 additions & 2 deletions build/js/intlTelInput-jquery.min.js

Large diffs are not rendered by default.

12 changes: 4 additions & 8 deletions build/js/intlTelInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@
userOverrideFormatting = false;
}
// handle FAYT, unless userOverrideFormatting or it's a paste event
if (_this6.options.formatAsYouType && !userOverrideFormatting && e.inputType !== "insertFromPaste") {
if (_this6.options.formatAsYouType && !userOverrideFormatting && !isPaste) {
// maintain caret position after reformatting
var currentCaretPos = _this6.telInput.selectionStart;
var valueBeforeCaret = _this6.telInput.value.substring(0, currentCaretPos);
Expand All @@ -839,14 +839,10 @@
_this6.telInput.setSelectionRange(newCaretPos, newCaretPos);
}
};
// this handles individual key presses as well as cut/paste events
// the advantage of the "input" event over "keyup" etc is that "input" only fires when the value changes,
// whereas "keyup" fires even for arrow key presses etc
this.telInput.addEventListener("input", this._handleKeyEvent);
// update flag on cut/paste events (now supported in all major browsers)
this._handleClipboardEvent = function() {
// hack because "paste" event is fired before input is updated
setTimeout(_this6._handleKeyEvent);
};
this.telInput.addEventListener("cut", this._handleClipboardEvent);
this.telInput.addEventListener("paste", this._handleClipboardEvent);
}
}, {
key: "_translateCursorPosition",
Expand Down
4 changes: 2 additions & 2 deletions build/js/intlTelInput.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions react/build/IntlTelInput.cjs.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions react/build/IntlTelInput.cjs.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions react/build/IntlTelInput.esm.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions react/build/IntlTelInput.esm.js.map

Large diffs are not rendered by default.

92 changes: 45 additions & 47 deletions react/demo/simple-bundle.js

Large diffs are not rendered by default.

92 changes: 45 additions & 47 deletions react/demo/validation-bundle.js

Large diffs are not rendered by default.

13 changes: 4 additions & 9 deletions src/js/intlTelInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ class Iti {
}

// handle FAYT, unless userOverrideFormatting or it's a paste event
if (this.options.formatAsYouType && !userOverrideFormatting && e.inputType !== "insertFromPaste") {
if (this.options.formatAsYouType && !userOverrideFormatting && !isPaste) {
// maintain caret position after reformatting
const currentCaretPos = this.telInput.selectionStart;
const valueBeforeCaret = this.telInput.value.substring(0, currentCaretPos);
Expand All @@ -814,15 +814,10 @@ class Iti {
this.telInput.setSelectionRange(newCaretPos, newCaretPos);
}
};
// this handles individual key presses as well as cut/paste events
// the advantage of the "input" event over "keyup" etc is that "input" only fires when the value changes,
// whereas "keyup" fires even for arrow key presses etc
this.telInput.addEventListener("input", this._handleKeyEvent);

// update flag on cut/paste events (now supported in all major browsers)
this._handleClipboardEvent = () => {
// hack because "paste" event is fired before input is updated
setTimeout(this._handleKeyEvent);
};
this.telInput.addEventListener("cut", this._handleClipboardEvent);
this.telInput.addEventListener("paste", this._handleClipboardEvent);
}

// iterate through the formattedValue until hit the right number of relevant chars
Expand Down

0 comments on commit ae29c47

Please sign in to comment.