Skip to content
This repository was archived by the owner on May 14, 2020. It is now read-only.

Commit 53ba4df

Browse files
committed
Tidied up a bit
1 parent ab73116 commit 53ba4df

File tree

6 files changed

+174
-105
lines changed

6 files changed

+174
-105
lines changed

.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// Temporarily disabled due to babel-eslint issues:
1414
"block-scoped-var": 0,
1515
"padded-blocks": 0,
16+
"no-param-reassign": 0,
1617
},
1718
"plugins": [
1819
"react"

src/JSONArrayNode.js

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,57 @@ import React from 'react';
22
import JSONNestedNode from './JSONNestedNode';
33
import grabNode from './grab-node';
44

5+
// Returns the "n Items" string for this node, generating and caching it if it hasn't been created yet.
6+
function renderItemString({
7+
data,
8+
getItemString,
9+
itemString,
10+
itemType
11+
}) {
12+
if (!itemString) {
13+
itemString = data.length + ' item' + (data.length !== 1 ? 's' : '');
14+
}
15+
return getItemString('Array', data, itemType, itemString);
16+
}
17+
518
// Returns the child nodes for each entry in iterable.
619
// If we have generated them previously we return from cache; otherwise we create them.
7-
function getChildNodes(context) {
8-
if (context.state.expanded && context.needsChildNodes) {
9-
let childNodes = [];
10-
context.props.data.forEach((value, key) => {
11-
let previousData;
12-
if (typeof context.props.previousData !== 'undefined' && context.props.previousData !== null) {
13-
previousData = context.props.previousData[key];
14-
}
15-
const node = grabNode({
16-
...context.props,
17-
key,
18-
previousData,
19-
value
20-
});
21-
if (node !== false) {
22-
childNodes.push(node);
23-
}
20+
function getChildNodes({
21+
data,
22+
getItemString,
23+
initialExpanded,
24+
labelRenderer,
25+
previousData,
26+
styles,
27+
theme,
28+
valueRenderer
29+
}) {
30+
const childNodes = [];
31+
data.forEach((value, key) => {
32+
let previousDataValue;
33+
if (typeof previousData !== 'undefined' && previousData !== null) {
34+
previousDataValue = previousData[key];
35+
}
36+
37+
const node = grabNode({
38+
getItemString,
39+
initialExpanded,
40+
key,
41+
labelRenderer,
42+
previousData: previousDataValue,
43+
renderItemString,
44+
styles,
45+
theme,
46+
value,
47+
valueRenderer
2448
});
25-
context.needsChildNodes = false;
26-
context.renderedChildren = childNodes;
27-
}
28-
return context.renderedChildren;
29-
}
3049

31-
// Returns the "n Items" string for this node, generating and caching it if it hasn't been created yet.
32-
function getItemString(itemType, context) {
33-
if (!context.itemString) {
34-
context.itemString = context.props.data.length + ' item' + (context.props.data.length !== 1 ? 's' : '');
35-
}
36-
return context.props.getItemString('Array', context.props.data, itemType, context.itemString);
50+
if (node !== false) {
51+
childNodes.push(node);
52+
}
53+
});
54+
55+
return childNodes;
3756
}
3857

3958
// Configures <JSONNestedNode> to render an Array
@@ -42,9 +61,9 @@ export default function JSONArrayNode({ ...props }) {
4261
<JSONNestedNode
4362
{...props}
4463
getChildNodes={getChildNodes}
45-
getItemStringWrapper={getItemString}
4664
nodeType='Array'
4765
nodeTypeIndicator='[]'
66+
renderItemString={renderItemString}
4867
/>
4968
);
5069
}

src/JSONIterableNode.js

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,14 @@ import React from 'react';
22
import JSONNestedNode from './JSONNestedNode';
33
import grabNode from './grab-node';
44

5-
// Returns the child nodes for each entry in iterable.
6-
// If we have generated them previously we return from cache; otherwise we create them.
7-
function getChildNodes(context) {
8-
if (context.state.expanded && context.needsChildNodes) {
9-
let childNodes = [];
10-
for (const entry of context.props.data) {
11-
let key = null;
12-
let value = null;
13-
if (Array.isArray(entry)) {
14-
[key, value] = entry;
15-
} else {
16-
key = childNodes.length;
17-
value = entry;
18-
}
19-
20-
let previousData;
21-
if (typeof context.props.previousData !== 'undefined' && context.props.previousData !== null) {
22-
previousData = context.props.previousData[key];
23-
}
24-
25-
const node = grabNode({
26-
...context.props,
27-
key,
28-
previousData,
29-
value
30-
});
31-
if (node !== false) {
32-
childNodes.push(node);
33-
}
34-
}
35-
context.needsChildNodes = false;
36-
context.renderedChildren = childNodes;
37-
}
38-
return context.renderedChildren;
39-
}
40-
415
// Returns the "n Items" string for this node, generating and caching it if it hasn't been created yet.
42-
function getItemString(itemType, context) {
43-
if (!context.itemString) {
44-
const { data } = context.props;
6+
function renderItemString({
7+
data,
8+
getItemString,
9+
itemString,
10+
itemType
11+
}) {
12+
if (!itemString) {
4513
let count = 0;
4614
if (Number.isSafeInteger(data.size)) {
4715
count = data.size;
@@ -50,9 +18,57 @@ function getItemString(itemType, context) {
5018
count += 1;
5119
}
5220
}
53-
context.itemString = count + ' entr' + (count !== 1 ? 'ies' : 'y');
21+
itemString = count + ' entr' + (count !== 1 ? 'ies' : 'y');
22+
}
23+
return getItemString('Iterable', data, itemType, itemString);
24+
}
25+
26+
// Returns the child nodes for each entry in iterable.
27+
// If we have generated them previously we return from cache; otherwise we create them.
28+
function getChildNodes({
29+
data,
30+
getItemString,
31+
initialExpanded,
32+
labelRenderer,
33+
previousData,
34+
styles,
35+
theme,
36+
valueRenderer
37+
}) {
38+
const childNodes = [];
39+
for (const entry of data) {
40+
let key = null;
41+
let value = null;
42+
if (Array.isArray(entry)) {
43+
[key, value] = entry;
44+
} else {
45+
key = childNodes.length;
46+
value = entry;
47+
}
48+
49+
let previousDataValue;
50+
if (typeof previousData !== 'undefined' && previousData !== null) {
51+
previousDataValue = previousData[key];
52+
}
53+
54+
const node = grabNode({
55+
getItemString,
56+
initialExpanded,
57+
key,
58+
labelRenderer,
59+
previousData: previousDataValue,
60+
styles,
61+
theme,
62+
value,
63+
valueRenderer
64+
});
65+
66+
if (node !== false) {
67+
childNodes.push(node);
68+
}
5469
}
55-
return context.props.getItemString('Iterable', context.props.data, itemType, context.itemString);
70+
71+
return childNodes;
5672
}
5773

5874
// Configures <JSONNestedNode> to render an iterable
@@ -61,9 +77,9 @@ export default function({ ...props }) {
6177
<JSONNestedNode
6278
{...props}
6379
getChildNodes={getChildNodes}
64-
getItemStringWrapper={getItemString}
6580
nodeType='Iterable'
6681
nodeTypeIndicator='()'
82+
renderItemString={renderItemString}
6783
/>
6884
);
6985
}

src/JSONNestedNode.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,22 @@ export default class JSONNestedNode extends React.Component {
7373
color: this.props.theme.base03
7474
};
7575
}
76+
77+
if (this.state.expanded && this.needsChildNodes) {
78+
this.needsChildNodes = false;
79+
this.renderedChildren = this.props.getChildNodes({
80+
...this.props
81+
});
82+
}
83+
84+
const itemType = <span style={styles.spanType}>{this.props.nodeTypeIndicator}</span>;
85+
const renderedItemString = this.props.renderItemString({
86+
data: this.props.data,
87+
getItemString: this.props.getItemString,
88+
itemString: this.itemString,
89+
itemType
90+
});
91+
7692
return (
7793
<li style={containerStyle}>
7894
<JSONArrow theme={this.props.theme} open={this.state.expanded} onClick={::this.handleClick} style={this.props.styles.getArrowStyle(this.state.expanded)} />
@@ -87,13 +103,13 @@ export default class JSONNestedNode extends React.Component {
87103
...spanStyle,
88104
...this.props.styles.getItemStringStyle(this.props.nodeType, this.state.expanded)
89105
}} onClick={::this.handleClick}>
90-
{this.props.getItemStringWrapper(<span style={styles.spanType}>{this.props.nodeTypeIndicator}</span>, this)}
106+
{renderedItemString}
91107
</span>
92108
<ul style={{
93109
...childListStyle,
94110
...this.props.styles.getListStyle(this.props.nodeType, this.state.expanded)
95111
}}>
96-
{this.props.getChildNodes(this)}
112+
{this.renderedChildren}
97113
</ul>
98114
</li>
99115
);

src/JSONObjectNode.js

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,60 @@ import React from 'react';
22
import JSONNestedNode from './JSONNestedNode';
33
import grabNode from './grab-node';
44

5+
// Returns the "n Items" string for this node, generating and caching it if it hasn't been created yet.
6+
function renderItemString({
7+
data,
8+
getItemString,
9+
itemString,
10+
itemType
11+
}) {
12+
if (!itemString) {
13+
const len = Object.keys(data).length;
14+
itemString = len + ' key' + (len !== 1 ? 's' : '');
15+
}
16+
return getItemString('Object', data, itemType, itemString);
17+
}
18+
519
// Returns the child nodes for each entry in iterable.
620
// If we have generated them previously we return from cache; otherwise we create them.
7-
function getChildNodes(context) {
8-
if (context.state.expanded && context.needsChildNodes) {
9-
const obj = context.props.data;
10-
let childNodes = [];
11-
for (let k in obj) {
12-
if (obj.hasOwnProperty(k)) {
13-
let previousData;
14-
if (typeof context.props.previousData !== 'undefined' && context.props.previousData !== null) {
15-
previousData = context.props.previousData[k];
16-
}
17-
const node = grabNode({
18-
...context.props,
19-
key: k,
20-
previousData,
21-
value: obj[k]
22-
});
23-
if (node !== false) {
24-
childNodes.push(node);
25-
}
21+
function getChildNodes({
22+
data,
23+
getItemString,
24+
initialExpanded,
25+
labelRenderer,
26+
previousData,
27+
styles,
28+
theme,
29+
valueRenderer
30+
}) {
31+
const childNodes = [];
32+
for (let key in data) {
33+
if (data.hasOwnProperty(key)) {
34+
let previousDataValue;
35+
if (typeof previousData !== 'undefined' && previousData !== null) {
36+
previousDataValue = previousData[key];
37+
}
38+
39+
const node = grabNode({
40+
getItemString,
41+
initialExpanded,
42+
key,
43+
labelRenderer,
44+
previousData: previousDataValue,
45+
renderItemString,
46+
styles,
47+
theme,
48+
value: data[key],
49+
valueRenderer
50+
});
51+
52+
if (node !== false) {
53+
childNodes.push(node);
2654
}
2755
}
28-
context.needsChildNodes = false;
29-
context.renderedChildren = childNodes;
3056
}
31-
return context.renderedChildren;
32-
}
3357

34-
// Returns the "n Items" string for context node, generating and caching it if it hasn't been created yet.
35-
function getItemString(itemType, context) {
36-
if (!context.itemString) {
37-
const len = Object.keys(context.props.data).length;
38-
context.itemString = len + ' key' + (len !== 1 ? 's' : '');
39-
}
40-
return context.props.getItemString('Object', context.props.data, itemType, context.itemString);
58+
return childNodes;
4159
}
4260

4361
// Configures <JSONNestedNode> to render an Object
@@ -46,9 +64,9 @@ export default function({ ...props }) {
4664
<JSONNestedNode
4765
{...props}
4866
getChildNodes={getChildNodes}
49-
getItemStringWrapper={getItemString}
5067
nodeType='Object'
5168
nodeTypeIndicator='{}'
69+
renderItemString={renderItemString}
5270
/>
5371
);
5472
}

src/grab-node.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ export default function({
3535
const nestedNodeProps = {
3636
...simpleNodeProps,
3737
data: value,
38-
getItemString,
3938
initialExpanded,
4039
keyName: key
4140
};

0 commit comments

Comments
 (0)