Skip to content

Commit 9e85626

Browse files
committed
Support displaying limited number of array groups
1 parent cbf13bc commit 9e85626

File tree

13 files changed

+133
-23
lines changed

13 files changed

+133
-23
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ import ReactJsonView from '@microlink/react-json-view'
7373
| `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. |
7474
| `shouldCollapse` | `(field)=>{}` | `false` | Callback function to provide control over what objects and arrays should be collapsed by default. An object is passed to the callback containing `name`, `src`, `type` ("array" or "object") and `namespace`. |
7575
| `groupArraysAfterLength` | `integer` | 100 | When an integer value is assigned, arrays will be displayed in groups by count of the value. Groups are displayed with bracket notation and can be expanded and collapsed by clicking on the brackets. |
76+
| `numberOfArrayGroupsToDisplay` | `integer` or `null` | `null` | When an integer value is assigned, only the specified number of array groups will be displayed. If there are more groups, an ellipsis will be shown. This works in conjunction with groupArraysAfterLength. When null (default), all groups are displayed (no change in behavior). |
7677
| `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. |
7778
| `displayObjectSize` | `boolean` | `true` | When set to `true`, objects and arrays are labeled with size. |
7879
| `displayDataTypes` | `boolean` | `true` | When set to `true`, data type labels prefix values. |

