Skip to content

Commit 8aedae6

Browse files
committed
updated core function
1 parent cdc59f7 commit 8aedae6

File tree

3 files changed

+27
-35
lines changed

3 files changed

+27
-35
lines changed

src/core.js

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,6 @@ export function FacadeBlurEvent() {
3434
})
3535
}
3636

37-
/**
38-
* Transform an array or string config into an object
39-
*
40-
* @param {object} config The format config object
41-
* @param {object} modifiers An object of modifier flags that can influence the formating process
42-
*/
43-
export function normalizeConfig(defaults, extras) {
44-
defaults = defaults || {}
45-
extras = extras || {}
46-
return Object.keys(defaults).concat(Object.keys(extras)).reduce((acc, val) => {
47-
acc[val] = extras[val] === undefined ? defaults[val] : extras[val]
48-
return acc
49-
}, {})
50-
}
51-
5237
/**
5338
* ensure that the element we're attaching to is an input element
5439
* if not try to find an input element in this elements childrens
@@ -75,7 +60,7 @@ export function updateCursor(el, position) {
7560
const setSelectionRange = () => { el.setSelectionRange(position, position) }
7661
setSelectionRange()
7762
// Android Fix
78-
setTimeout(setSelectionRange(), 0)
63+
setTimeout(setSelectionRange(), 1)
7964
}
8065

8166
/**
@@ -95,22 +80,22 @@ export function updateValue(el, vnode, { emit = true, force = false, clean = fal
9580
oldValue = oldValue || ''
9681
currentValue = currentValue || ''
9782

98-
const number = new NumberFormat(config).clean(clean && !config.reverseFill)
99-
let masked = number.format(currentValue)
100-
let unmasked = number.clean(!config.reverseFill).unformat(currentValue)
101-
102-
// check value with in range max and min value
103-
if (clean) {
104-
if (Number(config.max) && unmasked > Number(config.max)) {
105-
masked = number.format(config.max)
106-
unmasked = number.unformat(config.max)
107-
} else if (Number(config.min) && unmasked < Number(config.min)) {
108-
masked = number.format(config.min)
109-
unmasked = number.unformat(config.min)
83+
if (force || oldValue !== currentValue) {
84+
const number = new NumberFormat(config).clean(clean && !config.reverseFill)
85+
let masked = number.format(currentValue)
86+
let unmasked = number.clean(!config.reverseFill).unformat(currentValue)
87+
88+
// check value with in range max and min value
89+
if (clean) {
90+
if (Number(config.max) && unmasked > Number(config.max)) {
91+
masked = number.format(config.max)
92+
unmasked = number.unformat(config.max)
93+
} else if (Number(config.min) && unmasked < Number(config.min)) {
94+
masked = number.format(config.min)
95+
unmasked = number.unformat(config.min)
96+
}
11097
}
111-
}
11298

113-
if (force || oldValue !== currentValue) {
11499
el[CONFIG_KEY].oldValue = masked
115100
el.unmaskedValue = unmasked
116101
// safari makes the cursor jump to the end if el.value gets assign even if to the same value
@@ -143,7 +128,8 @@ export function inputHandler(event) {
143128
let positionFromEnd = target.value.length - target.selectionEnd
144129
const { oldValue, config } = target[CONFIG_KEY]
145130

146-
updateValue(target, null, { emit: false })
131+
updateValue(target, null, { emit: false }, event)
132+
147133
// updated cursor position
148134
positionFromEnd = Math.max(positionFromEnd, config.suffix.length)
149135
positionFromEnd = target.value.length - positionFromEnd
@@ -169,7 +155,7 @@ export function blurHandler(event) {
169155

170156
const { oldValue } = target[CONFIG_KEY]
171157

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

174160
if (oldValue !== target.value) {
175161
target.dispatchEvent(FacadeBlurEvent())

src/directive.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const CONFIG_KEY = core.CONFIG_KEY
77
export default {
88
bind: (el, { value, modifiers }, vnode) => {
99
el = core.getInputElement(el)
10-
const config = Object.assign({}, defaults, value, modifiers)
10+
const config = Object.assign({}, core.cloneDeep(defaults), value, modifiers)
1111
el[CONFIG_KEY] = { config }
1212
// set initial value
1313
core.updateValue(el, vnode, { force: config.prefill, clean: true })
@@ -22,7 +22,12 @@ export default {
2222
const handlerOwner = el.parentElement || el
2323

2424
// use anonymous event handler to avoid inadvertently removing masking for all inputs within a container
25-
const oninput = (e) => core.inputHandler(e)
25+
const oninput = (e) => {
26+
if (e.target !== el) {
27+
return
28+
}
29+
core.inputHandler(e)
30+
}
2631

2732
handlerOwner.addEventListener('input', oninput, true)
2833

@@ -34,7 +39,7 @@ export default {
3439
if (([110, 190].includes(e.keyCode) || e.key === config.decimal) && !el.value.includes(config.decimal)) {
3540
e.preventDefault()
3641
el.setRangeText(config.decimal)
37-
core.updateValue(el, null, { emit: true })
42+
core.updateValue(el, null, { emit: true }, e)
3843
core.updateCursor(el, el.value.indexOf(config.decimal) + 1)
3944
} else if (
4045
([110, 190].includes(e.keyCode) || e.key === config.decimal) && el.value.includes(config.decimal)

vue-v3

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit c06a626fefa70c2dd4d3be39436bcfa743b41a8a

0 commit comments

Comments
 (0)