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

Highlight values by a given list of paths

Open MykolaGolubyev opened this issue 8 years ago • 2 comments

Hello, My scenario requires to highlights certain values within json. Will it be possible to add a new prop like "highlight" with a list of values like ["root.path.el[1]", "root.anotherPath"] so the corresponded values will have a distinct class names or styles assigned to them.

MykolaGolubyev avatar Sep 20 '17 10:09 MykolaGolubyev

You should be able to achieve this through the custom label/value renderer.

<JSONTree
    labelRenderer={raw => <strong>{raw}</strong>}
    valueRenderer={raw => <em>{raw}</em>}
/>

The accept a function that at the end receives the path to the key/value being rendered. For your use case, you could do something like

this.valueRenderer(pretty, raw, ...path) {
  if (path.join('.') === 'root.path.el') {
    return <span className="highlighted">{pretty}</span>
  }
  
  <span className="highlighted">{pretty}</span>
}

joshhunt avatar Sep 23 '17 20:09 joshhunt

Works like a charm! Thank you. Based on docs it wasn't clear if there is more than one parameter.

MykolaGolubyev avatar Sep 27 '17 20:09 MykolaGolubyev