Skip to content

Commit 67bce7a

Browse files
committed
Use ReactDOMServer.renderToString to render HTML string
1 parent 35ab5ff commit 67bce7a

File tree

1 file changed

+71
-76
lines changed

1 file changed

+71
-76
lines changed

examples/renderer.js

Lines changed: 71 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { buildHTML, classNames, quoteattr } from './helper';
1+
import React from 'react';
2+
import ReactDOMServer from 'react-dom/server';
3+
import classNames from 'classnames';
24

35
const renderer = (node, treeOptions) => {
46
const { id, name, loadOnDemand = false, children, state, props = {} } = node;
@@ -7,84 +9,77 @@ const renderer = (node, treeOptions) => {
79
const childrenLength = Object.keys(children).length;
810
const more = node.hasChildren();
911

10-
let togglerContent = '';
11-
if (!more && loadOnDemand) {
12-
togglerContent = buildHTML('i', '', {
13-
'class': classNames('glyphicon', 'glyphicon-triangle-right')
14-
});
15-
}
16-
if (more && open) {
17-
togglerContent = buildHTML('i', '', {
18-
'class': classNames('glyphicon', 'glyphicon-triangle-bottom')
19-
});
20-
}
21-
if (more && !open) {
22-
togglerContent = buildHTML('i', '', {
23-
'class': classNames('glyphicon', 'glyphicon-triangle-right')
24-
});
25-
}
26-
const toggler = buildHTML('a', togglerContent, {
27-
'class': (() => {
28-
if (!more && loadOnDemand) {
29-
return classNames(treeOptions.togglerClass, 'infinite-tree-closed');
30-
}
31-
if (more && open) {
32-
return classNames(treeOptions.togglerClass);
33-
}
34-
if (more && !open) {
35-
return classNames(treeOptions.togglerClass, 'infinite-tree-closed');
36-
}
37-
return '';
38-
})()
39-
});
12+
return ReactDOMServer.renderToString(
13+
<div
14+
className={classNames(
15+
'infinite-tree-item',
16+
{ 'infinite-tree-selected': selected }
17+
)}
18+
data-id={id}
19+
data-expanded={more && open}
20+
data-depth={depth}
21+
data-path={path}
22+
data-selected={selected}
23+
data-children={childrenLength}
24+
data-total={total}
25+
droppable={droppable}
26+
>
27+
<div
28+
className="infinite-tree-node"
29+
style={{ marginLeft: depth * 18 }}
30+
>
31+
<a
32+
className={(() => {
33+
if (!more && loadOnDemand) {
34+
return classNames(treeOptions.togglerClass, 'infinite-tree-closed');
35+
}
36+
if (more && open) {
37+
return classNames(treeOptions.togglerClass);
38+
}
39+
if (more && !open) {
40+
return classNames(treeOptions.togglerClass, 'infinite-tree-closed');
41+
}
42+
return '';
43+
})()}
44+
>
45+
{!more && loadOnDemand &&
46+
<i className="glyphicon glyphicon-triangle-right" />
47+
}
48+
{more && open &&
49+
<i className="glyphicon glyphicon-triangle-bottom" />
50+
}
51+
{more && !open &&
52+
<i className="glyphicon glyphicon-triangle-right" />
53+
}
54+
</a>
4055

41-
const icon = buildHTML('i', '', {
42-
'class': classNames(
43-
'infinite-tree-folder-icon',
44-
'glyphicon',
45-
{ 'glyphicon-folder-open': more && open },
46-
{ 'glyphicon-folder-close': more && !open },
47-
{ 'glyphicon-file': !more }
48-
)
49-
});
50-
const title = buildHTML('span', quoteattr(name), {
51-
'class': classNames('infinite-tree-title')
52-
});
53-
const loadingIcon = buildHTML('i', '', {
54-
'style': 'margin-left: 5px',
55-
'class': classNames(
56-
{ 'hidden': !loading },
57-
'glyphicon',
58-
'glyphicon-refresh',
59-
{ 'rotating': loading }
60-
)
61-
});
62-
const count = buildHTML('span', childrenLength, {
63-
'class': 'count'
64-
});
65-
const treeNode = buildHTML('div', toggler + icon + title + loadingIcon + count, {
66-
'class': 'infinite-tree-node',
67-
'style': 'margin-left: ' + depth * 18 + 'px'
68-
});
56+
<i
57+
className={classNames(
58+
'infinite-tree-folder-icon',
59+
'glyphicon',
60+
{ 'glyphicon-folder-open': more && open },
61+
{ 'glyphicon-folder-close': more && !open },
62+
{ 'glyphicon-file': !more }
63+
)}
64+
>
65+
</i>
6966

70-
let treeNodeAttributes = {
71-
'data-id': id,
72-
'data-expanded': more && open,
73-
'data-depth': depth,
74-
'data-path': path,
75-
'data-selected': selected,
76-
'data-children': childrenLength,
77-
'data-total': total,
78-
'class': classNames(
79-
'infinite-tree-item',
80-
{ 'infinite-tree-selected': selected }
81-
)
82-
};
83-
if (droppable) {
84-
treeNodeAttributes['droppable'] = true;
85-
}
67+
<span className="infinite-tree-title">{name}</span>
8668

87-
return buildHTML('div', treeNode, treeNodeAttributes);
69+
<i
70+
style={{ marginLeft: 5 }}
71+
className={classNames(
72+
{ 'hidden': !loading },
73+
'glyphicon',
74+
'glyphicon-refresh',
75+
{ 'rotating': loading }
76+
)}
77+
/>
78+
79+
<span className="count">{childrenLength}</span>
80+
</div>
81+
</div>
82+
);
8883
};
8984

9085
export default renderer;

0 commit comments

Comments
 (0)