blob: a2f1f467f3eb65b47c63529d01c9866754a5413c (
plain)
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
|
const React = require('react');
const ReactRedux = require('react-redux');
/**
* Use the GitBook context provided by ContextProvider to map actions to props
* @param {ReactComponent} Component
* @param {Function} mapActionsToProps
* @return {ReactComponent}
*/
function connectToActions(Component, mapActionsToProps) {
if (!mapActionsToProps) {
return Component;
}
return React.createClass({
displayName: `ConnectActions(${Component.displayName})`,
propTypes: {
children: React.PropTypes.node
},
contextTypes: {
gitbookContext: React.PropTypes.object.isRequired
},
render() {
const { gitbookContext } = this.context;
const { children, ...props } = this.props;
const { actions, store } = gitbookContext;
const actionsProps = mapActionsToProps(actions, store.dispatch);
return <Component {...props} {...actionsProps}>{children}</Component>;
}
});
}
/**
* Connect a component to the GitBook context (store and actions).
*
* @param {ReactComponent} Component
* @param {Function} mapStateToProps
* @return {ReactComponent}
*/
function connect(Component, mapStateToProps, mapActionsToProps) {
const connectToStore = ReactRedux.connect(mapStateToProps);
return connectToActions(connectToStore(Component), mapActionsToProps);
}
module.exports = connect;
|