Skip to content

Commit

Permalink
fix: incorrect cursor position on delete.
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhinav Raut committed Jan 7, 2024
1 parent fdfed1e commit 0eb0434
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/livenumberformat.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 5 additions & 10 deletions livenumberformat.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ export default class LiveNumberFormat {

this.maxUndoStackSize = options.maxUndoStackSize || 500;

// To detect if delete key was pressed in the input event
this.deletePressed = false;

this.init();

if (this.input.value) {
Expand Down Expand Up @@ -203,10 +200,6 @@ export default class LiveNumberFormat {
oldRawValue = oldValue.slice(0, prevPos).replace(delimiterRegex, '');
newRawValue = newValue.slice(0, prevPos).replace(delimiterRegex, '');

// Delete key pressed before a delimiter, Move cursor 1 place to right
if (this.deletePressed && newFormattedValueAfterCursor.startsWith(delimiter)) {
return 1;
}

// some edge cases where cursor position needs to be adjusted
if (newRawValue.startsWith('-') && (oldRawValue.match(/[^0-9\.]/g))) {
Expand All @@ -229,8 +222,6 @@ export default class LiveNumberFormat {
}

handleKeydown (e) {
this.deletePressed = false;

if (e.key === "ArrowLeft" && !e.shiftKey) {
this.handleLeftArrow(e);
return;
Expand All @@ -244,7 +235,11 @@ export default class LiveNumberFormat {
this.performRedo(e);
return;
} else if (e.key === "Delete") {
this.deletePressed = true;
// skip if cursor is before a delimiter and delete is pressed
if (this.input.value[this.input.selectionStart] === this.delimiter) {
e.preventDefault();
this.setCursorPosition(this.input.selectionStart + 1);
}
return;
}

Expand Down

0 comments on commit 0eb0434

Please sign in to comment.