Skip to content

Commit a88a78b

Browse files
committed
fix(cursor): fixes cursor jumping out of place when deleting from a dynamic mask
1 parent 9f5cc59 commit a88a78b

File tree

3 files changed

+11
-15
lines changed

3 files changed

+11
-15
lines changed

src/core.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,22 +95,22 @@ export function updateCursor(event, originalValue, originalPosition) {
9595
}
9696

9797
// get some information about the cursor based on the original value
98-
const pasting = event.inputType === 'insertFromPaste'
99-
const isCursorAtEnd = (event.data || pasting) && originalPosition == originalValue.length
100-
let insertedChar = originalValue[originalPosition - 1]
98+
const isInsertEvent = ['insertText', 'insertFromPaste'].includes(event.inputType)
99+
const wasCursorAtEnd = isInsertEvent && originalPosition == originalValue.length
100+
let lastInsertedChar = isInsertEvent && originalValue[originalPosition - 1]
101101

102102
const newValue = target.value.toLocaleLowerCase()
103103

104104
// set the cursor position to an appropriate location
105105
let cursorPosition = originalPosition
106-
if (isCursorAtEnd) {
106+
if (wasCursorAtEnd) {
107107
cursorPosition = newValue.length
108-
} else if (insertedChar) {
109-
insertedChar = insertedChar.toLocaleLowerCase()
108+
} else if (lastInsertedChar) {
109+
lastInsertedChar = lastInsertedChar.toLocaleLowerCase()
110110

111111
let newPosition = cursorPosition
112112
// if the last inserted char was changed, increment position until find it again
113-
while (newPosition <= newValue.length && newValue.charAt(newPosition - 1) !== insertedChar) {
113+
while (newPosition <= newValue.length && newValue.charAt(newPosition - 1) !== lastInsertedChar) {
114114
newPosition++
115115
}
116116
// if we didnt find the digit must be an unacceptable char, leave the cursor where it was

tests/directive.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ describe('Directive', () => {
9393
const newCursorPos = cursorPos + 1 // one new char inserted before
9494

9595
element.selectionEnd = cursorPos
96-
wrapper.find('input').trigger('input')
96+
wrapper.find('input').trigger('input', { inputType: 'insertText' })
9797

9898
expect(wrapper.element.setSelectionRange).toBeCalledWith(newCursorPos, newCursorPos)
9999
})
@@ -104,7 +104,7 @@ describe('Directive', () => {
104104
const newCursorPos = cursorPos + 2 // two new characters after masking
105105

106106
element.selectionEnd = cursorPos
107-
wrapper.find('input').trigger('input', { data: '3' })
107+
wrapper.find('input').trigger('input', { inputType: 'insertText' })
108108

109109
expect(wrapper.element.setSelectionRange).toBeCalledWith(newCursorPos, newCursorPos)
110110
})
@@ -115,7 +115,7 @@ describe('Directive', () => {
115115
const newCursorPos = cursorPos - 1 // needs to move back as 'j' is not an allowed char
116116

117117
element.selectionEnd = cursorPos
118-
wrapper.find('input').trigger('input', { data: 'J' })
118+
wrapper.find('input').trigger('input', { inputType: 'insertText' })
119119

120120
expect(wrapper.element.setSelectionRange).toBeCalledWith(newCursorPos, newCursorPos)
121121
})
@@ -128,7 +128,7 @@ describe('Directive', () => {
128128
element.value = 'ABC-1J|2'
129129
const cursorPos = element.value.indexOf('|')
130130
element.selectionEnd = cursorPos
131-
wrapper.find('input').trigger('input', { data: 'J' })
131+
wrapper.find('input').trigger('input', { inputType: 'insertText' })
132132
expect(wrapper.element.setSelectionRange).not.toBeCalled()
133133
})
134134
})

tests/formatter.test.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ test('1 -> [(#)]', () => {
1818
expect(formatter('1', { mask: '[(#)]' })).toMatchObject({ masked: '[(1)]', unmasked: '1' })
1919
})
2020

21-
test('1 -> #..#', () => {
22-
expect(formatter('1', { mask: '#..#', short: true })).toMatchObject({ masked: '1', unmasked: '1' })
23-
})
24-
2521
test('1 -> #.#', () => {
2622
expect(formatter('1', { mask: '#.#' })).toMatchObject({ masked: '1.', unmasked: '1' })
2723
})

0 commit comments

Comments
 (0)