-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecorator.js
54 lines (46 loc) · 1.44 KB
/
Decorator.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
// @flow
import Compiler from './Compiler';
// types
import type {TConfig} from './Compiler';
const getDisplayName = (WrappedComponent: any) =>
WrappedComponent.displayName || WrappedComponent.name || 'Component';
export default function(config: TConfig) {
let compiler;
const decorator = function(WrappedComponent: any) {
const isStateless =
!WrappedComponent.prototype ||
(!WrappedComponent.isReactComponent &&
!WrappedComponent.prototype.render &&
!WrappedComponent.prototype.componentDidMount);
if (isStateless) {
return function ClassNamesComponent(props: Object) {
return compiler.traverse(WrappedComponent(props), props.children);
};
} else {
class Wrapper extends WrappedComponent {
static initClass() {
this.displayName = `ClassNames(${getDisplayName(WrappedComponent)})`;
}
render() {
if (WrappedComponent.prototype.render) {
return compiler.traverse(super.render(), this.props.children);
} else {
return compiler.traverse(
WrappedComponent(this.props, this.props.children),
this.props.children
);
}
}
}
Wrapper.initClass();
return Wrapper;
}
};
if (typeof config === 'function') {
compiler = new Compiler();
return decorator(config);
} else {
compiler = new Compiler(config);
return decorator;
}
}