Skip to content

Commit 03621ee

Browse files
committed
adjust render fn + fix props validation for functional components
1 parent b6d386b commit 03621ee

File tree

5 files changed

+25
-15
lines changed

5 files changed

+25
-15
lines changed

src/core/instance/lifecycle.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export function lifecycleMixin (Vue: Class<Component>) {
118118
const propKeys = vm.$options._propKeys || []
119119
for (let i = 0; i < propKeys.length; i++) {
120120
const key = propKeys[i]
121-
vm[key] = validateProp(vm, key, propsData)
121+
vm[key] = validateProp(key, vm.$options.props, propsData, vm)
122122
}
123123
observerState.shouldConvert = true
124124
if (process.env.NODE_ENV !== 'production') {

src/core/instance/state.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function initProps (vm: Component) {
4040
const key = keys[i]
4141
/* istanbul ignore else */
4242
if (process.env.NODE_ENV !== 'production') {
43-
defineReactive(vm, key, validateProp(vm, key, propsData), () => {
43+
defineReactive(vm, key, validateProp(key, props, propsData, vm), () => {
4444
if (vm.$parent && !observerState.isSettingProps) {
4545
warn(
4646
`Avoid mutating a prop directly since the value will be ` +
@@ -52,7 +52,7 @@ function initProps (vm: Component) {
5252
}
5353
})
5454
} else {
55-
defineReactive(vm, key, validateProp(vm, key, propsData))
55+
defineReactive(vm, key, validateProp(key, props, propsData, vm))
5656
}
5757
}
5858
observerState.shouldConvert = true

src/core/util/props.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@ type PropOptions = {
1111
validator: ?Function
1212
}
1313

14-
export function validateProp (vm: Component, key: string, propsData: ?Object): any {
14+
export function validateProp (
15+
key: string,
16+
propOptions: Object,
17+
propsData: ?Object,
18+
vm?: Component
19+
): any {
1520
/* istanbul ignore if */
16-
if (!vm.$options.props || !propsData) return
17-
const prop = vm.$options.props[key]
21+
if (!propsData) return
22+
const prop = propOptions[key]
1823
const absent = !hasOwn(propsData, key)
1924
let value = propsData[key]
2025
// handle boolean props
@@ -43,7 +48,7 @@ export function validateProp (vm: Component, key: string, propsData: ?Object): a
4348
/**
4449
* Get the default value of a prop.
4550
*/
46-
function getPropDefaultValue (vm: Component, prop: PropOptions, name: string): any {
51+
function getPropDefaultValue (vm: ?Component, prop: PropOptions, name: string): any {
4752
// no default, return undefined
4853
if (!hasOwn(prop, 'default')) {
4954
return undefined
@@ -71,7 +76,7 @@ function assertProp (
7176
prop: PropOptions,
7277
name: string,
7378
value: any,
74-
vm: Component,
79+
vm: ?Component,
7580
absent: boolean
7681
) {
7782
if (prop.required && absent) {

src/core/vdom/create-component.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Vue from '../instance/index'
44
import VNode from './vnode'
55
import { normalizeChildren } from './helpers'
66
import { callHook } from '../instance/lifecycle'
7-
import { warn, isObject, hasOwn, hyphenate } from '../util/index'
7+
import { warn, isObject, hasOwn, hyphenate, validateProp } from '../util/index'
88

99
const hooks = { init, prepatch, insert, destroy }
1010
const hooksToMerge = Object.keys(hooks)
@@ -69,12 +69,17 @@ export function createComponent (
6969

7070
// functional component
7171
if (Ctor.options.functional) {
72+
const props = {}
73+
const propOptions = Ctor.options.props
74+
if (propOptions) {
75+
Object.keys(propOptions).forEach(key => {
76+
props[key] = validateProp(key, propOptions, propsData)
77+
})
78+
}
7279
return Ctor.options.render.call(
7380
null,
74-
parent.$createElement, // h
75-
propsData || {}, // props
76-
normalizeChildren(children), // children
77-
data // data
81+
parent.$createElement,
82+
{ props, parent, data, children: normalizeChildren(children) }
7883
)
7984
}
8085

test/unit/features/options/functional.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('Options functional', () => {
99
wrap: {
1010
functional: true,
1111
props: ['msg'],
12-
render (h, props, children) {
12+
render (h, { props, children }) {
1313
return h('div', null, [props.msg, ' '].concat(children))
1414
}
1515
}
@@ -35,7 +35,7 @@ describe('Options functional', () => {
3535
validate: {
3636
functional: true,
3737
props: ['field'],
38-
render (h, props, children, { on }) {
38+
render (h, { props, children, data: { on } }) {
3939
props.child = children[0]
4040
return h('validate-control', { props, on })
4141
}

0 commit comments

Comments
 (0)