Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get current object path with custom button #35

Open
olavim opened this issue Feb 29, 2024 · 8 comments
Open

Get current object path with custom button #35

olavim opened this issue Feb 29, 2024 · 8 comments

Comments

@olavim
Copy link

olavim commented Feb 29, 2024

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?

@olavim olavim changed the title Adding extra functionality Get current object path with custom button Feb 29, 2024
jaywcjlove added a commit that referenced this issue Feb 29, 2024
github-actions bot pushed a commit that referenced this issue Feb 29, 2024
@jaywcjlove
Copy link
Member

@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
Copy link
Member

@olavim upgrade v2.0.0-alpha.17

@olavim
Copy link
Author

olavim commented Feb 29, 2024

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

@nnurmano
Copy link

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
Copy link
Author

olavim commented Apr 17, 2024

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

@nnurmano
Copy link

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

@lsst25
Copy link

lsst25 commented Jul 29, 2024

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
Copy link
Member

jaywcjlove commented Aug 6, 2024

@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>
    )
  }}
/>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants