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
/
Copy pathTreeContainer.js
86 lines (74 loc) · 2.26 KB
/
TreeContainer.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
74
75
76
77
78
79
80
81
82
83
84
85
86
'use strict';
import R from 'ramda';
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import Registry from './registry';
import NotifyObservers from './components/core/NotifyObservers.react';
export default class TreeContainer extends Component {
shouldComponentUpdate(nextProps) {
return nextProps.layout !== this.props.layout;
}
render() {
return render(this.props.layout);
}
}
TreeContainer.propTypes = {
layout: PropTypes.object,
};
function render(component) {
if (
R.contains(R.type(component), ['String', 'Number', 'Null', 'Boolean'])
) {
return component;
}
// Create list of child elements
let children;
const componentProps = R.propOr({}, 'props', component);
if (
!R.has('props', component) ||
!R.has('children', component.props) ||
typeof component.props.children === 'undefined'
) {
// No children
children = [];
} else if (
R.contains(R.type(component.props.children), [
'String',
'Number',
'Null',
'Boolean',
])
) {
children = [component.props.children];
} else {
// One or multiple objects
// Recursively render the tree
// TODO - I think we should pass in `key` here.
children = (Array.isArray(componentProps.children)
? componentProps.children
: [componentProps.children]
).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(['children'], component.props),
...children
);
return <NotifyObservers id={componentProps.id}>{parent}</NotifyObservers>;
}
render.propTypes = {
children: PropTypes.object,
};