|
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 |
26 |
| - } |
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 = []; |
41 |
| - |
42 |
| - // cache store for the number of items string we display |
43 |
| - itemString = false; |
44 |
| - |
45 |
| - constructor(props) { |
46 |
| - super(props); |
47 |
| - this.state = { |
48 |
| - expanded: this.props.initialExpanded, |
49 |
| - createdChildNodes: false |
50 |
| - }; |
51 |
| - } |
52 |
| - |
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 |
| - } |
| 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 | + 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 |
68 | 20 | });
|
69 |
| - this.needsChildNodes = false; |
70 |
| - this.renderedChildren = childNodes; |
71 |
| - } |
72 |
| - return this.renderedChildren; |
| 21 | + if (node !== false) { |
| 22 | + childNodes.push(node); |
| 23 | + } |
| 24 | + }); |
| 25 | + context.needsChildNodes = false; |
| 26 | + context.renderedChildren = childNodes; |
73 | 27 | }
|
| 28 | + return context.renderedChildren; |
| 29 | +} |
74 | 30 |
|
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); |
| 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' : ''); |
82 | 35 | }
|
| 36 | + return context.props.getItemString('Array', context.props.data, itemType, context.itemString); |
| 37 | +} |
83 | 38 |
|
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 |
| - } |
| 39 | +// Configures <JSONNestedNode> to render an Array |
| 40 | +export default function JSONArrayNode({ ...props }) { |
| 41 | + return ( |
| 42 | + <JSONNestedNode |
| 43 | + {...props} |
| 44 | + getChildNodes={getChildNodes} |
| 45 | + getItemStringWrapper={getItemString} |
| 46 | + nodeType='Array' |
| 47 | + nodeTypeIndicator='[]' |
| 48 | + /> |
| 49 | + ); |
131 | 50 | }
|
0 commit comments