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

Commit 5340074

Browse files
committed
Merge pull request #18 from bvaughn/master
Support new `labelRenderer` and `valueRenderer` properties
2 parents d37ecea + 0dccc37 commit 5340074

18 files changed

+436
-697
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"

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,19 @@ Then the preview of child elements now look like this:
117117
118118
![](http://cl.ly/image/1J1a0b0T0K3c/screenshot%202015-10-07%20at%203.44.31%20PM.png)
119119
120+
#### Customize Rendering
121+
122+
You can pass the following properties to customize rendered labels and values:
123+
124+
```js
125+
<JSONTree
126+
labelRenderer={raw => <strong>{raw}</strong>}
127+
valueRenderer={raw => <em>{raw}</em>}
128+
/>
129+
```
130+
131+
In this example the label and value will be rendered with `<strong>` and `<em>` wrappers respectively.
132+
120133
### Credits
121134
122135
- All credits to [Dave Vedder](http://www.eskimospy.com/) ([[email protected]](mailto:[email protected])), who wrote the original code as [JSONViewer](https://bitbucket.org/davevedder/react-json-viewer/).

examples/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
},
3939
"dependencies": {
4040
"immutable": "^3.7.4",
41-
"react": "^0.13.0"
41+
"react": "^0.14.0",
42+
"react-dom": "^0.14.5"
4243
}
4344
}

examples/src/App.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ export default class App extends Component {
6565
getValueStyle={ getStyle }
6666
getItemString={ getItemString }/>
6767
</div>
68+
<h3>More Fine Grained Rendering</h3>
69+
<p>Pass <code>labelRenderer</code> or <code>valueRenderer</code>.</p>
70+
<div>
71+
<JSONTree data={ data }
72+
labelRenderer={raw => <strong>{ raw }</strong>}
73+
valueRenderer={raw => <em>{ raw }</em>} />
74+
</div>
6875
</div>
6976
);
7077
}

examples/src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1+
import { render } from 'react-dom';
12
import React from 'react';
23
import App from './App';
34

4-
React.render(<App />, document.getElementById('root'));
5+
render(<App />, document.getElementById('root'));

src/JSONArrayNode.js

Lines changed: 56 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,67 @@
11
import React from 'react';
2-
import reactMixin from 'react-mixin';
3-
import { ExpandedStateHandlerMixin } from './mixins';
4-
import JSONArrow from './JSONArrow';
2+
import JSONNestedNode from './JSONNestedNode';
53
import grabNode from './grab-node';
64

7-
const styles = {
8-
base: {
9-
position: 'relative',
10-
paddingTop: 3,
11-
paddingBottom: 3,
12-
paddingRight: 0,
13-
marginLeft: 14
14-
},
15-
label: {
16-
margin: 0,
17-
padding: 0,
18-
display: 'inline-block'
19-
},
20-
span: {
21-
cursor: 'default'
22-
},
23-
spanType: {
24-
marginLeft: 5,
25-
marginRight: 5
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' : '');
2614
}
27-
};
28-
29-
@reactMixin.decorate(ExpandedStateHandlerMixin)
30-
export default class JSONArrayNode extends React.Component {
31-
defaultProps = {
32-
data: [],
33-
initialExpanded: false
34-
};
35-
36-
// flag to see if we still need to render our child nodes
37-
needsChildNodes = true;
38-
39-
// cache store for our child nodes
40-
renderedChildren = [];
15+
return getItemString('Array', data, itemType, itemString);
16+
}
4117

