Skip to content

Commit 146002a

Browse files
committed
Merge pull request #1955 from lepture/coerce
add coerce option for component props
2 parents 9f514b6 + 3dbdf4c commit 146002a

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

src/directives/internal/prop.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import Watcher from '../../watcher'
77
import config from '../../config'
8-
import { assertProp, initProp } from '../../util/index'
8+
import { assertProp, initProp, coerceProp } from '../../util/index'
99

1010
const bindingModes = config._propBindingModes
1111

@@ -25,6 +25,7 @@ export default {
2525
parent,
2626
parentKey,
2727
function (val) {
28+
val = coerceProp(prop, val)
2829
if (assertProp(prop, val)) {
2930
child[childKey] = val
3031
}

src/util/component.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ function getIsBinding (el) {
7676

7777
export function initProp (vm, prop, value) {
7878
const key = prop.path
79+
value = coerceProp(prop, value)
7980
vm[key] = vm._data[key] = assertProp(prop, value)
8081
? value
8182
: undefined
@@ -143,6 +144,23 @@ export function assertProp (prop, value) {
143144
return true
144145
}
145146

147+
/**
148+
* Force parsing value with coerce option.
149+
*
150+
* @param {*} value
151+
* @param {Object} options
152+
* @return {*}
153+
*/
154+
155+
export function coerceProp (prop, value) {
156+
var coerce = prop.options.coerce
157+
if (!coerce) {
158+
return value
159+
}
160+
// coerce is a function
161+
return coerce(value)
162+
}
163+
146164
function formatType (val) {
147165
return val
148166
? val.charAt(0).toUpperCase() + val.slice(1)

test/unit/specs/directives/internal/prop_spec.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ describe('prop', function () {
295295

296296
describe('assertions', function () {
297297

298-
function makeInstance (value, type, validator) {
298+
function makeInstance (value, type, validator, coerce) {
299299
return new Vue({
300300
el: document.createElement('div'),
301301
template: '<test :test="val"></test>',
@@ -307,7 +307,8 @@ describe('prop', function () {
307307
props: {
308308
test: {
309309
type: type,
310-
validator: validator
310+
validator: validator,
311+
coerce: coerce
311312
}
312313
}
313314
}
@@ -391,6 +392,17 @@ describe('prop', function () {
391392
expect(hasWarned('Expected String')).toBe(true)
392393
})
393394

395+
it('type check + coerce', function () {
396+
makeInstance(123, String, null, String)
397+
expect(getWarnCount()).toBe(0)
398+
makeInstance('123', Number, null, Number)
399+
expect(getWarnCount()).toBe(0)
400+
makeInstance('123', Boolean, null, function (val) {
401+
return val === '123'
402+
})
403+
expect(getWarnCount()).toBe(0)
404+
})
405+
394406
it('required', function () {
395407
new Vue({
396408
el: document.createElement('div'),

0 commit comments

Comments
 (0)