UNPKG

2.46 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.copyRefs = copyRefs;
7
8var _react = require('react');
9
10var React = _interopRequireWildcard(_react);
11
12function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
13
14var REACT_METHODS = ['autobind', 'childContextTypes', 'componentDidMount', 'componentDidUpdate', 'componentWillMount', 'componentWillReceiveProps', 'componentWillUnmount', 'componentWillUpdate', 'contextTypes', 'displayName', 'getChildContext', 'getDefaultProps', 'getDOMNode', 'getInitialState', 'mixins', 'propTypes', 'render', 'replaceProps', 'setProps', 'shouldComponentUpdate', 'statics', 'updateComponent'];
15
16function copyRefs(TargetComponent, SourceComponent) {
17 if (!SourceComponent.prototype) {
18 return TargetComponent;
19 }
20
21 // $FlowFixMe
22 Object.getOwnPropertyNames(SourceComponent.prototype).filter(function (prop) {
23 return !(REACT_METHODS.includes(prop) || // React specific methods and properties
24 prop in React.Component.prototype || // Properties from React's prototype such as `setState`
25 // $FlowFixMe
26 prop in TargetComponent.prototype || // Properties from enhanced component's prototype
27 // Private methods
28 prop.startsWith('_'));
29 }).forEach(function (prop) {
30 // $FlowFixMe
31 if (typeof SourceComponent.prototype[prop] === 'function') {
32 /* eslint-disable func-names, no-param-reassign */
33 // $FlowFixMe
34 TargetComponent.prototype[prop] = function () {
35 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
36 args[_key] = arguments[_key];
37 }
38
39 // Make sure the function is called with correct context
40 // $FlowFixMe
41 return SourceComponent.prototype[prop].apply(this.getWrappedInstance(), args);
42 };
43 } else {
44 // Copy properties as getters and setters
45 // This make sure dynamic properties always stay up-to-date
46 // $FlowFixMe
47 Object.defineProperty(TargetComponent.prototype, prop, {
48 get: function get() {
49 return this.getWrappedInstance()[prop];
50 },
51 set: function set(value) {
52 this.getWrappedInstance()[prop] = value;
53 }
54 });
55 }
56 });
57
58 return TargetComponent;
59}
\No newline at end of file