42-
// cache store for the number of items string we display
43-
itemString = false;
18+
// Returns the child nodes for each entry in iterable.
19+
// If we have generated them previously we return from cache; otherwise we create them.
20+
function getChildNodes({
21+
data,
22+
getItemString,
23+
labelRenderer,
24+
previousData,
25+
styles,
26+
theme,
27+
valueRenderer
28+
}) {
29+
const childNodes = [];
30+
data.forEach((value, key) => {
31+
let previousDataValue;
32+
if (typeof previousData !== 'undefined' && previousData !== null) {
33+
previousDataValue = previousData[key];
34+
}
4435

45-
constructor(props) {
46-
super(props);
47-
this.state = {
48-
expanded: this.props.initialExpanded,
49-
createdChildNodes: false
50-
};
51-
}
36+
const node = grabNode({
37+
getItemString,
38+
key,
39+
labelRenderer,
40+
previousData: previousDataValue,
41+
renderItemString,
42+
styles,
43+
theme,
44+
value,
45+
valueRenderer
46+
});
5247

53-
// Returns the child nodes for each element in the array. If we have
54-
// generated them previously, we return from cache, otherwise we create
55-
// them.
56-
getChildNodes() {
57-
if (this.state.expanded && this.needsChildNodes) {
58-
let childNodes = [];
59-
this.props.data.forEach((element, idx) => {
60-
let prevData;
61-
if (typeof this.props.previousData !== 'undefined' && this.props.previousData !== null) {
62-
prevData = this.props.previousData[idx];
63-
}
64-
const node = grabNode(idx, element, prevData, this.props.theme, this.props.styles, this.props.getItemString);
65-
if (node !== false) {
66-
childNodes.push(node);
67-
}
68-
});
69-
this.needsChildNodes = false;
70-
this.renderedChildren = childNodes;
48+
if (node !== false) {
49+
childNodes.push(node);
7150
}
72-
return this.renderedChildren;
73-
}
51+
});
7452

75-
// Returns the "n Items" string for this node, generating and
76-
// caching it if it hasn't been created yet.
77-
getItemString(itemType) {
78-
if (!this.itemString) {
79-
this.itemString = this.props.data.length + ' item' + (this.props.data.length !== 1 ? 's' : '');
80-
}
81-
return this.props.getItemString('Array', this.props.data, itemType, this.itemString);
82-
}
53+
return childNodes;
54+
}
8355

84-
render() {
85-
const childNodes = this.getChildNodes();
86-
const childListStyle = {
87-
padding: 0,
88-
margin: 0,
89-
listStyle: 'none',
90-
display: (this.state.expanded) ? 'block' : 'none'
91-
};
92-
let containerStyle;
93-
let spanStyle = {
94-
...styles.span,
95-
color: this.props.theme.base0E
96-
};
97-
containerStyle = {
98-
...styles.base
99-
};
100-
if (this.state.expanded) {
101-
spanStyle = {
102-
...spanStyle,
103-
color: this.props.theme.base03
104-
};
105-
}
106-
return (
107-
<li style={containerStyle}>
108-
<JSONArrow theme={this.props.theme} open={this.state.expanded} onClick={::this.handleClick} style={this.props.styles.getArrowStyle(this.state.expanded)}/>
109-
<label style={{
110-
...styles.label,
111-
color: this.props.theme.base0D,
112-
...this.props.styles.getLabelStyle('Array', this.state.expanded)
113-
}} onClick={::this.handleClick}>
114-
{this.props.keyName}:
115-
</label>
116-
<span style={{
117-
...spanStyle,
118-
...this.props.styles.getItemStringStyle('Array', this.state.expanded)
119-
}} onClick={::this.handleClick}>
120-
{this.getItemString(<span style={styles.spanType}>[]</span>)}
121-
</span>
122-
<ol style={{
123-
...childListStyle,
124-
...this.props.styles.getListStyle('Array', this.state.expanded)
125-
}}>
126-
{childNodes}
127-
</ol>
128-
</li>
129-
);
130-
}
56+
// Configures <JSONNestedNode> to render an Array
57+
export default function JSONArrayNode({ ...props }) {
58+
return (
59+
<JSONNestedNode
60+
{...props}
61+
getChildNodes={getChildNodes}
62+
nodeType='Array'
63+
nodeTypeIndicator='[]'
64+
renderItemString={renderItemString}
65+
/>
66+
);
13167
}

src/JSONBooleanNode.js

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/JSONDateNode.js

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/JSONFunctionNode.js

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)