Skip to content

Commit 6abc18d

Browse files
committed
added integer support to collapsed prop
1 parent f1c8d7d commit 6abc18d

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ Name|Type|Default|Description
4545
`name`|`string`|"root"|Contains the name of your root node
4646
`theme`|`string`|"rjv-default"|RJV supports base-16 themes. Check out the [list of supported themes here](https://github.com/gaearon/base16-js/tree/master/src). A custom "rjv-default" theme applies by default.
4747
`indentWidth`|`integer`|4|Set the indent-width for nested objects
48-
`collapsed`|`boolean`|`false`|When set to `true`, all nodes will be collapsed by default
48+
`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.
4949
`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.
5050
`enableClipboard`|`boolean`|`true`|When set to `true`, the user can copy objects and arrays to clipboard
5151
`displayObjectSize`|`boolean`|`true`|When set to `true`, objects and arrays are labeled with size
5252
`displayDataTypes`|`boolean`|`true`|When set to `true`, data type labels prefix values
53+
`onEdit`|`(edit) => {}`|`false`|When a callback function is passed in, `edit` functionality is enabled. The callback is invoked before edits are completed. Returning `false` from `onEdit` will prevent the change from being made. [see: onEdit docs](#onedit-interaction)
5354
`onAdd`|`(add) => {}`|`false`|When a callback function is passed in, `add` functionality is enabled. The callback is invoked before additions are completed. Returning `false` from `onAdd` will prevent the change from being made.
5455
`onDelete`|`(delete) => {}`|`false`|When a callback function is passed in, `delete` functionality is enabled. The callback is invoked before deletions are completed. Returning `false` from `onDelete` will prevent the change from being made.
55-
`onEdit`|`(edit) => {}`|`false`|When a callback function is passed in, `edit` functionality is enabled. The callback is invoked before edits are completed. Returning `false` from `onEdit` will prevent the change from being made. [see: onEdit docs](#onedit-interaction)
5656

5757
### Features
5858
* Object and array nodes can be collapsed and expanded
@@ -148,4 +148,3 @@ I'm also inspired by users who come up with interesting feature requests. Reach
148148
### To-Do's
149149
1. Improve documentation for `onAdd` and `onDelete` props
150150
2. Improve style organization
151-
3. Support integer input to `collapsed` prop to collapse at a particular depth.

example/example.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ ReactDom.render(
4444

4545
<br />
4646

47+
{/* initialize this one with a name and default collapsed */}
48+
<JsonViewer
49+
src={getExampleJson2()}
50+
collapsed={1}
51+
name={'feature_set'}
52+
displayDataTypes={false}
53+
indentWidth={5}
54+
/>
55+
56+
<br />
57+
4758
{/* initialize an example with a long string */}
4859
<JsonViewer
4960
src={getExampleJson3()}

src/js/components/DataTypes/Object.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,23 @@ class rjvObject extends React.Component {
3636
state = {}
3737

3838
init = (props) => {
39+
const expanded = (
40+
props.collapsed === false
41+
|| (props.collapsed !== true
42+
&& props.collapsed > props.depth)
43+
);
3944
const state = {
4045
rjvId: props.rjvId,
4146
state_key: props.namespace.join('.'),
4247
namespace: props.namespace,
4348
indentWidth: props.indentWidth,
4449
expanded: props.jsvRoot
45-
? !props.collapsed
50+
? expanded
4651
: AttributeStore.get(
4752
props.rjvId,
4853
props.namespace,
4954
'expanded',
50-
!props.collapsed
55+
expanded
5156
),
5257
object_type: (props.type == 'array' ? 'array' : 'object'),
5358
parent_type: (props.type == 'array' ? 'array' : 'object'),

test/tests/js/components/DataTypes/Object-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ describe('<JsonObject />', function () {
5555
theme='rjv-default'
5656
indentWidth={1}
5757
depth={1}
58+
collapsed={false}
5859
displayDataTypes={true}
5960
type='object'/>
6061
);

0 commit comments

Comments
 (0)