This repository has been archived by the owner on Jun 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
renderTree.js
73 lines (58 loc) · 1.93 KB
/
renderTree.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'use strict'
import R from 'ramda';
import React, {PropTypes} from 'react';
import Registry from './registry';
import NotifyObservers from './components/core/NotifyObservers.react';
export default function render(component) {
if (R.contains(R.type(component), ['String', 'Number', 'Null'])) {
return component;
}
// Create list of child elements
let children;
const props = R.propOr({}, 'props', component);
// TODO - Rename component.content to component.children
if (!R.has('props', component) ||
!R.has('content', component.props) ||
typeof component.props.content === 'undefined') {
// No children
children = [];
} else if (R.contains(
R.type(component.props.content),
['String', 'Number', 'Null'])
) {
children = [component.props.content];
} else {
// One or multiple objects
// Recursively render the tree
// TODO - I think we should pass in `key` here.
children = (Array.isArray(props.content) ? props.content : [props.content])
.map(render);
}
if (!component.type) {
/* eslint-disable no-console */
console.error(R.type(component), component);
/* eslint-enable no-console */
throw new Error('component.type is undefined');
}
if (!component.namespace) {
/* eslint-disable no-console */
console.error(R.type(component), component);
/* eslint-enable no-console */
throw new Error('component.namespace is undefined');
}
const element = Registry.resolve(component.type, component.namespace);
const parent = React.createElement(
element,
R.omit(['content'], component.props),
...children
);
return (
<NotifyObservers id={props.id}>
{parent}
</NotifyObservers>
);
}
render.propTypes = {
content: PropTypes.object,
id: PropTypes.string
}