Skip to content

Commit cdc59f7

Browse files
committed
fix the issue decial update
1 parent b3b3011 commit cdc59f7

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@coders-tm/vue-number-format",
3-
"version": "1.0.0",
3+
"version": "2.2.1",
44
"private": false,
55
"description": "Easy formatted numbers, currency and percentage with input/directive mask for Vue.js",
66
"author": "Dipak Sarkar <hello@dipaksarkar.in> (https://dipaksarkar.in/)",

src/component.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
<script>
1616
import directive from './directive'
17-
import { cloneDeep } from "./core";
17+
import { cloneDeep } from './core';
1818
import defaultOptions from './options'
1919
2020
const options = cloneDeep(defaultOptions)

src/core.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const CONFIG_KEY = '__input-facade__'
66
/**
77
* Creates a fuction to clone the objcet
88
*/
9-
export function cloneDeep(data) {
9+
export function cloneDeep(data) {
1010
return JSON.parse(JSON.stringify(data))
1111
}
1212

@@ -143,7 +143,7 @@ export function inputHandler(event) {
143143
let positionFromEnd = target.value.length - target.selectionEnd
144144
const { oldValue, config } = target[CONFIG_KEY]
145145

146-
updateValue(target, null, { emit: false }, event)
146+
updateValue(target, null, { emit: false })
147147
// updated cursor position
148148
positionFromEnd = Math.max(positionFromEnd, config.suffix.length)
149149
positionFromEnd = target.value.length - positionFromEnd
@@ -169,7 +169,7 @@ export function blurHandler(event) {
169169

170170
const { oldValue } = target[CONFIG_KEY]
171171

172-
updateValue(target, null, { force: true, clean: true }, event)
172+
updateValue(target, null, { force: true, clean: true })
173173

174174
if (oldValue !== target.value) {
175175
target.dispatchEvent(FacadeBlurEvent())

src/directive.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,30 @@ export default {
3434
if (([110, 190].includes(e.keyCode) || e.key === config.decimal) && !el.value.includes(config.decimal)) {
3535
e.preventDefault()
3636
el.setRangeText(config.decimal)
37-
core.updateValue(el, null, { emit: true }, e)
37+
core.updateValue(el, null, { emit: true })
3838
core.updateCursor(el, el.value.indexOf(config.decimal) + 1)
39-
} else if (([110, 190].includes(e.keyCode) || e.key === config.decimal) && el.value.includes(config.decimal)) {
39+
} else if (
40+
([110, 190].includes(e.keyCode) || e.key === config.decimal) && el.value.includes(config.decimal)
41+
) {
4042
e.preventDefault()
43+
} else if ([8].includes(e.keyCode)) {
44+
// check current cursor position is after separator when backspace key down
45+
const character = el.value.slice(el.selectionEnd - 1, el.selectionEnd)
46+
const replace = el.value.slice(el.selectionEnd - 2, el.selectionEnd)
47+
if (character === config.separator) {
48+
e.preventDefault()
49+
50+
let positionFromEnd = el.value.length - el.selectionEnd
51+
// remove separator and before character
52+
el.value = el.value.replace(replace, '')
53+
// updated cursor position
54+
positionFromEnd = Math.max(positionFromEnd, config.suffix.length)
55+
positionFromEnd = el.value.length - positionFromEnd
56+
positionFromEnd = Math.max(positionFromEnd, config.prefix.length)
57+
core.updateCursor(el, positionFromEnd)
58+
// trigger input event
59+
el.dispatchEvent(new Event('input'))
60+
}
4161
}
4262
}
4363

0 commit comments

Comments
 (0)