[IMP] component: defaultProps application

We re-add the application of defaultProps. Note that the application
is done twice in dev mode.
This commit is contained in:
Mathieu Duckerts-Antoine
2021-10-22 11:56:56 +02:00
committed by Géry Debongnie
parent 6e0834ce5e
commit c3695ec8db
4 changed files with 131 additions and 44 deletions
+19
View File
@@ -1,5 +1,22 @@
import { Component } from "./component";
/**
* Apply default props (only top level).
*
* Note that this method does modify in place the props
*/
export function applyDefaultProps(props: { [key: string]: any }, ComponentClass: typeof Component) {
const defaultProps = (ComponentClass as any).defaultProps
if (defaultProps) {
for (let propName in defaultProps) {
if (props![propName] === undefined) {
props![propName] = defaultProps[propName];
}
}
}
}
//------------------------------------------------------------------------------
// Prop validation helper
//------------------------------------------------------------------------------
@@ -17,6 +34,8 @@ export const validateProps = function (name: string | typeof Component, props: a
const ComponentClass =
typeof name !== "string" ? name : parent.constructor.components[name as string];
applyDefaultProps(props, ComponentClass);
const propsDef = (<any>ComponentClass).props;
if (propsDef instanceof Array) {
// list of strings (prop names)