UNPKG

4.73 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _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; }; }();
8
9function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
10
11function _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; }
12
13function _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; }
14
15/**
16 * Created by cmeyers on 10/21/16.
17 */
18var React = require('react');
19var ReactDOM = require('react-dom');
20var PropTypes = React.PropTypes;
21
22var ContextBridge = require('./ContextBridge').ContextBridge;
23var ErrorUtils = require('./ErrorUtils').default;
24
25/**
26 * Component that renders any passed-in children via a separate ReactDOM.render call.
27 * Useful for rendering untrusted React elements: errors will be trapped and displayed so the main UI isn't broken.
28 */
29
30var SandboxedComponent = exports.SandboxedComponent = function (_React$Component) {
31 _inherits(SandboxedComponent, _React$Component);
32
33 function SandboxedComponent(props) {
34 _classCallCheck(this, SandboxedComponent);
35
36 var _this = _possibleConstructorReturn(this, (SandboxedComponent.__proto__ || Object.getPrototypeOf(SandboxedComponent)).call(this, props));
37
38 _this.domNode = null;
39 return _this;
40 }
41
42 _createClass(SandboxedComponent, [{
43 key: 'componentWillReceiveProps',
44 value: function componentWillReceiveProps(nextProps) {
45 if (nextProps.children !== this.props.children) {
46 this._cleanupChild();
47 this._renderChild(nextProps);
48 }
49 }
50 }, {
51 key: 'componentDidMount',
52 value: function componentDidMount() {
53 this._renderChild(this.props);
54 }
55 }, {
56 key: 'componentWillUnmount',
57 value: function componentWillUnmount() {
58 this._cleanupChild();
59 this.domNode = null;
60 }
61 }, {
62 key: '_renderChild',
63 value: function _renderChild(props) {
64 if (!this.domNode) {
65 return null;
66 }
67
68 try {
69 var contextValuesAsProps = {
70 config: this.context.config,
71 router: this.context.router
72 };
73 var bridgedComponent = React.createElement(ContextBridge, contextValuesAsProps, props.children);
74 ReactDOM.render(bridgedComponent, this.domNode);
75 } catch (e) {
76 console.warn('error rendering: ', e);
77 ReactDOM.render(ErrorUtils.errorToElement(e), this.domNode);
78 }
79 }
80 }, {
81 key: '_cleanupChild',
82 value: function _cleanupChild() {
83 if (this.domNode) {
84 try {
85 ReactDOM.unmountComponentAtNode(this.domNode);
86 } catch (err) {
87 console.log("Error unmounting component", err);
88 }
89 }
90 }
91 }, {
92 key: 'render',
93 value: function render() {
94 var _this2 = this;
95
96 var extraClass = this.props.className || '';
97
98 return React.createElement('div', { className: 'sandbox-component ' + extraClass, ref: function ref(node) {
99 _this2.domNode = node;
100 } });
101 }
102 }]);
103
104 return SandboxedComponent;
105}(React.Component);
106
107SandboxedComponent.propTypes = {
108 children: PropTypes.node,
109 className: PropTypes.string
110};
111
112SandboxedComponent.contextTypes = {
113 router: PropTypes.object,
114 config: PropTypes.object
115};
\No newline at end of file