/** * Copyright (c) 2017-present Jared Palmer * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ export * from './utils/isEmptyChildren'; import { ComponentClass } from 'react'; const REACT_STATICS: any = { childContextTypes: true, contextTypes: true, defaultProps: true, displayName: true, getDefaultProps: true, mixins: true, propTypes: true, type: true, }; const KNOWN_STATICS: any = { name: true, length: true, prototype: true, caller: true, callee: true, arguments: true, arity: true, }; const getOwnPropertySymbols = Object.getOwnPropertySymbols; const propIsEnumerable = Object.prototype.propertyIsEnumerable; const getPrototypeOf = Object.getPrototypeOf; const objectPrototype = getPrototypeOf && getPrototypeOf(Object); const getOwnPropertyNames = Object.getOwnPropertyNames; export function hoistNonReactStatics
( targetComponent: ComponentClass
,
sourceComponent: ComponentClass {
if (typeof sourceComponent !== 'string') {
// don't hoist over string (html) components
if (objectPrototype) {
let inheritedComponent = getPrototypeOf(sourceComponent);
if (inheritedComponent && inheritedComponent !== objectPrototype) {
hoistNonReactStatics(targetComponent, inheritedComponent, blacklist);
}
}
let keys = getOwnPropertyNames(sourceComponent);
if (getOwnPropertySymbols) {
keys = keys.concat(getOwnPropertySymbols(sourceComponent) as any);
}
for (let i = 0; i < keys.length; ++i) {
let key: string = keys[i];
if (
!REACT_STATICS[key] &&
!KNOWN_STATICS[key] &&
(!blacklist || !blacklist[key])
) {
// Only hoist enumerables and non-enumerable functions
if (
propIsEnumerable.call(sourceComponent, key) ||
typeof (sourceComponent as any)[key] === 'function'
) {
try {
// Avoid failures from read-only properties
(targetComponent as any)[key] = (sourceComponent as any)[key];
// tslint:disable-next-line:no-empty
} catch (e) {}
}
}
}
return targetComponent;
}
return targetComponent;
}