uiwjs / react-json-view

A React component for displaying and editing javascript arrays and JSON objects.
https://uiwjs.github.io/react-json-view/
MIT License
182 stars 11 forks source link

Add support for preview when collapsed #54

Open igo opened 1 month ago

igo commented 1 month ago

Instead of showing three dots when collapsed

image

would be great to set which fields are important and try to show them in preview when collapsed:

image
jaywcjlove commented 1 month ago

@igo It looks easy to implement. How about adding a collapsedPreviewLength prop?

<JsonView
+  collapsedPreviewLength={12}
/>
igo commented 1 month ago

To make this useful I need to select which fields to show. collapsedPreviewLength would be useful to limit how many of those fields would fit on the screen.

jaywcjlove commented 1 month ago

@igo Would it be acceptable if the API is designed like this?

<JsonView
+  collapsedPreview={12}
/>
<JsonView
+  collapsedPreview="some text"
/>
<JsonView
+  collapsedPreview={(value, expandKey = '', level, keys = []) => {
+     return "some text"
+  }}
/>
igo commented 1 month ago

I do not fully understand your suggested API. I was thinking about something like this:

{
"user": { "username": "tim" } // firstName intentionally omitted
"address": [{"city": "NY"}]
}
<JsonView
collapsedPreviewKeys=['user.username', 'user.firstName', 'user.address.[0].city']
collapsedPreviewKeyName={true/false} // would show "username -> tim, city -> NY" if true else "tim, NY"
/>
jaywcjlove commented 1 month ago

@igo I think callback functions would be more flexible.

var object = {
  "user": { "username": "tim" }
  "address": [{"city": "NY"}]
}

<JsonView
  value={object}
  collapsedPreview={(value, expandKey = '', level, keys = []) => {
    if (expandKey == "user") {
      return value.username
    }
    if (expandKey == "city") {
      return value
    }
    if (keys.join(".") == "user.address.0.city") {
      return value
    }
  }}
/>