-
-
Notifications
You must be signed in to change notification settings - Fork 214
/
extendReactClass.js
74 lines (57 loc) · 1.74 KB
/
extendReactClass.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* eslint-disable react/prop-types */
import _ from 'lodash';
import React from 'react';
import hoistNonReactStatics from 'hoist-non-react-statics';
import linkClass from './linkClass';
import renderNothing from './renderNothing';
/**
* @param {ReactClass} Component
* @param {Object} defaultStyles
* @param {Object} options
* @returns {ReactClass}
*/
export default (Component: Object, defaultStyles: Object, options: Object) => {
const WrappedComponent = class extends Component {
render () {
let styles;
const hasDefaultstyles = _.isObject(defaultStyles);
let renderResult;
if (this.props.styles || hasDefaultstyles) {
const props = Object.assign({}, this.props);
if (props.styles) {
styles = props.styles;
} else if (hasDefaultstyles) {
styles = defaultStyles;
delete props.styles;
}
Object.defineProperty(props, 'styles', {
configurable: true,
enumerable: false,
value: styles,
writable: false
});
const originalProps = this.props;
let renderIsSuccessful = false;
try {
this.props = props;
renderResult = super.render();
renderIsSuccessful = true;
} finally {
this.props = originalProps;
}
// @see https://github.com/facebook/react/issues/14224
if (!renderIsSuccessful) {
renderResult = super.render();
}
} else {
styles = {};
renderResult = super.render();
}
if (renderResult) {
return linkClass(renderResult, styles, options);
}
return renderNothing(React.version);
}
};
return hoistNonReactStatics(WrappedComponent, Component);
};