Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80529 views
1
'use strict';
2
3
var _interopRequire = function (obj) { return obj && obj.__esModule ? obj['default'] : obj; };
4
5
var _objectWithoutProperties = function (obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; };
6
7
var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } };
8
9
var _inherits = function (subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; };
10
11
/**
12
* Flux Component
13
*
14
* Component interface to reactComponentMethods module.
15
*
16
* Children of FluxComponent are given access to the flux instance via
17
* `context.flux`. Use this near the top of your app hierarchy and all children
18
* will have easy access to the flux instance (including, of course, other
19
* Flux components!):
20
*
21
* <FluxComponent flux={flux}>
22
* ...the rest of your app
23
* </FluxComponent>
24
*
25
* Now any child can access the flux instance again like this:
26
*
27
* <FluxComponent>
28
* ...children
29
* </FluxComponent>
30
*
31
* We don't need the flux prop this time because flux is already part of
32
* the context.
33
*
34
* Additionally, immediate children are given a `flux` prop.
35
*
36
* The component has an optional prop `connectToStores`, which is passed to
37
* `this.connectToStores` and used to set the initial state. The component's
38
* state is injected as props to the child components.
39
*
40
* The practical upshot of all this is that fluxMixin, state changes, and
41
* context are now simply implementation details. Among other things, this means
42
* you can write your components as plain ES6 classes. Here's an example:
43
*
44
* class ParentComponent extends React.Component {
45
*
46
* render() {
47
* <FluxComponent connectToStores="fooStore">
48
* <ChildComponent />
49
* </FluxComponent>
50
* }
51
*
52
* }
53
*
54
* ChildComponent in this example has prop `flux` containing the flux instance,
55
* and props that sync with each of the state keys of fooStore.
56
*/
57
58
var _React = require('react/addons');
59
60
var React = _interopRequire(_React);
61
62
var _instanceMethods$staticProperties = require('./reactComponentMethods');
63
64
var _assign = require('object-assign');
65
66
var assign = _interopRequire(_assign);
67
68
var FluxComponent = (function (_React$Component) {
69
function FluxComponent(props, context) {
70
_classCallCheck(this, FluxComponent);
71
72
_React$Component.call(this, props, context);
73
74
this.initialize();
75
76
this.state = this.connectToStores(props.connectToStores, props.stateGetter);
77
78
this.wrapChild = this.wrapChild.bind(this);
79
}
80
81
_inherits(FluxComponent, _React$Component);
82
83
FluxComponent.prototype.wrapChild = function wrapChild(child) {
84
return React.addons.cloneWithProps(child, this.getChildProps());
85
};
86
87
FluxComponent.prototype.getChildProps = function getChildProps() {
88
var _props = this.props;
89
var children = _props.children;
90
var render = _props.render;
91
var connectToStores = _props.connectToStores;
92
var stateGetter = _props.stateGetter;
93
var flux = _props.flux;
94
95
var extraProps = _objectWithoutProperties(_props, ['children', 'render', 'connectToStores', 'stateGetter', 'flux']);
96
97
return assign({ flux: this.getFlux() }, // TODO: remove in next major version
98
this.state, extraProps);
99
};
100
101
FluxComponent.prototype.render = (function (_render) {
102
function render() {
103
return _render.apply(this, arguments);
104
}
105
106
render.toString = function () {
107
return render.toString();
108
};
109
110
return render;
111
})(function () {
112
var _props2 = this.props;
113
var children = _props2.children;
114
var render = _props2.render;
115
116
if (typeof render === 'function') {
117
return render(this.getChildProps(), this.getFlux());
118
}
119
120
if (!children) return null;
121
122
if (!Array.isArray(children)) {
123
var child = children;
124
return this.wrapChild(child);
125
} else {
126
return React.createElement(
127
'span',
128
null,
129
React.Children.map(children, this.wrapChild)
130
);
131
}
132
});
133
134
return FluxComponent;
135
})(React.Component);
136
137
assign(FluxComponent.prototype, _instanceMethods$staticProperties.instanceMethods);
138
139
assign(FluxComponent, _instanceMethods$staticProperties.staticProperties);
140
141
module.exports = FluxComponent;
142