From 3d6df57ec9dafc9437adf9bb6ad30b5a6547bf52 Mon Sep 17 00:00:00 2001 From: Alexander Shabunevich Date: Sun, 12 May 2024 20:11:25 +0300 Subject: [PATCH] fix: cursor position Better work with eager and number masks --- packages/maska/src/input.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/maska/src/input.ts b/packages/maska/src/input.ts index 8ea30d3..3022489 100644 --- a/packages/maska/src/input.ts +++ b/packages/maska/src/input.ts @@ -98,7 +98,7 @@ export class MaskInput { closure() // if pos is null, it means element does not support setSelectionRange - // and when cursor at the end, process only on delete event + // and when cursor at the end, skip non-delete event if (pos === null || (pos === value.length && !isDelete)) return const valueNew = input.value @@ -106,9 +106,15 @@ export class MaskInput { const leftPartNew = valueNew.slice(0, pos) const unmasked = this.processInput(input, leftPart).unmasked const unmaskedNew = this.processInput(input, leftPartNew).unmasked - const newPos = pos + (unmasked.length - unmaskedNew.length) + let posFixed = pos - input.setSelectionRange(newPos, newPos) + if (leftPart !== leftPartNew) { + posFixed += isDelete + ? valueNew.length - value.length + : unmasked.length - unmaskedNew.length + } + + input.setSelectionRange(posFixed, posFixed) } private setValue (input: HTMLInputElement, value: string): void {