UNPKG

2.99 kBJavaScriptView Raw
1function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
2
3function _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; }
4
5function _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; }
6
7function _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; }
8
9import React, { createElement, PureComponent } from 'react';
10
11var Fallback = function Fallback() {
12 return null;
13};
14
15var renderFromObject = function renderFromObject(tree, params) {
16 var components = params && params.components || {};
17 var fallback = params && params.fallback || Fallback;
18
19 var createChild = function createChild(c) {
20 if (c == null) {
21 return null;
22 }
23 if (Array.isArray(c)) {
24 return c.map(createChild);
25 }
26 if (typeof c === 'boolean' || typeof c === 'number' || typeof c === 'string') {
27 return c;
28 }
29 if (c.type) {
30 return createFromObject(c);
31 }
32 return null;
33 };
34
35 var createFromObject = function createFromObject(converted) {
36 var component = components[converted.type] || fallback;
37
38 if (!converted.props) {
39 return createElement(component);
40 }
41
42 var _converted$props = converted.props,
43 children = _converted$props.children,
44 props = _objectWithoutProperties(_converted$props, ['children']);
45
46 if (children != null) {
47 ;props.children = createChild(children);
48 }
49 return createElement(component, props);
50 };
51
52 return createFromObject(tree);
53};
54
55export { renderFromObject };
56
57
58export var Renderer = function (_PureComponent) {
59 _inherits(Renderer, _PureComponent);
60
61 function Renderer() {
62 _classCallCheck(this, Renderer);
63
64 return _possibleConstructorReturn(this, _PureComponent.apply(this, arguments));
65 }
66
67 Renderer.prototype.render = function render() {
68 var _props = this.props,
69 json = _props.json,
70 tree = _props.tree,
71 params = _objectWithoutProperties(_props, ['json', 'tree']);
72
73 var obj = void 0;
74 if (tree) obj = tree;else if (json) obj = JSON.parse(json); // flowlint-line sketchy-null-string:off
75
76 return obj ? renderFromObject(obj, params) : null;
77 };
78
79 return Renderer;
80}(PureComponent);
\No newline at end of file