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

This commit is contained in:
Evan You 2016-11-15 11:05:08 -05:00
parent 77497931e0
commit 3e8ac270a8
3 changed files with 52 additions and 4 deletions

View File

@ -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

View File

@ -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')
})
})

View File

@ -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()