Skip to content

feat(v-model): add lazy modifier for v-model on components(close #6914) #11830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions flow/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ declare type ASTElement = {
value: string;
callback: string;
expression: string;
lazy: boolean;
};

directives?: Array<ASTDirective>;
Expand Down
1 change: 1 addition & 0 deletions flow/vnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ declare interface VNodeData {
model?: {
value: any;
callback: Function;
lazy: boolean;
};
};

Expand Down
9 changes: 8 additions & 1 deletion src/compiler/codegen/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,15 @@ export function genData (el: ASTElement, state: CodegenState): string {
el.model.callback
},expression:${
el.model.expression
}},`
}`

if (el.model.lazy) {
data += `,lazy:true`
}

data += '}'
}

// inline-template
if (el.inlineTemplate) {
const inlineTemplate = genInlineTemplate(el, state)
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/directives/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function genComponentModel (
value: string,
modifiers: ?ASTModifiers
): ?boolean {
const { number, trim } = modifiers || {}
const { number, trim, lazy } = modifiers || {}

const baseValueExpression = '$$v'
let valueExpression = baseValueExpression
Expand All @@ -26,6 +26,7 @@ export function genComponentModel (
el.model = {
value: `(${value})`,
expression: JSON.stringify(value),
lazy: !!lazy,
callback: `function (${baseValueExpression}) {${assignment}}`
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/core/vdom/create-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,9 @@ function mergeHook (f1: any, f2: any): Function {
// prop and event handler respectively.
function transformModel (options, data: any) {
const prop = (options.model && options.model.prop) || 'value'
const event = (options.model && options.model.event) || 'input'
const defaultEvent = data.model.lazy ? 'change' : 'input'
const event = (options.model && (data.model.lazy ?
options.model.eventLazy : options.model.event)) || defaultEvent
;(data.attrs || (data.attrs = {}))[prop] = data.model.value
const on = data.on || (data.on = {})
const existing = on[event]
Expand Down
34 changes: 34 additions & 0 deletions test/unit/features/directives/model-component.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,40 @@ describe('Directive v-model component', () => {
expect(vm.text).toBe('foo o')
})

it('modifier: .lazy', () => {
const vm = new Vue({
template: `
<div>
<default-model ref="default" v-model.lazy="text1" />
<custom-model ref="custom" v-model.lazy="text2" />
</div>
`,
data: { text1: 'foo', text2: 'foo' },
components: {
'default-model': {
template: '<input>'
},
'custom-model': {
template: '<input>',
model: {
eventLazy: 'lazy-update'
}
}
}
}).$mount()
expect(vm.text1).toBe('foo')
vm.$refs.default.$emit('input', 'bar')
expect(vm.text1).toBe('foo')
vm.$refs.default.$emit('change', 'baz')
expect(vm.text1).toBe('baz')

// customized model option
vm.$refs.custom.$emit('change', 'bar')
expect(vm.text2).toBe('foo')
vm.$refs.custom.$emit('lazy-update', 'baz')
expect(vm.text2).toBe('baz')
})

// #8436
it('should not double transform mode props', () => {
const BaseInput = {
Expand Down
6 changes: 6 additions & 0 deletions test/unit/modules/compiler/codegen.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ describe('codegen', () => {
'<my-component v-model="\n test \n" />',
`with(this){return _c('my-component',{model:{value:(\n test \n),callback:function ($$v) {\n test \n=$$v},expression:"\\n test \\n"}})}`
)

// for lazy modifier
assertCodegen(
'<my-component v-model.lazy="\n test \n" />',
`with(this){return _c('my-component',{model:{value:(\n test \n),callback:function ($$v) {\n test \n=$$v},expression:"\\n test \\n",lazy:true}})}`
)
})

it('generate template tag', () => {
Expand Down
2 changes: 2 additions & 0 deletions types/options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export interface ComponentOptions<
model?: {
prop?: string;
event?: string;
eventLazy?: string;
};

parent?: Vue;
Expand All @@ -127,6 +128,7 @@ export interface FunctionalComponentOptions<Props = DefaultProps, PropDefs = Pro
model?: {
prop?: string;
event?: string;
eventLazy?: string;
};
inject?: InjectOptions;
functional: boolean;
Expand Down