dev-server/src/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,16 @@ ReactDom.render(
232232
src={getExampleJson4()}
233233
/>
234234

235+
{/* Demo of numberOfArrayGroupsToDisplay */}
236+
<JsonViewer
237+
theme='monokai'
238+
collapsed={false}
239+
name='array_with_limited_groups'
240+
groupArraysAfterLength={5}
241+
numberOfArrayGroupsToDisplay={3}
242+
src={getLargeArrayForDemo()}
243+
/>
244+
235245
{/* Name as colored react component */}
236246
<JsonViewer
237247
collapsed
@@ -368,3 +378,7 @@ function getExampleArray () {
368378
function getExampleWithStringEscapeSequences () {
369379
return { '\\\n\t\r\f\\n': '\\\n\t\r\f\\n' }
370380
}
381+
382+
function getLargeArrayForDemo () {
383+
return new Array(25).fill().map((_, i) => `Item ${i + 1}`)
384+
}

docs/src/js/entry.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require('./../style/scss/global.scss')
55

66
const app = document.getElementById('mac-react-container')
77

8-
//app entrypoint
8+
// app entrypoint
99
ReactDom.render(
1010
<div className='app-entry'>
1111
<Index />

index.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ export interface ReactJsonViewProps {
6868
* Default: 100
6969
*/
7070
groupArraysAfterLength?: number
71+
/**
72+
* When an integer value is assigned, only the specified number of array groups will be displayed.
73+
* If there are more groups, an ellipsis will be shown. This works in conjunction with groupArraysAfterLength.
74+
* When null (default), all groups are displayed (no change in behavior).
75+
*
76+
* Default: null
77+
*/
78+
numberOfArrayGroupsToDisplay?: number | null
7179
/**
7280
* When prop is not false, the user can copy objects and arrays to clipboard by clicking on the clipboard icon.
7381
* Copy callbacks are supported.

src/js/components/ArrayGroup.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export default class extends React.PureComponent {
4444
const {
4545
src,
4646
groupArraysAfterLength,
47+
numberOfArrayGroupsToDisplay,
4748
depth,
4849
name,
4950
theme,
@@ -53,36 +54,38 @@ export default class extends React.PureComponent {
5354
...rest
5455
} = this.props
5556

56-
let object_padding_left = 0
57+
let objectPaddingLeft = 0
5758

58-
const array_group_padding_left = this.props.indentWidth * SINGLE_INDENT
59+
const arrayGroupPaddingLeft = this.props.indentWidth * SINGLE_INDENT
5960

6061
if (!jsvRoot) {
61-
object_padding_left = this.props.indentWidth * SINGLE_INDENT
62+
objectPaddingLeft = this.props.indentWidth * SINGLE_INDENT
6263
}
6364

6465
const size = groupArraysAfterLength
6566
const groups = Math.ceil(src.length / size)
67+
const displayGroups = numberOfArrayGroupsToDisplay !== null && numberOfArrayGroupsToDisplay !== undefined ? Math.min(groups, numberOfArrayGroupsToDisplay) : groups
68+
const hasMoreGroups = groups > displayGroups
6669

6770
return (
6871
<div
6972
className='object-key-val'
7073
{...Theme(theme, jsvRoot ? 'jsv-root' : 'objectKeyVal', {
71-
paddingLeft: object_padding_left
74+
paddingLeft: objectPaddingLeft
7275
})}
7376
>
7477
<ObjectName {...this.props} />
7578

7679
<span>
7780
<VariableMeta size={src.length} {...this.props} />
7881
</span>
79-
{[...Array(groups)].map((_, i) => (
82+
{[...Array(displayGroups)].map((_, i) => (
8083
<div
8184
key={i}
8285
className='object-key-val array-group'
8386
{...Theme(theme, 'objectKeyVal', {
8487
marginLeft: 6,
85-
paddingLeft: array_group_padding_left
88+
paddingLeft: arrayGroupPaddingLeft
8689
})}
8790
>
8891
<span {...Theme(theme, 'brace-row')}>
@@ -110,7 +113,7 @@ export default class extends React.PureComponent {
110113
parent_type='array_group'
111114
theme={theme}
112115
showComma={this.props.showComma}
113-
isLast={i === groups - 1}
116+
isLast={i === displayGroups - 1 && !hasMoreGroups}
114117
{...rest}
115118
/>
116119
)
@@ -141,6 +144,21 @@ export default class extends React.PureComponent {
141144
</span>
142145
</div>
143146
))}
147+
{hasMoreGroups && (
148+
<div
149+
className='object-key-val array-group-ellipsis'
150+
{...Theme(theme, 'objectKeyVal', {
151+
marginLeft: 6,
152+
paddingLeft: arrayGroupPaddingLeft
153+
})}
154+
>
155+
<span {...Theme(theme, 'brace-row')}>
156+
<span {...Theme(theme, 'brace')} className='array-group-ellipsis'>
157+
...
158+
</span>
159+
</span>
160+
</div>
161+
)}
144162
</div>
145163
)
146164
}

src/js/components/DataTypes/Object.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ class RjvObject extends React.PureComponent {
240240
index_offset,
241241
groupArraysAfterLength,
242242
namespace,
243-
showComma,
243+
showComma
244244
} = this.props
245245
const { object_type } = this.state
246246
const elements = []

src/js/components/VariableEditor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ class VariableEditor extends React.PureComponent {
338338
if (BigNumber && parsedInput.type === 'bigNumber') {
339339
new_value = new BigNumber(new_value)
340340
}
341-
}
341+
}
342342
this.setState({
343343
editMode: false
344344
})

src/js/helpers/dispatcher.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Dispatcher {
22
handler = () => {}
33

4-
register(handler) {
4+
register (handler) {
55
this.handler = handler
66
}
77

8-
dispatch(data) {
8+
dispatch (data) {
99
this.handler?.(data)
1010
}
1111
}

src/js/helpers/util.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// returns a string "type" of input object
22
export function toType (obj, bigNumber) {
3-
43
/* Check if the object is an instance of the custom BigNumber class passed in as a prop
54
* If it matches, return 'bigNumber' type so it can be displayed appropriately
65
*/

src/js/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class ReactJsonView extends React.PureComponent {
4444
sortKeys: false,
4545
quotesOnKeys: true,
4646
groupArraysAfterLength: 100,
47+
numberOfArrayGroupsToDisplay: null,
4748
indentWidth: 4,
4849
enableClipboard: true,
4950
escapeStrings: true,

0 commit comments

Comments
 (0)