UNPKG

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