1 | # react-json-tree
|
2 |
|
3 | React JSON Viewer Component, Extracted from [redux-devtools](https://github.com/reduxjs/redux-devtools). Supports [iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#iterable) objects, such as [Immutable.js](https://facebook.github.io/immutable-js/).
|
4 |
|
5 | ![](https://img.shields.io/npm/v/react-json-tree.svg)
|
6 |
|
7 | ### Usage
|
8 |
|
9 | ```jsx
|
10 | import { JSONTree } from 'react-json-tree';
|
11 | // If you're using Immutable.js: `npm i --save immutable`
|
12 | import { Map } from 'immutable';
|
13 |
|
14 | // Inside a React component:
|
15 | const json = {
|
16 | array: [1, 2, 3],
|
17 | bool: true,
|
18 | object: {
|
19 | foo: 'bar',
|
20 | },
|
21 | immutable: Map({ key: 'value' }),
|
22 | };
|
23 |
|
24 | <JSONTree data={json} />;
|
25 | ```
|
26 |
|
27 | #### Result:
|
28 |
|
29 | ![](https://i.ibb.co/0KSYRJg/example-result.png)
|
30 |
|
31 | Check out [examples](examples) directory for more details.
|
32 |
|
33 | ### Theming
|
34 |
|
35 | This component now uses [react-base16-styling](https://github.com/reduxjs/redux-devtools/tree/main/packages/react-base16-styling) module, which allows to customize component via `theme` property, which can be the following:
|
36 |
|
37 | - [base16](https://github.com/chriskempson/base16) theme data. [The example theme data can be found here](https://github.com/gaearon/redux-devtools/tree/75322b15ee7ba03fddf10ac3399881e302848874/src/react/themes).
|
38 | - 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](https://github.com/reduxjs/redux-devtools/blob/main/packages/react-json-tree/src/createStylingFromTheme.ts) for the list of styling object keys. Also, this object can extend `base16` theme via `extend` property.
|
39 |
|
40 | Every theme has a light version, which is enabled with `invertTheme` prop.
|
41 |
|
42 | ```jsx
|
43 | const theme = {
|
44 | scheme: 'monokai',
|
45 | author: 'wimer hazenberg (http://www.monokai.nl)',
|
46 | base00: '#272822',
|
47 | base01: '#383830',
|
48 | base02: '#49483e',
|
49 | base03: '#75715e',
|
50 | base04: '#a59f85',
|
51 | base05: '#f8f8f2',
|
52 | base06: '#f5f4f1',
|
53 | base07: '#f9f8f5',
|
54 | base08: '#f92672',
|
55 | base09: '#fd971f',
|
56 | base0A: '#f4bf75',
|
57 | base0B: '#a6e22e',
|
58 | base0C: '#a1efe4',
|
59 | base0D: '#66d9ef',
|
60 | base0E: '#ae81ff',
|
61 | base0F: '#cc6633',
|
62 | };
|
63 |
|
64 | <div>
|
65 | <JSONTree data={data} theme={theme} invertTheme={false} />
|
66 | </div>;
|
67 | ```
|
68 |
|
69 | #### Result (Monokai theme, dark background):
|
70 |
|
71 | ![](http://cl.ly/image/330o2L1J3V0h/screenshot%202015-08-26%20at%2010.48.24%20AM.png)
|
72 |
|
73 | #### Advanced Customization
|
74 |
|
75 | ```jsx
|
76 | <div>
|
77 | <JSONTree
|
78 | data={data}
|
79 | theme={{
|
80 | extend: theme,
|
81 | // underline keys for literal values
|
82 | valueLabel: {
|
83 | textDecoration: 'underline',
|
84 | },
|
85 | // switch key for objects to uppercase when object is expanded.
|
86 | // `nestedNodeLabel` receives additional argument `expandable`
|
87 | nestedNodeLabel: ({ style }, keyPath, nodeType, expanded) => ({
|
88 | style: {
|
89 | ...style,
|
90 | textTransform: expanded ? 'uppercase' : style.textTransform,
|
91 | },
|
92 | }),
|
93 | }}
|
94 | />
|
95 | </div>
|
96 | ```
|
97 |
|
98 | #### Customize Labels for Arrays, Objects, and Iterables
|
99 |
|
100 | You can pass `getItemString` to customize the way arrays, objects, and iterable nodes are displayed (optional).
|
101 |
|
102 | By default, it'll be:
|
103 |
|
104 | ```jsx
|
105 | <JSONTree getItemString={(type, data, itemType, itemString, keyPath)
|
106 | => <span>{itemType} {itemString}</span>}
|
107 | ```
|
108 |
|
109 | But if you pass the following:
|
110 |
|
111 | ```jsx
|
112 | const getItemString = (type, data, itemType, itemString, keyPath)
|
113 | => (<span> // {type}</span>);
|
114 | ```
|
115 |
|
116 | Then the preview of child elements now look like this:
|
117 |
|
118 | ![](http://cl.ly/image/1J1a0b0T0K3c/screenshot%202015-10-07%20at%203.44.31%20PM.png)
|
119 |
|
120 | #### Customize Rendering
|
121 |
|
122 | You can pass the following properties to customize rendered labels and values:
|
123 |
|
124 | ```jsx
|
125 | <JSONTree
|
126 | labelRenderer={([key]) => <strong>{key}</strong>}
|
127 | valueRenderer={(raw) => <em>{raw}</em>}
|
128 | />
|
129 | ```
|
130 |
|
131 | In this example the label and value will be rendered with `<strong>` and `<em>` wrappers respectively.
|
132 |
|
133 | For `labelRenderer`, you can provide a full path - [see this PR](https://github.com/chibicode/react-json-tree/pull/32).
|
134 |
|
135 | Their full signatures are:
|
136 |
|
137 | - `labelRenderer: function(keyPath, nodeType, expanded, expandable)`
|
138 | - `valueRenderer: function(valueAsString, value, ...keyPath)`
|
139 |
|
140 | #### More Options
|
141 |
|
142 | - `shouldExpandNodeInitially: function(keyPath, data, level)` - determines if node should be expanded when it first renders (root is expanded by default)
|
143 | - `hideRoot: boolean` - if `true`, the root node is hidden.
|
144 | - `sortObjectKeys: boolean | function(a, b)` - sorts object keys with compare function (optional). Isn't applied to iterable maps like `Immutable.Map`.
|
145 | - `postprocessValue: function(value)` - maps `value` to a new `value`
|
146 | - `isCustomNode: function(value)` - overrides the default object type detection and renders the value as a single value
|
147 | - `collectionLimit: number` - sets the number of nodes that will be rendered in a collection before rendering them in collapsed ranges
|
148 | - `keyPath: (string | number)[]` - overrides the initial key path for the root node (defaults to `[root]`)
|
149 |
|
150 | ### Credits
|
151 |
|
152 | - All credits to [Dave Vedder](http://www.eskimospy.com/) ([veddermatic@gmail.com](mailto:veddermatic@gmail.com)), who wrote the original code as [JSONViewer](https://bitbucket.org/davevedder/react-json-viewer/).
|
153 | - Extracted from [redux-devtools](https://github.com/gaearon/redux-devtools), which contained ES6 + inline style port of [JSONViewer](https://bitbucket.org/davevedder/react-json-viewer/) by [Daniele Zannotti](http://www.github.com/dzannotti) ([dzannotti@me.com](mailto:dzannotti@me.com))
|
154 | - [Iterable support](https://github.com/gaearon/redux-devtools/pull/79) thanks to [Daniel K](https://github.com/FredyC).
|
155 | - npm package created by [Shu Uesugi](http://github.com/chibicode) ([shu@chibicode.com](mailto:shu@chibicode.com)) per [this issue](https://github.com/gaearon/redux-devtools/issues/85).
|
156 | - Improved and maintained by [Alexander Kuznetsov](https://github.com/alexkuz). The repository was merged into [`redux-devtools` monorepo](https://github.com/reduxjs/redux-devtools) from [`alexkuz/react-json-tree`](https://github.com/alexkuz/react-json-tree).
|
157 |
|
158 | ### Similar Libraries
|
159 |
|
160 | - [react-treeview](https://github.com/chenglou/react-treeview)
|
161 | - [react-json-inspector](https://github.com/Lapple/react-json-inspector)
|
162 | - [react-object-inspector](https://github.com/xyc/react-object-inspector)
|
163 | - [react-json-view](https://github.com/mac-s-g/react-json-view)
|
164 |
|
165 | ### License
|
166 |
|
167 | MIT
|