Procházet zdrojové kódy

fix .trim modifier when v-model is used on custom component (fix #4204)

Evan You před 9 roky
rodič
revize
3e8ac270a8

+ 1 - 1
src/platforms/web/compiler/directives/model.js

@@ -123,7 +123,7 @@ function genDefaultModel (
 
   let valueExpression = isNative
     ? `$event.target.value${trim ? '.trim()' : ''}`
-    : `$event`
+    : trim ? `(typeof $event === 'string' ? $event.trim() : $event)` : `$event`
   valueExpression = number || type === 'number'
     ? `_n(${valueExpression})`
     : valueExpression

+ 51 - 0
test/unit/features/directives/model-component.spec.js

@@ -51,4 +51,55 @@ describe('Directive v-model component', () => {
       vm.$destroy()
     }).then(done)
   })
+
+  it('modifier: .lazy', () => {
+    const vm = new Vue({
+      template: `<div><my-input ref="input" v-model.lazy="text"></my-input></div>`,
+      data: { text: 'foo' },
+      components: {
+        'my-input': {
+          template: '<input>'
+        }
+      }
+    }).$mount()
+    expect(vm.text).toBe('foo')
+    vm.$refs.input.$emit('input', 'bar')
+    expect(vm.text).toBe('foo')
+    vm.$refs.input.$emit('change', 'bar')
+    expect(vm.text).toBe('bar')
+  })
+
+  it('modifier: .number', () => {
+    const vm = new Vue({
+      template: `<div><my-input ref="input" v-model.number="text"></my-input></div>`,
+      data: { text: 'foo' },
+      components: {
+        'my-input': {
+          template: '<input>'
+        }
+      }
+    }).$mount()
+    expect(vm.text).toBe('foo')
+    vm.$refs.input.$emit('input', 'bar')
+    expect(vm.text).toBe('bar')
+    vm.$refs.input.$emit('input', '123')
+    expect(vm.text).toBe(123)
+  })
+
+  it('modifier: .trim', () => {
+    const vm = new Vue({
+      template: `<div><my-input ref="input" v-model.trim="text"></my-input></div>`,
+      data: { text: 'foo' },
+      components: {
+        'my-input': {
+          template: '<input>'
+        }
+      }
+    }).$mount()
+    expect(vm.text).toBe('foo')
+    vm.$refs.input.$emit('input', '  bar  ')
+    expect(vm.text).toBe('bar')
+    vm.$refs.input.$emit('input', '   foo o  ')
+    expect(vm.text).toBe('foo o')
+  })
 })

+ 0 - 3
test/weex/compiler/v-model.spec.js

@@ -11,9 +11,6 @@ describe('compile v-model', () => {
     expect(errors).toEqual([])
   })
 
-  it('should compile into render functions without runtime model directive', () => {
-  })
-
   it('should compile other component with whole $event as the value', () => {
     const { render, staticRenderFns, errors } = compile(`<div><foo v-model="x" /></div>`)
     expect(render).not.toBeUndefined()