Skip to content

Commit 2bf4668

Browse files
committed
fix: avoid emitting input event if initial value is null
fixes #21
1 parent 23e1501 commit 2bf4668

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

src/core.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,11 @@ export function updateCursor(event, originalValue, originalPosition) {
135135
* @param {Event} [event] The event that triggered this this update, null if not triggered by an input event
136136
*/
137137
export function updateValue(el, vnode, { emit = true, force = false } = {}, event) {
138-
const { config, oldValue } = el[CONFIG_KEY]
139-
const currentValue = vnode && vnode.data.model ? vnode.data.model.value : el.value
138+
let { config, oldValue } = el[CONFIG_KEY]
139+
let currentValue = vnode && vnode.data.model ? vnode.data.model.value : el.value
140+
141+
oldValue = oldValue || ''
142+
currentValue = currentValue || ''
140143

141144
if (force || oldValue !== currentValue) {
142145
let newValue = masker(currentValue, config)
@@ -161,7 +164,7 @@ export function updateValue(el, vnode, { emit = true, force = false } = {}, even
161164
}
162165

163166
// this part needs to be outside the above IF statement for vuetify in firefox
164-
// draweback is that we endup with two's input events in firefox
167+
// drawback is that we endup with two's input events in firefox
165168
emit && el.dispatchEvent(FacadeInputEvent())
166169
}
167170
}

src/directive.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ export default {
66
el = core.getInputElement(el)
77
el.addEventListener('input', core.inputHandler, true)
88

9-
el[CONFIG_KEY] = {
10-
config: core.normalizeConfig(value, modifiers)
11-
}
9+
const config = core.normalizeConfig(value, modifiers)
10+
el[CONFIG_KEY] = { config }
1211

1312
// set initial value
14-
core.updateValue(el, vnode)
13+
core.updateValue(el, vnode, { force: config.prefill })
1514
},
1615

1716
update: (el, { value, oldValue, modifiers }, vnode) => {

tests/component.test.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ describe('Component', () => {
1616
expect(wrapper.vm.maskedValue).toBe('123-4')
1717
})
1818

19+
test('Should not emit input if initial value is null', () => {
20+
const value = null
21+
const mask = '####'
22+
23+
const wrapper = createWrapper({ value, mask })
24+
expect(wrapper.emitted().input).toBeFalsy()
25+
})
26+
1927
test('Updating the value should update the values', async () => {
2028
const value = '1234'
2129
const mask = '###-#'
@@ -69,8 +77,7 @@ describe('Component', () => {
6977
const input = wrapper.find('input')
7078

7179
input.trigger('change')
72-
// only the initial input should be present
73-
expect(wrapper.emitted().input.length).toEqual(1)
80+
expect(wrapper.emitted().input).toBeFalsy()
7481
})
7582

7683
test('Adding a format function should call that function on input', async () => {

0 commit comments

Comments
 (0)