Skip to content

Commit a62d82a

Browse files
committed
refactor($compile): Remove unnecesarry warn
Disable warn if v-model is used with ternary text-like types for input
1 parent b8f1bcd commit a62d82a

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

Diff for: src/platforms/web/compiler/directives/model.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import config from 'core/config'
44
import { addHandler, addProp, getBindingAttr } from 'compiler/helpers'
55
import { genComponentModel, genAssignmentCode } from 'compiler/directives/model'
66

7+
const ternaryTextInputRE = /^[^?]{1,}\?\s*('|"|`)(?:text|number|password|search|email|tel|url)\1\s*:\s*('|"|`)(?:text|number|password|search|email|tel|url)\2\s*$/
78
let warn
89

910
// in some cases, the event used has to be determined at runtime
@@ -24,7 +25,7 @@ export default function model (
2425

2526
if (process.env.NODE_ENV !== 'production') {
2627
const dynamicType = el.attrsMap['v-bind:type'] || el.attrsMap[':type']
27-
if (tag === 'input' && dynamicType) {
28+
if (tag === 'input' && dynamicType && !ternaryTextInputRE.test(dynamicType)) {
2829
warn(
2930
`<input :type="${dynamicType}" v-model="${value}">:\n` +
3031
`v-model does not support dynamic input types. Use v-if branches instead.`

Diff for: test/unit/features/directives/model-dynamic.spec.js

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import Vue from 'vue'
22

33
describe('Directive v-model dynamic input type', () => {
4+
it('should not warn if supported ternary', function () {
5+
new Vue({
6+
data: {
7+
type: 'text',
8+
text: 'hi'
9+
},
10+
template: `<input :type="type ? 'text' : 'password'" v-model="text">`
11+
}).$mount()
12+
expect(`v-model does not support dynamic input types`).not.toHaveBeenWarned()
13+
})
414
it('should warn', function () {
515
new Vue({
616
data: {

0 commit comments

Comments
 (0)