@@ -39,15 +39,37 @@ export function styleObjToCss(s) {
39
39
return str || undefined ;
40
40
}
41
41
42
+ /**
43
+ * Copy all properties from `props` onto `obj`.
44
+ * @param {object } obj Object onto which properties should be copied.
45
+ * @param {object } props Object from which to copy properties.
46
+ * @returns {object }
47
+ * @private
48
+ */
42
49
export function assign ( obj , props ) {
43
50
for ( let i in props ) obj [ i ] = props [ i ] ;
44
51
return obj ;
45
52
}
46
53
54
+ /**
55
+ * Reconstruct Component-style `props` from a VNode.
56
+ * Ensures default/fallback values from `defaultProps`:
57
+ * Own-properties of `defaultProps` not present in `vnode.attributes` are added.
58
+ * @param {import('preact').VNode } vnode The VNode to get props for
59
+ * @returns {object } The props to use for this VNode
60
+ */
47
61
export function getNodeProps ( vnode ) {
48
- let defaultProps = vnode . nodeName . defaultProps ,
49
- props = assign ( { } , defaultProps || vnode . attributes ) ;
50
- if ( defaultProps ) assign ( props , vnode . attributes ) ;
51
- if ( vnode . children ) props . children = vnode . children ;
62
+ let props = assign ( { } , vnode . attributes ) ;
63
+ props . children = vnode . children ;
64
+
65
+ let defaultProps = vnode . nodeName . defaultProps ;
66
+ if ( defaultProps !== undefined ) {
67
+ for ( let i in defaultProps ) {
68
+ if ( props [ i ] === undefined ) {
69
+ props [ i ] = defaultProps [ i ] ;
70
+ }
71
+ }
72
+ }
73
+
52
74
return props ;
53
75
}
0 commit comments