UNPKG

3.54 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _isObject2 = require('lodash/isObject');
8
9var _isObject3 = _interopRequireDefault(_isObject2);
10
11var _react = require('react');
12
13var _react2 = _interopRequireDefault(_react);
14
15var _hoistNonReactStatics = require('hoist-non-react-statics');
16
17var _hoistNonReactStatics2 = _interopRequireDefault(_hoistNonReactStatics);
18
19var _linkClass = require('./linkClass');
20
21var _linkClass2 = _interopRequireDefault(_linkClass);
22
23var _renderNothing = require('./renderNothing');
24
25var _renderNothing2 = _interopRequireDefault(_renderNothing);
26
27function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
28
29function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; }
30
31function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
32
33function _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; }
34
35function _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) : _defaults(subClass, superClass); } /* eslint-disable react/prop-types */
36
37/**
38 * @param {ReactClass} Component
39 * @param {Object} defaultStyles
40 * @param {Object} options
41 * @returns {ReactClass}
42 */
43exports.default = function (Component, defaultStyles, options) {
44 var WrappedComponent = function (_Component) {
45 _inherits(WrappedComponent, _Component);
46
47 function WrappedComponent() {
48 _classCallCheck(this, WrappedComponent);
49
50 return _possibleConstructorReturn(this, _Component.apply(this, arguments));
51 }
52
53 WrappedComponent.prototype.render = function render() {
54 var propsChanged = void 0;
55 var styles = void 0;
56
57 propsChanged = false;
58
59 if (this.props.styles) {
60 styles = this.props.styles;
61 } else if ((0, _isObject3.default)(defaultStyles)) {
62 var props = Object.assign({}, this.props);
63
64 Object.defineProperty(props, 'styles', {
65 configurable: true,
66 enumerable: false,
67 value: defaultStyles,
68 writable: false
69 });
70
71 this.props = props;
72
73 propsChanged = true;
74 styles = defaultStyles;
75 } else {
76 styles = {};
77 }
78
79 var renderResult = _Component.prototype.render.call(this);
80
81 if (propsChanged) {
82 delete this.props.styles;
83 }
84
85 if (renderResult) {
86 return (0, _linkClass2.default)(renderResult, styles, options);
87 }
88
89 return (0, _renderNothing2.default)(_react2.default.version);
90 };
91
92 return WrappedComponent;
93 }(Component);
94
95 return (0, _hoistNonReactStatics2.default)(WrappedComponent, Component);
96};
97
98module.exports = exports['default'];
\No newline at end of file