UNPKG

7.88 kBJavaScriptView Raw
1/**
2 * Baobab-React Higher Order Component
3 * ====================================
4 *
5 * ES6 higher order component to enchance one's component.
6 */
7'use strict';
8
9Object.defineProperty(exports, '__esModule', {
10 value: true
11});
12
13var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
14
15var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
16
17var _get = function get(_x2, _x3, _x4) { var _again = true; _function: while (_again) { var object = _x2, property = _x3, receiver = _x4; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x2 = parent; _x3 = property; _x4 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
18
19exports.root = root;
20exports.branch = branch;
21
22function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
23
24function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
25
26function _inherits(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) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
27
28var _react = require('react');
29
30var _react2 = _interopRequireDefault(_react);
31
32var _baobab = require('baobab');
33
34var _baobab2 = _interopRequireDefault(_baobab);
35
36var _utilsHelpersJs = require('./utils/helpers.js');
37
38var _utilsPropTypesJs = require('./utils/prop-types.js');
39
40var _utilsPropTypesJs2 = _interopRequireDefault(_utilsPropTypesJs);
41
42var makeError = _baobab2['default'].helpers.makeError;
43
44/**
45 * Root component
46 */
47
48function root(Component, tree) {
49 if (!(tree instanceof _baobab2['default'])) throw makeError('baobab-react:higher-order.root: given tree is not a Baobab.', { target: tree });
50
51 var componentDisplayName = Component.name || Component.displayName || 'Component';
52
53 var ComposedComponent = (function (_React$Component) {
54 _inherits(ComposedComponent, _React$Component);
55
56 function ComposedComponent() {
57 _classCallCheck(this, ComposedComponent);
58
59 _get(Object.getPrototypeOf(ComposedComponent.prototype), 'constructor', this).apply(this, arguments);
60 }
61
62 _createClass(ComposedComponent, [{
63 key: 'getChildContext',
64
65 // Handling child context
66 value: function getChildContext() {
67 return {
68 tree: tree
69 };
70 }
71
72 // Render shim
73 }, {
74 key: 'render',
75 value: function render() {
76 return _react2['default'].createElement(Component, this.props);
77 }
78 }], [{
79 key: 'displayName',
80 value: 'Rooted' + componentDisplayName,
81 enumerable: true
82 }, {
83 key: 'childContextTypes',
84 value: {
85 tree: _utilsPropTypesJs2['default'].baobab
86 },
87 enumerable: true
88 }]);
89
90 return ComposedComponent;
91 })(_react2['default'].Component);
92
93 return ComposedComponent;
94}
95
96/**
97 * Branch component
98 */
99
100function branch(Component) {
101 var mapping = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1];
102
103 var componentDisplayName = Component.name || Component.displayName || 'Component';
104
105 var ComposedComponent = (function (_React$Component2) {
106 _inherits(ComposedComponent, _React$Component2);
107
108 _createClass(ComposedComponent, [{
109 key: 'getChildContext',
110
111 // Passing the component's cursors through context
112 value: function getChildContext() {
113 return this.cursors ? {
114 cursors: this.cursors
115 } : {};
116 }
117
118 // Building initial state
119 }], [{
120 key: 'displayName',
121 value: 'Branched' + componentDisplayName,
122 enumerable: true
123 }, {
124 key: 'contextTypes',
125 value: {
126 tree: _utilsPropTypesJs2['default'].baobab
127 },
128 enumerable: true
129 }, {
130 key: 'childContextTypes',
131 value: {
132 cursors: _utilsPropTypesJs2['default'].cursors
133 },
134 enumerable: true
135 }]);
136
137 function ComposedComponent(props, context) {
138 _classCallCheck(this, ComposedComponent);
139
140 _get(Object.getPrototypeOf(ComposedComponent.prototype), 'constructor', this).call(this, props, context);
141
142 if (mapping.cursors) {
143 var solvedMapping = (0, _utilsHelpersJs.solveMapping)(mapping.cursors, props, context);
144
145 if (!solvedMapping) throw makeError('baobab-react:higher-order.branch: given cursors mapping is invalid (check the "' + displayName + '" component).', { mapping: solvedMapping });
146
147 // Creating the watcher
148 this.watcher = this.context.tree.watch(solvedMapping);
149 this.cursors = this.watcher.getCursors();
150 this.state = this.watcher.get();
151 }
152 }
153
154 // On component mount
155
156 _createClass(ComposedComponent, [{
157 key: 'componentWillMount',
158 value: function componentWillMount() {
159 if (!this.watcher) return;
160
161 var handler = (function () {
162 if (this.watcher) this.setState(this.watcher.get());
163 }).bind(this);
164
165 this.watcher.on('update', handler);
166 }
167
168 // Render shim
169 }, {
170 key: 'render',
171 value: function render() {
172 var tree = this.context.tree,
173 suppl = {};
174
175 // Binding actions if any
176 if (mapping.actions) {
177 suppl.actions = {};
178
179 Object.keys(mapping.actions).forEach(function (k) {
180 suppl.actions[k] = mapping.actions[k].bind(tree, tree);
181 });
182 }
183
184 return _react2['default'].createElement(Component, _extends({}, this.props, suppl, this.state));
185 }
186
187 // On component unmount
188 }, {
189 key: 'componentWillUnmount',
190 value: function componentWillUnmount() {
191 if (!this.watcher) return;
192
193 // Releasing watcher
194 this.watcher.release();
195 this.watcher = null;
196 }
197
198 // On new props
199 }, {
200 key: 'componentWillReceiveProps',
201 value: function componentWillReceiveProps(props) {
202 if (!this.watcher || !mapping.cursors) return;
203
204 var solvedMapping = (0, _utilsHelpersJs.solveMapping)(mapping.cursors, props, this.context);
205
206 if (!solvedMapping) throw makeError('baobab-react:higher-order.branch: given mapping is invalid (check the "' + displayName + '" component).', { mapping: solvedMapping });
207
208 // Refreshing the watcher
209 this.watcher.refresh(solvedMapping);
210 this.cursors = this.watcher.getCursors();
211 this.setState(this.watcher.get());
212 }
213 }]);
214
215 return ComposedComponent;
216 })(_react2['default'].Component);
217
218 return ComposedComponent;
219}
\No newline at end of file