Skip to content

Commit

Permalink
refactor($compile): Remove unnecesarry warn
Browse files Browse the repository at this point in the history
Disable warn if v-model is used with ternary text-like types for input
  • Loading branch information
nickmessing committed Jul 26, 2017
1 parent b8f1bcd commit a62d82a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/platforms/web/compiler/directives/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import config from 'core/config'
import { addHandler, addProp, getBindingAttr } from 'compiler/helpers'
import { genComponentModel, genAssignmentCode } from 'compiler/directives/model'

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

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

if (process.env.NODE_ENV !== 'production') {
const dynamicType = el.attrsMap['v-bind:type'] || el.attrsMap[':type']
if (tag === 'input' && dynamicType) {
if (tag === 'input' && dynamicType && !ternaryTextInputRE.test(dynamicType)) {
warn(
`<input :type="${dynamicType}" v-model="${value}">:\n` +
`v-model does not support dynamic input types. Use v-if branches instead.`
Expand Down
10 changes: 10 additions & 0 deletions test/unit/features/directives/model-dynamic.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import Vue from 'vue'

describe('Directive v-model dynamic input type', () => {
it('should not warn if supported ternary', function () {
new Vue({
data: {
type: 'text',
text: 'hi'
},
template: `<input :type="type ? 'text' : 'password'" v-model="text">`
}).$mount()
expect(`v-model does not support dynamic input types`).not.toHaveBeenWarned()
})
it('should warn', function () {
new Vue({
data: {
Expand Down

0 comments on commit a62d82a

Please sign in to comment.