UNPKG

13.7 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _get = function get(object, property, receiver) { 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 { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
8
9var _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; }; }();
10
11var _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; };
12
13exports.default = enhanceWithRadium;
14
15var _react = require('react');
16
17var _propTypes = require('prop-types');
18
19var _propTypes2 = _interopRequireDefault(_propTypes);
20
21var _styleKeeper = require('./style-keeper');
22
23var _styleKeeper2 = _interopRequireDefault(_styleKeeper);
24
25var _resolveStyles2 = require('./resolve-styles');
26
27var _resolveStyles3 = _interopRequireDefault(_resolveStyles2);
28
29var _getRadiumStyleState = require('./get-radium-style-state');
30
31var _getRadiumStyleState2 = _interopRequireDefault(_getRadiumStyleState);
32
33function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
34
35function _objectWithoutProperties(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; }
36
37function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
38
39function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
40
41function _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; }
42
43var KEYS_TO_IGNORE_WHEN_COPYING_PROPERTIES = ['arguments', 'callee', 'caller', 'length', 'name', 'prototype', 'type'];
44
45var RADIUM_PROTO = void 0;
46var RADIUM_METHODS = void 0;
47
48function copyProperties(source, target) {
49 Object.getOwnPropertyNames(source).forEach(function (key) {
50 if (KEYS_TO_IGNORE_WHEN_COPYING_PROPERTIES.indexOf(key) < 0 && !target.hasOwnProperty(key)) {
51 var descriptor = Object.getOwnPropertyDescriptor(source, key);
52 Object.defineProperty(target, key, descriptor);
53 }
54 });
55}
56
57// Handle scenarios of:
58// - Inherit from `React.Component` in any fashion
59// See: https://github.com/FormidableLabs/radium/issues/738
60// - There's an explicit `render` field defined
61function isStateless(component) {
62 var proto = component.prototype || {};
63
64 return !component.isReactComponent && !proto.isReactComponent && !component.render && !proto.render;
65}
66
67// Check if value is a real ES class in Native / Node code.
68// See: https://stackoverflow.com/a/30760236
69function isNativeClass(component) {
70 return typeof component === 'function' && /^\s*class\s+/.test(component.toString());
71}
72
73function enhanceWithRadium(configOrComposedComponent) {
74 var _class, _temp;
75
76 var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
77
78 if (typeof configOrComposedComponent !== 'function') {
79 var newConfig = _extends({}, config, configOrComposedComponent);
80 return function (configOrComponent) {
81 return enhanceWithRadium(configOrComponent, newConfig);
82 };
83 }
84
85 var component = configOrComposedComponent;
86 var _ComposedComponent = component;
87
88 // Radium is transpiled in npm, so it isn't really using es6 classes at
89 // runtime. However, the user of Radium might be. In this case we have
90 // to maintain forward compatibility with native es classes.
91 if (isNativeClass(_ComposedComponent)) {
92 _ComposedComponent = function (OrigComponent) {
93 function NewComponent() {
94 // Use Reflect.construct to simulate 'new'
95 var obj = Reflect.construct(OrigComponent, arguments, this.constructor);
96
97 return obj;
98 }
99
100 // $FlowFixMe
101 Reflect.setPrototypeOf(NewComponent.prototype, OrigComponent.prototype);
102 // $FlowFixMe
103 Reflect.setPrototypeOf(NewComponent, OrigComponent);
104
105 return NewComponent;
106 }(_ComposedComponent);
107 }
108
109 // Handle stateless components
110 if (isStateless(_ComposedComponent)) {
111 _ComposedComponent = function (_Component) {
112 _inherits(ComposedComponent, _Component);
113
114 function ComposedComponent() {
115 _classCallCheck(this, ComposedComponent);
116
117 return _possibleConstructorReturn(this, (ComposedComponent.__proto__ || Object.getPrototypeOf(ComposedComponent)).apply(this, arguments));
118 }
119
120 _createClass(ComposedComponent, [{
121 key: 'render',
122 value: function render() {
123 return component(this.props, this.context);
124 }
125 }]);
126
127 return ComposedComponent;
128 }(_react.Component);
129
130 _ComposedComponent.displayName = component.displayName || component.name;
131 }
132
133 // Shallow copy composed if still original (we may mutate later).
134 if (_ComposedComponent === component) {
135 _ComposedComponent = function (_ComposedComponent2) {
136 _inherits(ComposedComponent, _ComposedComponent2);
137
138 function ComposedComponent() {
139 _classCallCheck(this, ComposedComponent);
140
141 return _possibleConstructorReturn(this, (ComposedComponent.__proto__ || Object.getPrototypeOf(ComposedComponent)).apply(this, arguments));
142 }
143
144 return ComposedComponent;
145 }(_ComposedComponent);
146 }
147
148 var RadiumEnhancer = (_temp = _class = function (_ComposedComponent3) {
149 _inherits(RadiumEnhancer, _ComposedComponent3);
150
151 function RadiumEnhancer() {
152 _classCallCheck(this, RadiumEnhancer);
153
154 var _this3 = _possibleConstructorReturn(this, (RadiumEnhancer.__proto__ || Object.getPrototypeOf(RadiumEnhancer)).apply(this, arguments));
155
156 _this3.state = _this3.state || {};
157 _this3.state._radiumStyleState = {};
158 _this3._radiumIsMounted = true;
159
160 var self = _this3;
161
162 // Handle es7 arrow functions on React class method names by detecting
163 // and transfering the instance method to original class prototype.
164 // (Using a copy of the class).
165 // See: https://github.com/FormidableLabs/radium/issues/738
166 RADIUM_METHODS.forEach(function (name) {
167 var thisDesc = Object.getOwnPropertyDescriptor(self, name);
168 var thisMethod = (thisDesc || {}).value;
169
170 // Only care if have instance method.
171 if (!thisMethod) {
172 return;
173 }
174
175 var radiumDesc = Object.getOwnPropertyDescriptor(RADIUM_PROTO, name);
176 var radiumProtoMethod = radiumDesc.value;
177 var superProtoMethod = _ComposedComponent.prototype[name];
178
179 // Allow transfer when:
180 // 1. have an instance method
181 // 2. the super class prototype doesn't have any method
182 // 3. it is not already the radium prototype's
183 if (!superProtoMethod && thisMethod !== radiumProtoMethod) {
184 // Transfer dynamic render component to Component prototype (copy).
185 Object.defineProperty(_ComposedComponent.prototype, name, thisDesc);
186
187 // Remove instance property, leaving us to have a contrived
188 // inheritance chain of (1) radium, (2) superclass.
189 delete self[name];
190 }
191 });
192 return _this3;
193 }
194
195 _createClass(RadiumEnhancer, [{
196 key: 'componentWillUnmount',
197 value: function componentWillUnmount() {
198 if (_get(RadiumEnhancer.prototype.__proto__ || Object.getPrototypeOf(RadiumEnhancer.prototype), 'componentWillUnmount', this)) {
199 _get(RadiumEnhancer.prototype.__proto__ || Object.getPrototypeOf(RadiumEnhancer.prototype), 'componentWillUnmount', this).call(this);
200 }
201
202 this._radiumIsMounted = false;
203
204 if (this._radiumMouseUpListener) {
205 this._radiumMouseUpListener.remove();
206 }
207
208 if (this._radiumMediaQueryListenersByQuery) {
209 Object.keys(this._radiumMediaQueryListenersByQuery).forEach(function (query) {
210 this._radiumMediaQueryListenersByQuery[query].remove();
211 }, this);
212 }
213 }
214 }, {
215 key: 'getChildContext',
216 value: function getChildContext() {
217 var superChildContext = _get(RadiumEnhancer.prototype.__proto__ || Object.getPrototypeOf(RadiumEnhancer.prototype), 'getChildContext', this) ? _get(RadiumEnhancer.prototype.__proto__ || Object.getPrototypeOf(RadiumEnhancer.prototype), 'getChildContext', this).call(this) : {};
218
219 if (!this.props.radiumConfig) {
220 return superChildContext;
221 }
222
223 var newContext = _extends({}, superChildContext);
224
225 if (this.props.radiumConfig) {
226 newContext._radiumConfig = this.props.radiumConfig;
227 }
228
229 return newContext;
230 }
231 }, {
232 key: 'render',
233 value: function render() {
234 var renderedElement = _get(RadiumEnhancer.prototype.__proto__ || Object.getPrototypeOf(RadiumEnhancer.prototype), 'render', this).call(this);
235 var currentConfig = this.props.radiumConfig || this.context._radiumConfig || config;
236
237 if (config && currentConfig !== config) {
238 currentConfig = _extends({}, config, currentConfig);
239 }
240
241 var _resolveStyles = (0, _resolveStyles3.default)(this, renderedElement, currentConfig),
242 extraStateKeyMap = _resolveStyles.extraStateKeyMap,
243 element = _resolveStyles.element;
244
245 this._extraRadiumStateKeys = Object.keys(extraStateKeyMap);
246
247 return element;
248 }
249
250 /* eslint-disable react/no-did-update-set-state, no-unused-vars */
251
252 }, {
253 key: 'componentDidUpdate',
254 value: function componentDidUpdate(prevProps, prevState) {
255 if (_get(RadiumEnhancer.prototype.__proto__ || Object.getPrototypeOf(RadiumEnhancer.prototype), 'componentDidUpdate', this)) {
256 _get(RadiumEnhancer.prototype.__proto__ || Object.getPrototypeOf(RadiumEnhancer.prototype), 'componentDidUpdate', this).call(this, prevProps, prevState);
257 }
258
259 if (this._extraRadiumStateKeys.length > 0) {
260 var trimmedRadiumState = this._extraRadiumStateKeys.reduce(function (state, key) {
261 var extraStateKey = state[key],
262 remainingState = _objectWithoutProperties(state, [key]);
263
264 return remainingState;
265 }, (0, _getRadiumStyleState2.default)(this));
266
267 this._lastRadiumState = trimmedRadiumState;
268 this.setState({ _radiumStyleState: trimmedRadiumState });
269 }
270 }
271 /* eslint-enable react/no-did-update-set-state, no-unused-vars */
272
273 }]);
274
275 return RadiumEnhancer;
276 }(_ComposedComponent), _class._isRadiumEnhanced = true, _temp);
277
278 // Lazy infer the method names of the Enhancer.
279
280 RADIUM_PROTO = RadiumEnhancer.prototype;
281 RADIUM_METHODS = Object.getOwnPropertyNames(RADIUM_PROTO).filter(function (n) {
282 return n !== 'constructor' && typeof RADIUM_PROTO[n] === 'function';
283 });
284
285 // Class inheritance uses Object.create and because of __proto__ issues
286 // with IE <10 any static properties of the superclass aren't inherited and
287 // so need to be manually populated.
288 // See http://babeljs.io/docs/advanced/caveats/#classes-10-and-below-
289 copyProperties(component, RadiumEnhancer);
290
291 if (process.env.NODE_ENV !== 'production') {
292 // This also fixes React Hot Loader by exposing the original components top
293 // level prototype methods on the Radium enhanced prototype as discussed in
294 // https://github.com/FormidableLabs/radium/issues/219.
295 copyProperties(_ComposedComponent.prototype, RadiumEnhancer.prototype);
296 }
297
298 if (RadiumEnhancer.propTypes && RadiumEnhancer.propTypes.style) {
299 RadiumEnhancer.propTypes = _extends({}, RadiumEnhancer.propTypes, {
300 style: _propTypes2.default.oneOfType([_propTypes2.default.array, _propTypes2.default.object])
301 });
302 }
303
304 RadiumEnhancer.displayName = component.displayName || component.name || 'Component';
305
306 RadiumEnhancer.contextTypes = _extends({}, RadiumEnhancer.contextTypes, {
307 _radiumConfig: _propTypes2.default.object,
308 _radiumStyleKeeper: _propTypes2.default.instanceOf(_styleKeeper2.default)
309 });
310
311 RadiumEnhancer.childContextTypes = _extends({}, RadiumEnhancer.childContextTypes, {
312 _radiumConfig: _propTypes2.default.object,
313 _radiumStyleKeeper: _propTypes2.default.instanceOf(_styleKeeper2.default)
314 });
315
316 return RadiumEnhancer;
317}
\No newline at end of file