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
212 stars 12 forks source link

Get current object path with custom button #35

Open olavim opened 8 months ago

olavim commented 8 months ago

I'm trying to add a button next to the copy button which would get me the current object path. For example, if I have the object

{
  "id": "1",
  "data": {
    "foo": "bar"
  }
}

and I clicked the button on the "foo": "bar" row, I want to return "data.foo". Similarly, if I clicked the button on the "data": { row, I want to return "data".

Here's an image with the button for reference (fake data)

image

My first attempt was using the JsonView.Copied component renderer to just throw the button next to the copy button, but I don't have access to the current path in the object there. The renderer only knows the key and value of the row. I don't see how I would achieve what I want with the other available component renderers either.

Is there a method with the current functionality? If not, would it be possible to add support for this use-case?

jaywcjlove commented 8 months ago

@olavim Now it returns 4 parameters: value, keyName, keys, parentValue. Could you give it a try?

<Copied
  as="span"
  render={({ 'data-copied': copied, style, onClick, ...props }, { value, keyName, keys, parentValue }) => {
    const styl = { whiteSpace: 'nowrap' }
    if (copied) {
      return <span style={{ ...style, ...styl }}>复制成功</span>
    }
    return <span style={{ ...style, ...styl }} onClick={onClick}>复制</span>
  }}
/>
jaywcjlove commented 8 months ago

@olavim upgrade v2.0.0-alpha.17

olavim commented 8 months ago

Thanks for the quick response! The change is working well for me.

nnurmano commented 6 months ago

This does not return the json path as the author intended. I am on ^2.0.0-alpha.23 and parentValue returns { "foo": "bar" }, which does not have data key.

olavim commented 6 months ago

@nnurmano the keys prop is the JSON path array. Or did I misunderstand what you meant?

nnurmano commented 6 months ago

Indeed! Sorry, I was looking at the wrong place.

lsst25 commented 3 months ago

Hi, I also have a similar use case, thank you a lot for handling it!

It would be even better to have something like JsonView.Extra. It can be placed after the value and before the copy icon (because the copy icon is mingling and will cause the extra to jump left and right).

Current solution is coupled with the copy feature and will not work with enableClipboard={false}. Also, it's not straightforward to display an "extra" button permanently (it is hiding when not hovered as well as the copy icon).

Example:

      <JsonView value={data}>
        <JsonView.Extra render={({ keys }) => <button onClick={() => hanleCustomAction(keys)}>Custom Button</button>} />
      </JsonView>
jaywcjlove commented 2 months ago

@lsst25 You can achieve it using JsonView.Copied

<JsonView.Copied
  render={({ 'data-copied': copied, style, onClick, ...props }, { value }) => {
    const styl = { whiteSpace: 'nowrap' }
    return (
      <div>
        <button onClick={() => hanleCustomAction(keys)}>Custom Button</button>} />
        <span style={{ ...style, ...styl }} onClick={onClick}>
          {copied ? "复制成功" : "复制"}
        </span>
      </div>
    )
  }}
/>