mirror of
https://gitee.com/vuejs/vue.git
synced 2024-11-30 11:07:51 +08:00
fix v-model number conversion (fix #3140)
This commit is contained in:
parent
dcd82c2429
commit
3fe127b05a
@ -94,6 +94,8 @@ declare interface Component {
|
||||
) => Object | void;
|
||||
// toString
|
||||
_s: (value: any) => string;
|
||||
// toNumber
|
||||
_n: (value: string) => number | string;
|
||||
// resolveFilter
|
||||
_f: (id: string) => Function;
|
||||
// renderList
|
||||
|
@ -5,7 +5,7 @@ import VNode, { emptyVNode } from '../vdom/vnode'
|
||||
import { normalizeChildren } from '../vdom/helpers'
|
||||
import {
|
||||
warn, bind, isObject, toObject,
|
||||
nextTick, resolveAsset, renderString
|
||||
nextTick, resolveAsset, renderString, toNumber
|
||||
} from '../util/index'
|
||||
|
||||
import {
|
||||
@ -96,6 +96,8 @@ export function renderMixin (Vue: Class<Component>) {
|
||||
|
||||
// toString for mustaches
|
||||
Vue.prototype._s = renderString
|
||||
// number conversion
|
||||
Vue.prototype._n = toNumber
|
||||
|
||||
// filter resolution helper
|
||||
const identity = _ => _
|
||||
|
@ -101,7 +101,7 @@ function genDefaultModel (
|
||||
|
||||
const valueExpression = `$event.target.value${trim ? '.trim()' : ''}`
|
||||
let code = number || type === 'number'
|
||||
? `${value}=Number(${valueExpression})`
|
||||
? `${value}=_n(${valueExpression})`
|
||||
: `${value}=${valueExpression}`
|
||||
if (needCompositionGuard) {
|
||||
code = `if($event.target.composing)return;${code}`
|
||||
|
@ -11,6 +11,15 @@ export function renderString (val: any): string {
|
||||
: String(val)
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a input value to a number for persistence.
|
||||
* If the conversion fails, return original string.
|
||||
*/
|
||||
export function toNumber (val: string): number | string {
|
||||
const n = parseFloat(val, 10)
|
||||
return (n || n === 0) ? n : val
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a map and return a function for checking if a key
|
||||
* is in that map.
|
||||
|
@ -45,7 +45,10 @@ describe('Directive v-model text', () => {
|
||||
expect(vm.test).toBe(1)
|
||||
vm.$el.value = '2'
|
||||
triggerEvent(vm.$el, 'input')
|
||||
expect(vm.test).toBe(2)
|
||||
// should let strings pass through
|
||||
vm.$el.value = 'f'
|
||||
triggerEvent(vm.$el, 'input')
|
||||
expect(vm.test).toBe('f')
|
||||
})
|
||||
|
||||
it('.trim modifier', () => {
|
||||
|
Loading…
Reference in New Issue
Block a user