Skip to content

Commit 966f9e5

Browse files
committed
updated directive and component
1 parent 82a04f9 commit 966f9e5

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

src/component.vue

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
<script lang="ts">
1313
import { defineComponent, ref, computed, watch } from 'vue'
1414
import { cloneDeep, CustomInputEvent, Input } from './core'
15+
import NumberFormat from './number-format'
1516
import directive from './directive'
16-
import defaultOptions from './options'
17+
import defaultOptions, { Options } from './options'
1718
1819
const options = cloneDeep(defaultOptions)
1920
@@ -99,9 +100,10 @@ export default defineComponent({
99100
100101
watch(
101102
() => props.modelValue,
102-
(newValue, oldValue) => {
103-
if (newValue !== oldValue) {
104-
maskedValue.value = newValue
103+
(newValue) => {
104+
const formatNumber = new NumberFormat(config as Options).format(newValue)
105+
if (formatNumber !== maskedValue.value) {
106+
maskedValue.value = formatNumber
105107
}
106108
}
107109
)

src/core.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ export function updateCursor(el: HTMLInputElement, position: number) {
9292
* @param {Boolean} options.force Forces the update even if the old value and the new value are the same
9393
*/
9494
export function updateValue(el: CustomInputElement, vnode: VNode | null, { emit = true, force = false, clean = false } = {}) {
95+
// console.log('updateValue', arguments)
96+
9597
const { options, oldValue } = el
9698
const { reverseFill, max, min } = options
9799
const currentValue = vnode?.props?.value || el.value
@@ -110,9 +112,9 @@ export function updateValue(el: CustomInputElement, vnode: VNode | null, { emit
110112
masked = number.format(min)
111113
unmasked = min.toString()
112114
}
113-
el.oldValue = masked
114115
}
115116

117+
el.oldValue = masked
116118
el.unmasked = unmasked
117119

118120
// safari makes the cursor jump to the end if el.value gets assign even if to the same value
@@ -149,7 +151,7 @@ export function inputHandler(event: CustomInputEvent) {
149151
positionFromEnd = target.value.length - target.selectionEnd
150152
}
151153

152-
updateValue(target, null, { clean: !options.precision })
154+
updateValue(target, null, { clean: !options.precision, emit: false })
153155

154156
// updated cursor position
155157
if (options.suffix) {
@@ -176,10 +178,10 @@ export function blurHandler(event: CustomInputEvent) {
176178

177179
const { oldValue } = target
178180

179-
updateValue(target, null, { force: true, clean: true })
181+
updateValue(target, null, { force: true, clean: true, emit: false })
180182

181183
if (oldValue !== target.value) {
182-
target.dispatchEvent(InputEvent('change'))
184+
target.dispatchEvent(InputEvent('input'))
183185
}
184186
}
185187

src/directive.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ export default {
6868
if (value !== oldValue) {
6969
const options = el.options
7070
el.options = Object.assign(options, value, modifiers)
71-
core.updateValue(el, vnode, { force: true, clean: true })
71+
core.updateValue(el, vnode, { force: true, clean: true, emit: false })
7272
} else {
73-
core.updateValue(el, vnode)
73+
core.updateValue(el, vnode, { emit: false })
7474
}
7575
},
7676

0 commit comments

Comments
 (0)