Skip to content

Commit 608ecf8

Browse files
author
Dipak Sarkar
committed
added mouse click event to change the cursor position on enter the elment
1 parent 956400e commit 608ecf8

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

docs/.vuepress/components/Example.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export default {
7575
decimal: ',',
7676
separator: '.',
7777
prefix: '$',
78-
suffix: '',
78+
suffix: ' %',
7979
precision: 2,
8080
masked: false
8181
}

src/core.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ export function inputHandler(event) {
115115
positionFromEnd = Math.max(positionFromEnd, config.suffix.length)
116116
positionFromEnd = target.value.length - positionFromEnd
117117
positionFromEnd = Math.max(positionFromEnd, config.prefix.length + 1)
118+
const decimalPosition = target.value.indexOf(config.decimal)
119+
if (decimalPosition > 0 && (positionFromEnd - decimalPosition) <= config.precision) {
120+
// eslint-disable-next-line no-plusplus
121+
positionFromEnd++
122+
}
118123
updateCursor(target, positionFromEnd)
119124

120125
if (oldValue !== target.value) {

src/directive.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,14 @@ export default {
2525
handlerOwner.addEventListener('input', oninput, true)
2626

2727
el.onblur = (e) => core.blurHandler(e)
28+
el.onclick = ({ target }) => {
29+
const { selectionStart, value } = target
30+
if (config.suffix.length && (value.length - selectionStart) < config.suffix.length) core.updateCursor(el, el.value.length - config.suffix.length)
31+
else if (config.prefix.length && selectionStart < config.prefix.length) core.updateCursor(el, config.prefix.length)
32+
}
2833

2934
// check decimal key and insert to current element
35+
// updated cursor position after format the value
3036
el.onkeydown = (e) => {
3137
if (e.key === '.') {
3238
e.preventDefault()

0 commit comments

Comments
 (0)