react-json-view icon indicating copy to clipboard operation
react-json-view copied to clipboard

Automatic opening of the specified number of rows has stopped working

Open r0mm4k opened this issue 1 year ago • 4 comments

collapsed={2} --> not work (in v2.0.0-alpha.33 and new)

r0mm4k avatar Sep 25 '25 08:09 r0mm4k

You can see that it is not working in the website preview. Setting the collapsed setting to anything other than false shows just an empty object.

Image

Davian99 avatar Oct 06 '25 08:10 Davian99

We are running into this issue too. It seems to be coming from 87a29ecde8b477a097b31652281b0b09d32e0c77 and/or c76a9ee661d324801c68ac303e1727779079adb2. It seems that now, shouldExpandNodeInitially is defaulted to () => false but has precedence over collapsed in KeyValues.tsx, NestedOpen.tsx and NestedClosed.tsx, so collapsed never has a chance to apply.

I suppose we could work around this by using shouldExpandNodeInitially instead of collapsed, as the callback gets the level as a parameter. But perhaps it would be better to maintain compatibility until the next major release? What do you think @jaywcjlove ?

Meanwhile I will try to come up with a different fix for #76.

pascalspadone avatar Oct 07 '25 08:10 pascalspadone

If anyone is using patch-package, I think this patch fixes collapsed without breaking shouldExpandNodeInitially (I didn't do a lot of testing though)

diff --git a/node_modules/@uiw/react-json-view/esm/comps/KeyValues.js b/node_modules/@uiw/react-json-view/esm/comps/KeyValues.js
index 28a3446..f5e64db 100644
--- a/node_modules/@uiw/react-json-view/esm/comps/KeyValues.js
+++ b/node_modules/@uiw/react-json-view/esm/comps/KeyValues.js
@@ -38,7 +38,7 @@ export var KeyValues = props => {
     keyName,
     parentValue
   });
-  if (expands[expandKey] === undefined && !shouldExpand) {
+  if (expands[expandKey] === undefined && shouldExpandNodeInitially && !shouldExpand) {
     return null;
   }
   if (isExpanded) {
diff --git a/node_modules/@uiw/react-json-view/esm/comps/NestedClose.js b/node_modules/@uiw/react-json-view/esm/comps/NestedClose.js
index 328be2a..bde3b42 100644
--- a/node_modules/@uiw/react-json-view/esm/comps/NestedClose.js
+++ b/node_modules/@uiw/react-json-view/esm/comps/NestedClose.js
@@ -27,7 +27,7 @@ export var NestedClose = props => {
     keyName,
     parentValue
   });
-  if (expands[expandKey] === undefined && !shouldExpand) {
+  if (expands[expandKey] === undefined && shouldExpandNodeInitially && !shouldExpand) {
     return null;
   }
   var len = Object.keys(value).length;
diff --git a/node_modules/@uiw/react-json-view/esm/comps/NestedOpen.js b/node_modules/@uiw/react-json-view/esm/comps/NestedOpen.js
index 28ffa7a..9a4d0c0 100644
--- a/node_modules/@uiw/react-json-view/esm/comps/NestedOpen.js
+++ b/node_modules/@uiw/react-json-view/esm/comps/NestedOpen.js
@@ -36,7 +36,7 @@ export var NestedOpen = props => {
     keyName,
     parentValue
   });
-  if (expands[expandKey] === undefined && shouldExpandNodeInitially) {
+  if (expands[expandKey] === undefined && shouldExpandNodeInitially && !shouldExpand) {
     isExpanded = !shouldExpand;
   }
   var click = () => {
diff --git a/node_modules/@uiw/react-json-view/esm/index.js b/node_modules/@uiw/react-json-view/esm/index.js
index d050374..f8cf642 100644
--- a/node_modules/@uiw/react-json-view/esm/index.js
+++ b/node_modules/@uiw/react-json-view/esm/index.js
@@ -44,8 +44,8 @@ var JsonView = /*#__PURE__*/forwardRef((props, ref) => {
       style,
       value,
       children,
-      collapsed = false,
-      shouldExpandNodeInitially = () => true,
+      collapsed,
+      shouldExpandNodeInitially,
       indentWidth = 15,
       displayObjectSize = true,
       shortenTextAfterLength = 30,
@@ -72,7 +72,7 @@ var JsonView = /*#__PURE__*/forwardRef((props, ref) => {
       value,
       objectSortKeys,
       indentWidth,
-      shouldExpandNodeInitially: collapsed === false ? shouldExpandNodeInitially : () => false,
+      shouldExpandNodeInitially,
       displayObjectSize,
       collapsed,
       enableClipboard,

pascalspadone avatar Oct 07 '25 08:10 pascalspadone

The patch only partially solves it for me - now I can use true / false but trying to set a level (any level) just makes it all collapse always, so

collapsed={99}
collapsed={3}
collapsed={1}
collapsed={true}

all have the same effect.

Example

collapsed={1} Image

collapsed={3} Image

collapsed={99} Image

collapsed={false} Image

(without the patch it's always collapsed no matter what)

eladavron avatar Nov 18 '25 07:11 eladavron