Skip to content

Commit f2cd3cd

Browse files
authored
Merge pull request mac-s-g#136 from ajbdev/array-groups
i added a couple of comments, but nothing blocking this pr from being merged. this is really cool. thanks again for contributing!
2 parents 09adc57 + cbb7bd8 commit f2cd3cd

File tree

17 files changed

+621
-184
lines changed

17 files changed

+621
-184
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Name|Type|Default|Description
4848
`indentWidth`|`integer`|4|Set the indent-width for nested objects
4949
`collapsed`|`boolean` or `integer`|`false`|When set to `true`, all nodes will be collapsed by default. Use an integer value to collapse at a particular depth.
5050
`collapseStringsAfterLength`|`integer`|`false`|When an integer value is assigned, strings will be cut off at that length. Collapsed strings are followed by an ellipsis. String content can be expanded and collapsed by clicking on the string value.
51+
`groupArraysAfterLength`|`integer`|`100`|When an integer value is assigned, arrays will be displayed in groups by count of the value. Groups are displayed with brakcet notation and can be expanded and collapsed by clickong on the brackets.
5152
`enableClipboard`|`boolean` or `(copy)=>{}`|`true`|When prop is not `false`, the user can copy objects and arrays to clipboard by clicking on the clipboard icon. Copy callbacks are supported.
5253
`displayObjectSize`|`boolean`|`true`|When set to `true`, objects and arrays are labeled with size
5354
`displayDataTypes`|`boolean`|`true`|When set to `true`, data type labels prefix values
@@ -63,6 +64,7 @@ Name|Type|Default|Description
6364
* Object and array nodes display length
6465
* Object and array nodes support a "Copy to Clipboard" feature
6566
* String values can be truncated after a specified length
67+
* Arrays can be subgrouped after a specified length
6668
* Base-16 Theme Support
6769
* When `onEdit` is enabled:
6870
* Double-Click Edit Mode

dev-server/src/index.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import JsonViewer from './../../src/js/index';
1010
//render 2 different examples of the react-json-view component
1111
ReactDom.render(
1212
<div>
13+
1314
{/* just pass in your JSON to the src attribute */}
1415
<JsonViewer
1516
style={{padding:'30px', backgroundColor: 'white'}}
@@ -59,7 +60,7 @@ ReactDom.render(
5960
name={'feature_set'}
6061
displayDataTypes={false}
6162
indentWidth={5}
62-
/>
63+
/>
6364

6465
<br />
6566

@@ -106,9 +107,19 @@ ReactDom.render(
106107
base0E: 'rgba(70, 70, 230, 1)',
107108
base0F: 'rgba(70, 70, 230, 1)'
108109
}}
110+
/>
111+
112+
113+
<JsonViewer
114+
theme="hopscotch"
115+
collapsed={false}
116+
name="large_array"
117+
groupArraysAfterLength={50}
118+
src={getExampleJson4()}
109119
/>
110120

111121

122+
112123
</div>,
113124
document.getElementById('app-container')
114125
);
@@ -199,6 +210,17 @@ function getExampleJson3() {
199210
}
200211

201212

213+
function getExampleJson4() {
214+
const large_array = new Array(225).fill('this is a large array full of items')
215+
216+
large_array.push(getExampleArray())
217+
218+
large_array.push(new Array(75).fill(Math.random()))
219+
220+
return large_array
221+
}
222+
223+
202224
function getExampleArray() {
203225
return [
204226
'you can also display arrays!',

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"dist/"
88
],
99
"dependencies": {
10-
"clipboard": "^1.7.1",
1110
"flux": "^3.1.3",
1211
"react-base16-styling": "^0.5.3",
1312
"react-textarea-autosize": "^5.1.0"

src/js/components/ArrayGroup.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import React from 'react';
2+
import Theme from './../themes/getStyle';
3+
4+
import VariableMeta from './VariableMeta';
5+
import ObjectName from './ObjectName'
6+
import ObjectComponent from './DataTypes/Object'
7+
8+
//icons
9+
import { CollapsedIcon, ExpandedIcon } from './ToggleIcons';
10+
11+
//single indent is 5px
12+
const SINGLE_INDENT = 5;
13+
14+
export default class extends React.Component {
15+
constructor(props) {
16+
super(props)
17+
18+
this.state = {
19+
expanded: {}
20+
}
21+
}
22+
23+
toggleCollapsed = (i) => {
24+
this.state.expanded[i] = !this.state.expanded[i]
25+
26+
this.setState(this.state);
27+
}
28+
29+
getExpandedIcon(i) {
30+
const { theme, iconStyle } = this.props
31+
32+
if (!!this.state.expanded[i]) {
33+
return <ExpandedIcon {...{theme, iconStyle}}/>
34+
}
35+
36+
return <CollapsedIcon {...{theme, iconStyle}} />
37+
}
38+
39+
render() {
40+
const {
41+
src, groupArraysAfterLength, depth,
42+
name, theme, jsvRoot, namespace, parent_type,
43+
...rest
44+
} = this.props
45+
46+
let expanded_icon, object_padding_left = 0;
47+
48+
const array_group_padding_left = this.props.indentWidth * SINGLE_INDENT
49+
50+
if (!jsvRoot) {
51+
object_padding_left = this.props.indentWidth * SINGLE_INDENT;
52+
}
53+
54+
const size = groupArraysAfterLength
55+
const groups = Math.ceil(src.length / size)
56+
57+
return (<div class='object-key-val'
58+
{...Theme(theme, jsvRoot ? 'jsv-root' : 'objectKeyVal', {paddingLeft: object_padding_left})}
59+
>
60+
<ObjectName {...this.props} />
61+
62+
<span>
63+
<VariableMeta size={src.length} {...this.props}/>
64+
</span>
65+
{[...Array(groups)].map((_, i) =>
66+
<div key={i} class='object-key-val array-group' {...Theme(theme, 'objectKeyVal', {
67+
marginLeft: 6,
68+
paddingLeft: array_group_padding_left
69+
})} >
70+
<span {...Theme(theme, 'brace-row')}>
71+
72+
<div class='icon-container' {...Theme(theme, 'icon-container')}
73+
onClick={(e) => {this.toggleCollapsed(i)}}>
74+
{this.getExpandedIcon(i)}
75+
</div>
76+
{!!this.state.expanded[i] ?
77+
<ObjectComponent key={name + i}
78+
depth={0}
79+
name={false}
80+
collapsed={false}
81+
groupArraysAfterLength={size}
82+
index_offset={i * size}
83+
src={src.slice(i * size, i * size + size)}
84+
namespace={namespace}
85+
type="array"
86+
parent_type="array_group"
87+
theme={theme}
88+
{...rest}
89+
/>
90+
: <span {...Theme(theme, 'brace')} onClick={(e) => {this.toggleCollapsed(i)}} class='array-group-brace'>
91+
[
92+
<div {...Theme(theme, 'array-group-meta-data')} class='array-group-meta-data'>
93+
<span class='object-size'
94+
{...Theme(theme, 'object-size')}>
95+
{i * size}
96+
{' - '}
97+
{i * size + size > src.length ? src.length : i * size + size}
98+
</span>
99+
</div>
100+
]
101+
</span>
102+
}
103+
104+
</span>
105+
</div>
106+
)}
107+
</div>
108+
);
109+
}
110+
111+
}

0 commit comments

Comments
 (0)