|
1 | 1 | 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'; |
5 | 3 | import grabNode from './grab-node';
|
6 | 4 |
|
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' : ''); |
26 | 14 | }
|
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 | +} |
41 | 17 |
|
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 | + } |
44 | 35 |
|
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 | + }); |
52 | 47 |
|
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); |
71 | 50 | }
|
72 |
| - return this.renderedChildren; |
73 |
| - } |
| 51 | + }); |
74 | 52 |
|
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 | +} |
83 | 55 |
|
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 | + ); |
131 | 67 | }
|
0 commit comments