Skip to content

Commit e28550f

Browse files
posvaaJean
authored andcommitted
fix(v-model): allow arbitrary naems for type binding (vuejs#6802)
Fix vuejs#6800
1 parent ef0afb1 commit e28550f

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/platforms/web/compiler/modules/model.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function preTransformNode (el: ASTElement, options: CompilerOptions) {
3636
addRawAttr(branch0, 'type', 'checkbox')
3737
processElement(branch0, options)
3838
branch0.processed = true // prevent it from double-processed
39-
branch0.if = `type==='checkbox'` + ifConditionExtra
39+
branch0.if = `(${typeBinding})==='checkbox'` + ifConditionExtra
4040
addIfCondition(branch0, {
4141
exp: branch0.if,
4242
block: branch0
@@ -47,7 +47,7 @@ function preTransformNode (el: ASTElement, options: CompilerOptions) {
4747
addRawAttr(branch1, 'type', 'radio')
4848
processElement(branch1, options)
4949
addIfCondition(branch0, {
50-
exp: `type==='radio'` + ifConditionExtra,
50+
exp: `(${typeBinding})==='radio'` + ifConditionExtra,
5151
block: branch1
5252
})
5353
// 3. other

test/unit/features/directives/model-dynamic.spec.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ describe('Directive v-model dynamic input type', () => {
44
it('should work', done => {
55
const vm = new Vue({
66
data: {
7-
type: null,
7+
inputType: null,
88
test: 'b'
99
},
10-
template: `<input :type="type" v-model="test">`
10+
template: `<input :type="inputType" v-model="test">`
1111
}).$mount()
1212
document.body.appendChild(vm.$el)
1313

1414
// test text
15-
assertInputWorks(vm).then(done)
15+
assertInputWorks(vm, 'inputType').then(done)
1616
})
1717

1818
it('with v-if', done => {
@@ -87,7 +87,11 @@ describe('Directive v-model dynamic input type', () => {
8787
})
8888
})
8989

90-
function assertInputWorks (vm, chain) {
90+
function assertInputWorks (vm, type, chain) {
91+
if (typeof type !== 'string') {
92+
if (!chain) chain = type
93+
type = 'type'
94+
}
9195
if (!chain) chain = waitForUpdate()
9296
chain.then(() => {
9397
expect(vm.$el.value).toBe('b')
@@ -99,7 +103,7 @@ function assertInputWorks (vm, chain) {
99103
expect(vm.test).toBe('c')
100104
}).then(() => {
101105
// change it to password
102-
vm.type = 'password'
106+
vm[type] = 'password'
103107
vm.test = 'b'
104108
}).then(() => {
105109
expect(vm.$el.type).toBe('password')
@@ -109,7 +113,7 @@ function assertInputWorks (vm, chain) {
109113
expect(vm.test).toBe('c')
110114
}).then(() => {
111115
// change it to checkbox...
112-
vm.type = 'checkbox'
116+
vm[type] = 'checkbox'
113117
}).then(() => {
114118
expect(vm.$el.type).toBe('checkbox')
115119
expect(vm.$el.checked).toBe(true)

0 commit comments

Comments
 (0)