-
Notifications
You must be signed in to change notification settings - Fork 125
/
Hoc.js
95 lines (83 loc) · 2.17 KB
/
Hoc.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import React from 'react'
import { View, throwError } from 'cerebral'
import PropTypes from 'prop-types'
class BaseComponent extends React.Component {
constructor(dependencies, mergeProps, props, controller, name) {
super(props)
if (!controller) {
throwError(
'Can not find controller, did you remember to use the Container component? Read more at: http://cerebraljs.com/docs/api/components.html#react'
)
}
this.onUpdate = this.onUpdate.bind(this)
this.view = new View({
dependencies,
mergeProps,
props,
controller,
displayName: name,
onUpdate: this.onUpdate,
})
this.view.mount()
}
/*
We only allow forced render by change of props passed
or Container tells it to render
*/
shouldComponentUpdate(nextProps) {
return this.view.onPropsUpdate(this.props, nextProps)
}
/*
We have to update any usage of "get" when the component mounts
*/
componentDidMount() {
if (this.view.dynamicDependencies.length) {
this.view.update(this.props)
}
}
/*
We have to update any usage of "get" when the component updates
*/
componentDidUpdate() {
if (this.view.dynamicDependencies.length) {
this.view.update(this.props)
}
}
/*
Unregister with existing state dependencies
*/
componentWillUnmount() {
this.view.unMount()
}
onUpdate(stateChanges, force) {
this.view.updateFromState(stateChanges, this.props, force)
this.forceUpdate()
}
}
export default function HOC(dependencies, mergeProps, Component) {
class CerebralComponent extends BaseComponent {
constructor(props, context) {
super(
dependencies,
mergeProps,
props,
context.controller,
Component.displayName || Component.name
)
}
toJSON() {
return this.view._displayName
}
render() {
return this.view.render(this.props, (props) =>
React.createElement(Component, props)
)
}
}
CerebralComponent.displayName = `CerebralWrapping_${Component.displayName ||
Component.name}`
CerebralComponent.contextTypes = {
controller: PropTypes.object,
}
return CerebralComponent
}