Skip to content

Commit 919d98d

Browse files
author
Dipak Sarkar
committed
fix cursor position
1 parent 608ecf8 commit 919d98d

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

src/core.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,11 @@ export function inputHandler(event) {
116116
positionFromEnd = target.value.length - positionFromEnd
117117
positionFromEnd = Math.max(positionFromEnd, config.prefix.length + 1)
118118
const decimalPosition = target.value.indexOf(config.decimal)
119-
if (decimalPosition > 0 && (positionFromEnd - decimalPosition) <= config.precision) {
120-
// eslint-disable-next-line no-plusplus
121-
positionFromEnd++
119+
const diff = positionFromEnd - decimalPosition
120+
const maxLength = target.value.length - config.suffix.length
121+
const positionAfterDecimal = positionFromEnd + 1
122+
if (decimalPosition > 0 && diff > 0 && positionAfterDecimal <= maxLength) {
123+
positionFromEnd = positionAfterDecimal
122124
}
123125
updateCursor(target, positionFromEnd)
124126

src/directive.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default {
3434
// check decimal key and insert to current element
3535
// updated cursor position after format the value
3636
el.onkeydown = (e) => {
37-
if (e.key === '.') {
37+
if (e.key === '.' || e.key === config.decimal) {
3838
e.preventDefault()
3939
el.setRangeText(config.decimal)
4040
el.dispatchEvent(new Event('input'))

0 commit comments

Comments
 (0)