2016-10-17 15:40:11 +00:00
|
|
|
/**
|
2018-09-11 22:27:47 +00:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2016-10-17 15:40:11 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2018-05-09 07:47:48 +00:00
|
|
|
*
|
|
|
|
* @format
|
2016-10-17 15:40:11 +00:00
|
|
|
*/
|
2018-05-09 07:47:48 +00:00
|
|
|
|
2016-11-21 01:49:21 +00:00
|
|
|
'use strict';
|
2016-10-17 15:40:11 +00:00
|
|
|
|
2018-05-09 07:47:48 +00:00
|
|
|
module.exports = (moduleName, instanceMethods) => {
|
2016-10-17 15:40:11 +00:00
|
|
|
const RealComponent = require.requireActual(moduleName);
|
|
|
|
const React = require('react');
|
|
|
|
|
2018-05-09 07:47:48 +00:00
|
|
|
const SuperClass =
|
|
|
|
typeof RealComponent === 'function' ? RealComponent : React.Component;
|
|
|
|
|
|
|
|
const Component = class extends SuperClass {
|
2018-06-26 14:38:22 +00:00
|
|
|
static displayName = 'Component';
|
|
|
|
|
2016-10-17 15:40:11 +00:00
|
|
|
render() {
|
2018-06-12 02:10:27 +00:00
|
|
|
const name =
|
|
|
|
RealComponent.displayName ||
|
|
|
|
RealComponent.name ||
|
|
|
|
(RealComponent.render // handle React.forwardRef
|
|
|
|
? RealComponent.render.displayName || RealComponent.render.name
|
|
|
|
: 'Unknown');
|
2016-10-28 17:56:58 +00:00
|
|
|
|
2018-06-01 17:11:23 +00:00
|
|
|
const props = Object.assign({}, RealComponent.defaultProps);
|
|
|
|
|
|
|
|
if (this.props) {
|
|
|
|
Object.keys(this.props).forEach(prop => {
|
|
|
|
// We can't just assign props on top of defaultProps
|
|
|
|
// because React treats undefined as special and different from null.
|
|
|
|
// If a prop is specified but set to undefined it is ignored and the
|
|
|
|
// default prop is used instead. If it is set to null, then the
|
|
|
|
// null value overwrites the default value.
|
|
|
|
if (this.props[prop] !== undefined) {
|
|
|
|
props[prop] = this.props[prop];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-10-17 15:40:11 +00:00
|
|
|
return React.createElement(
|
2018-05-09 07:47:48 +00:00
|
|
|
name.replace(/^(RCT|RK)/, ''),
|
2018-06-01 17:11:23 +00:00
|
|
|
props,
|
2016-10-17 15:40:11 +00:00
|
|
|
this.props.children,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2018-05-09 07:47:48 +00:00
|
|
|
|
2018-06-20 04:28:00 +00:00
|
|
|
Object.keys(RealComponent).forEach(classStatic => {
|
|
|
|
Component[classStatic] = RealComponent[classStatic];
|
|
|
|
});
|
2018-05-09 07:47:57 +00:00
|
|
|
|
2018-05-09 07:47:48 +00:00
|
|
|
if (instanceMethods != null) {
|
|
|
|
Object.assign(Component.prototype, instanceMethods);
|
|
|
|
}
|
|
|
|
|
2016-10-17 15:40:11 +00:00
|
|
|
return Component;
|
|
|
|
};
|