Skip to content
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

fix(model): fix dynamic v-model with v-else statement #6952

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 9 additions & 0 deletions src/platforms/web/compiler/modules/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ function preTransformNode (el: ASTElement, options: CompilerOptions) {
const typeBinding: any = getBindingAttr(el, 'type')
const ifCondition = getAndRemoveAttr(el, 'v-if', true)
const ifConditionExtra = ifCondition ? `&&(${ifCondition})` : ``
const elseStmt = getAndRemoveAttr(el, 'v-else', true)
const elseIfStmt = getAndRemoveAttr(el, 'v-else-if', true)
// 1. checkbox
const branch0 = cloneASTElement(el)
// process for on the main node
Expand Down Expand Up @@ -59,6 +61,13 @@ function preTransformNode (el: ASTElement, options: CompilerOptions) {
exp: ifCondition,
block: branch2
})

// v-else and v-else-if
if (elseStmt != null) {
branch0.else = true
} else if (elseIfStmt) {
branch0.elseif = elseIfStmt
}
return branch0
}
}
Expand Down
45 changes: 45 additions & 0 deletions test/unit/features/directives/model-dynamic.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,51 @@ describe('Directive v-model dynamic input type', () => {
assertInputWorks(vm, chain).then(done)
})

it('with v-else-if', done => {
const vm = new Vue({
data: {
foo: true,
bar: false,
type: null,
test: 'b'
},
template: `<div v-if="foo">text</div><input v-else-if="bar" :type="type" v-model="test">`
}).$mount()
document.body.appendChild(vm.$el)

const chain = waitForUpdate(() => {
expect(vm.$el.textContent).toBe('text')
}).then(() => {
vm.foo = false
}).then(() => {
expect(vm._vnode.isComment).toBe(true)
}).then(() => {
vm.bar = true
})

assertInputWorks(vm, chain).then(done)
})

it('with v-else', done => {
const vm = new Vue({
data: {
ok: true,
type: null,
test: 'b'
},
template: `<div v-if="ok">text</div><input v-else :type="type" v-model="test">`
}).$mount()
document.body.appendChild(vm.$el)

const chain = waitForUpdate(() => {
expect(vm.$el.textContent).toBe('text')
}).then(() => {
vm.ok = false
})

assertInputWorks(vm, chain).then(done)
})

it('with v-for', done => {
const vm = new Vue({
data: {
Expand Down