Skip to content

Commit 7c3faaf

Browse files
author
Boris Novikov
committed
fix: cursor position on auto update.
1 parent cad0abb commit 7c3faaf

File tree

2 files changed

+4055
-4469
lines changed

2 files changed

+4055
-4469
lines changed

packages/svelte-tel-input/src/lib/components/input/TelInput.svelte

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@
8888
formattedValue: string,
8989
initialCursorPosition: number
9090
) => {
91+
// If cursor was at the end of the original input, keep it at the end of the formatted input
92+
if (initialCursorPosition >= newValue.length) {
93+
return formattedValue.length;
94+
}
95+
96+
// For other cursor positions, use the existing logic to map cursor position
9197
let fvIndex = 0;
9298
for (let nvIndex = 0; nvIndex < initialCursorPosition; nvIndex++) {
9399
// Since newValue has not been normalized yet, we need to map any non standard digits.
@@ -155,6 +161,7 @@
155161
inputValue,
156162
initialCursorPosition
157163
);
164+
158165
el.selectionStart = newCursorPosition;
159166
el.selectionEnd = newCursorPosition;
160167
}
@@ -169,6 +176,7 @@
169176
inputValue,
170177
initialCursorPosition
171178
);
179+
172180
el.selectionStart = newCursorPosition;
173181
el.selectionEnd = newCursorPosition;
174182
}

0 commit comments

Comments
 (0)