• This repository has been archived on 15/May/2020
  • Stars
    star
    992
  • Rank 46,173 (Top 1.0 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 9 years ago
  • Updated almost 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

React JSON Viewer Component, Extracted from redux-devtools

This package was merged into redux-devtools monorepo. Please refer to that repository for the latest updates, issues and pull requests.


react-json-tree

React JSON Viewer Component, Extracted from redux-devtools. Supports iterable objects, such as Immutable.js.

Usage

import JSONTree from 'react-json-tree'
// If you're using Immutable.js: `npm i --save immutable`
import { Map } from 'immutable'

// Inside a React component:
const json = {
  array: [1, 2, 3],
  bool: true,
  object: {
    foo: 'bar'
  },
  immutable: Map({ key: 'value' })
}

<JSONTree data={json} />

Result:

Check out examples directory for more details.

Theming

This component now uses react-base16-styling module, which allows to customize component via theme property, which can be the following:

  • base16 theme data. The example theme data can be found here.
  • object that contains style objects, strings (that treated as classnames) or functions. A function is used to extend its first argument { style, className } and should return an object with the same structure. Other arguments depend on particular context (and should be described here). See createStylingFromTheme.js for the list of styling object keys. Also, this object can extend base16 theme via extend property.

Every theme has a light version, which is enabled with invertTheme prop.

const theme = {
  scheme: 'monokai',
  author: 'wimer hazenberg (http://www.monokai.nl)',
  base00: '#272822',
  base01: '#383830',
  base02: '#49483e',
  base03: '#75715e',
  base04: '#a59f85',
  base05: '#f8f8f2',
  base06: '#f5f4f1',
  base07: '#f9f8f5',
  base08: '#f92672',
  base09: '#fd971f',
  base0A: '#f4bf75',
  base0B: '#a6e22e',
  base0C: '#a1efe4',
  base0D: '#66d9ef',
  base0E: '#ae81ff',
  base0F: '#cc6633'
};

<div>
  <JSONTree data={data} theme={theme} invertTheme={false} />
</div>

Result (Monokai theme, dark background):

Advanced Customization

<div>
  <JSONTree data={data} theme={{
    extend: theme,
    // underline keys for literal values
    valueLabel: {
      textDecoration: 'underline'
    },
    // switch key for objects to uppercase when object is expanded.
    // `nestedNodeLabel` receives additional arguments `expanded` and `keyPath`
    nestedNodeLabel: ({ style }, nodeType, expanded) => ({
      style: {
        ...style,
        textTransform: expanded ? 'uppercase' : style.textTransform
      }
    })
  }} />
</div>

Customize Labels for Arrays, Objects, and Iterables

You can pass getItemString to customize the way arrays, objects, and iterable nodes are displayed (optional).

By default, it'll be:

<JSONTree getItemString={(type, data, itemType, itemString)
  => <span>{itemType} {itemString}</span>}

But if you pass the following:

const getItemString = (type, data, itemType, itemString)
  => (<span> // {type}</span>);

Then the preview of child elements now look like this:

Customize Rendering

You can pass the following properties to customize rendered labels and values:

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

In this example the label and value will be rendered with <strong> and <em> wrappers respectively.

For labelRenderer, you can provide a full path - see this PR.

More Options

  • shouldExpandNode: function(keyName, data, level) - determines if node should be expanded (root is expanded by default)
  • hideRoot: Boolean - if true, the root node is hidden.
  • sortObjectKeys: Boolean | function(a, b) - sorts object keys with compare function (optional). Isn't applied to iterable maps like Immutable.Map.

Credits

Similar Libraries

License

MIT

More Repositories

1

react-input-enhancements

Set of enhancements for input control
JavaScript
1,376
star
2

react-dock

Resizable dockable react component
JavaScript
544
star
3

webpack-chart

Webpack Chart
JavaScript
437
star
4

cake-chart

Interactive multi-layer pie chart
JavaScript
426
star
5

flask-react-boilerplate

JavaScript
368
star
6

redux-devtools-inspector

Another Redux DevTools Monitor
JavaScript
240
star
7

script-progress

Estimate script execution time
JavaScript
177
star
8

nyan-progress-webpack-plugin

Meow
JavaScript
172
star
9

markdown-react-js

Markdown to React Component converter
JavaScript
104
star
10

stellar-webpack

A little experiment
JavaScript
92
star
11

redux-pagan

managing internationalization via redux
JavaScript
78
star
12

restore-source-tree

Restores file structure from source map
JavaScript
62
star
13

additive-animation

Additive animation npm module
JavaScript
46
star
14

react-transform-debug-inspector

React inspector tranformation function for babel-plugin-wrap-react-components
JavaScript
29
star
15

react-base16-styling

React styling with base16 color scheme support
JavaScript
21
star
16

export-files-webpack-plugin

Exports files to FS (even in devserver mode)
JavaScript
17
star
17

SublimeLinter-inline-errors

Shows linting errors inline with Phantom API
Python
15
star
18

react-bootstrap-breadcrumbs

Breadcrumbs for React-Router and Bootstrap
JavaScript
15
star
19

react-pagan

JavaScript
14
star
20

jss-css

JavaScript
14
star
21

Tomato

Pebble pomodoro app
C
7
star
22

pg_unidecode

Postgres Unidecode extension
C
7
star
23

git-swap

Interactive, pure-shell menu for switching between git branches
Shell
5
star
24

sublime-bun

Bun binary files viewer and other Bun-related stuff
Python
5
star
25

sublime-yarn-lock

Syntax highlighting for yarn.lock and bun.lockb files
4
star
26

shittify-js

So you can honestly say your code is full of shit
JavaScript
4
star
27

babel-plugin-jss-css

JavaScript
4
star
28

svg-flag-editor

Online tool for editing flag svg image source
JavaScript
2
star
29

react-component-boilerplate

React component template
JavaScript
2
star
30

tslint-no-commented-code-rule

TSLint rule that bans commented code
TypeScript
2
star
31

sqlalchemy-flux-serializer

Serialize sqlalchemy models into flux-friendly json
Python
2
star
32

nodejs-dashboard-layout-progress

Layout for nodejs-dashboard with progress and status
JavaScript
2
star
33

alexkuz

HTML
2
star
34

eslint-plugin-jinja

This plugin treats Jinja template expressions as Javascript literals and ignores template statements and comments
JavaScript
2
star
35

mono-coffeescript-addin

CoffeeScript language addin for MonoDevelop
C#
1
star
36

custom-stickers

Python
1
star
37

SublimeLinter-contrib-zig-check

SublimeLinter plugin for Zig built-in linter (zig ast-check)
Python
1
star
38

alexkuz.github.io

HTML
1
star
39

bun-playground

Test repo to reproduce bun errors
TypeScript
1
star
40

python-rust-dprint

JavaScript
1
star
41

wds-ws-proxy-example

JavaScript
1
star
42

next-netlify-starter

JavaScript
1
star
43

nextjs-netlify-blog-template

TypeScript
1
star
44

shell-menu

Shell function for interactive menu
Shell
1
star
45

promisegate

Limits promise concurrency
JavaScript
1
star
46

micro-flag-icons

Collection of minimal country flag svg images
HTML
1
star