Skip to content

Commit d52a499

Browse files
posvayyx990803
authored andcommitted
Prevent unecessary input trigger with v-model (vuejs#5589)
* Prevent unecessary input trigger with v-model Fix vuejs#5586 * Add test for compositionend on v-model + @input * [skip ci] Rename tests for compositionend
1 parent 9ac4c41 commit d52a499

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/platforms/web/runtime/directives/model.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ function onCompositionStart (e) {
119119
}
120120

121121
function onCompositionEnd (e) {
122+
// prevent triggering an input event for no reason
123+
if (!e.target.composing) return
122124
e.target.composing = false
123125
trigger(e.target, 'input')
124126
}

test/unit/features/directives/model-text.spec.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,4 +249,49 @@ describe('Directive v-model text', () => {
249249
}).$mount()
250250
expect('You are binding v-model directly to a v-for iteration alias').toHaveBeenWarned()
251251
})
252+
253+
it('does not trigger extra input events with single compositionend', () => {
254+
const spy = jasmine.createSpy()
255+
const vm = new Vue({
256+
data: {
257+
a: 'a'
258+
},
259+
template: '<input v-model="a" @input="onInput">',
260+
methods: {
261+
onInput (e) {
262+
spy(e.target.value)
263+
}
264+
}
265+
}).$mount()
266+
console.log(spy.calls.count())
267+
expect(spy.calls.count()).toBe(0)
268+
vm.$el.value = 'b'
269+
triggerEvent(vm.$el, 'input')
270+
expect(spy.calls.count()).toBe(1)
271+
triggerEvent(vm.$el, 'compositionend')
272+
expect(spy.calls.count()).toBe(1)
273+
})
274+
275+
it('triggers extra input on compositionstart + end', () => {
276+
const spy = jasmine.createSpy()
277+
const vm = new Vue({
278+
data: {
279+
a: 'a'
280+
},
281+
template: '<input v-model="a" @input="onInput">',
282+
methods: {
283+
onInput (e) {
284+
spy(e.target.value)
285+
}
286+
}
287+
}).$mount()
288+
console.log(spy.calls.count())
289+
expect(spy.calls.count()).toBe(0)
290+
vm.$el.value = 'b'
291+
triggerEvent(vm.$el, 'input')
292+
expect(spy.calls.count()).toBe(1)
293+
triggerEvent(vm.$el, 'compositionstart')
294+
triggerEvent(vm.$el, 'compositionend')
295+
expect(spy.calls.count()).toBe(2)
296+
})
252297
})

0 commit comments

Comments
 (0)