2
0
mirror of https://github.com/tenrok/maska.git synced 2026-06-20 20:00:34 +03:00

fix: cursor position

Better work with eager and number masks
This commit is contained in:
Alexander Shabunevich
2024-05-12 20:11:25 +03:00
parent ff06ea6703
commit 3d6df57ec9
+9 -3
View File
@@ -98,7 +98,7 @@ export class MaskInput {
closure() closure()
// if pos is null, it means element does not support setSelectionRange // 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 if (pos === null || (pos === value.length && !isDelete)) return
const valueNew = input.value const valueNew = input.value
@@ -106,9 +106,15 @@ export class MaskInput {
const leftPartNew = valueNew.slice(0, pos) const leftPartNew = valueNew.slice(0, pos)
const unmasked = this.processInput(input, leftPart).unmasked const unmasked = this.processInput(input, leftPart).unmasked
const unmaskedNew = this.processInput(input, leftPartNew).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 { private setValue (input: HTMLInputElement, value: string): void {