Просмотр исходного кода

Prevent unecessary input trigger with v-model (#5589)

* Prevent unecessary input trigger with v-model

Fix #5586

* Add test for compositionend on v-model + @input

* [skip ci] Rename tests for compositionend
Eduardo San Martin Morote 9 лет назад
Родитель
Сommit
d52a4991bd

+ 2 - 0
src/platforms/web/runtime/directives/model.js

@@ -119,6 +119,8 @@ function onCompositionStart (e) {
 }
 
 function onCompositionEnd (e) {
+  // prevent triggering an input event for no reason
+  if (!e.target.composing) return
   e.target.composing = false
   trigger(e.target, 'input')
 }

+ 45 - 0
test/unit/features/directives/model-text.spec.js

@@ -249,4 +249,49 @@ describe('Directive v-model text', () => {
     }).$mount()
     expect('You are binding v-model directly to a v-for iteration alias').toHaveBeenWarned()
   })
+
+  it('does not trigger extra input events with single compositionend', () => {
+    const spy = jasmine.createSpy()
+    const vm = new Vue({
+      data: {
+        a: 'a'
+      },
+      template: '<input v-model="a" @input="onInput">',
+      methods: {
+        onInput (e) {
+          spy(e.target.value)
+        }
+      }
+    }).$mount()
+    console.log(spy.calls.count())
+    expect(spy.calls.count()).toBe(0)
+    vm.$el.value = 'b'
+    triggerEvent(vm.$el, 'input')
+    expect(spy.calls.count()).toBe(1)
+    triggerEvent(vm.$el, 'compositionend')
+    expect(spy.calls.count()).toBe(1)
+  })
+
+  it('triggers extra input on compositionstart + end', () => {
+    const spy = jasmine.createSpy()
+    const vm = new Vue({
+      data: {
+        a: 'a'
+      },
+      template: '<input v-model="a" @input="onInput">',
+      methods: {
+        onInput (e) {
+          spy(e.target.value)
+        }
+      }
+    }).$mount()
+    console.log(spy.calls.count())
+    expect(spy.calls.count()).toBe(0)
+    vm.$el.value = 'b'
+    triggerEvent(vm.$el, 'input')
+    expect(spy.calls.count()).toBe(1)
+    triggerEvent(vm.$el, 'compositionstart')
+    triggerEvent(vm.$el, 'compositionend')
+    expect(spy.calls.count()).toBe(2)
+  })
 })