|
1 |
| -# react-vite-component-template |
| 1 | +# react-json-tree |
2 | 2 |
|
3 |
| -Template for building a **React component library**, with **Vite**, **TypeScript** and **Storybook**. |
| 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 | 4 |
|
5 |
| -Check my post about this repository [here](https://victorlillo.dev/blog/react-typescript-vite-component-library). |
| 5 | + |
6 | 6 |
|
7 |
| -## Features |
| 7 | +### Usage |
8 | 8 |
|
9 |
| -- ⚛️ **React** component library with **TypeScript**. |
| 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'; |
10 | 13 |
|
11 |
| -- 🏗️ **Vite** as development environment. |
| 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 | +}; |
12 | 23 |
|
13 |
| -- 🌳 **Tree shaking**, for not distributing dead-code. |
| 24 | +<JSONTree data={json} />; |
| 25 | +``` |
14 | 26 |
|
15 |
| -- 📚 **Storybook** for live viewing the components. |
| 27 | +### Theming |
16 | 28 |
|
17 |
| -- 🎨 **PostCSS** for processing our CSS. |
| 29 | +TODO/In progress. All base styles are a single css class of specificity, and can be overwritten in the consuming application. |
18 | 30 |
|
19 |
| -- 🖌️ **CSS Modules** in development, compiled CSS for production builds. |
| 31 | +``` |
20 | 32 |
|
21 |
| -- 🧪 Testing with **Vitest** and **React Testing Library**. |
| 33 | +#### Advanced Customization |
22 | 34 |
|
23 |
| -- ✅ Code quality tools with **ESLint**, **Prettier** and **Stylelint**. |
| 35 | +```jsx |
| 36 | +<div> |
| 37 | + <JSONTree |
| 38 | + data={data} |
| 39 | + /> |
| 40 | +</div> |
| 41 | +``` |
24 | 42 |
|
25 |
| -## 🤖 Scripts |
| 43 | +#### Customize Labels for Arrays, Objects, and Iterables |
26 | 44 |
|
27 |
| -| Script | Function | |
28 |
| -| :---------------: | ---------------------------------------------------------------------------------- | |
29 |
| -| `build` | Build the `dist`, with types declarations, after checking types with TypeScript. | |
30 |
| -| `lint` | Lint the project with **Eslint**. | |
31 |
| -| `lint:fix` | Lint and fix the project with **Eslint**. | |
32 |
| -| `format` | Check the project format with **Prettier**. | |
33 |
| -| `format:fix` | Format the project code with **Prettier**. | |
34 |
| -| `stylelint` | Lint the styles code with **Stylelint**. | |
35 |
| -| `stylelint:fix` | Lint and fix the styles code with **Stylelint**. | |
36 |
| -| `storybook` | Start a Storybook development server. | |
37 |
| -| `build-storybook` | Build the Storybook `dist`. | |
38 |
| -| `test` | Run the tests with **Vitest** using `jsdom` and starts a **Vitest UI** dev server. | |
39 |
| -| `coverage` | Generate a coverage report, with **v8**. | |
| 45 | +You can pass `getItemString` to customize the way arrays, objects, and iterable nodes are displayed (optional). |
| 46 | + |
| 47 | +By default, it'll be: |
| 48 | + |
| 49 | +```jsx |
| 50 | +<JSONTree getItemString={(type, data, itemType, itemString, keyPath) |
| 51 | + => <span>{itemType} {itemString}</span>} |
| 52 | +``` |
| 53 | + |
| 54 | +But if you pass the following: |
| 55 | + |
| 56 | +```jsx |
| 57 | +const getItemString = (type, data, itemType, itemString, keyPath) |
| 58 | + => (<span> // {type}</span>); |
| 59 | +``` |
| 60 | +
|
| 61 | +Then the preview of child elements now look like this: |
| 62 | +
|
| 63 | + |
| 64 | +
|
| 65 | +#### Customize Rendering |
| 66 | +
|
| 67 | +You can pass the following properties to customize rendered labels and values: |
| 68 | +
|
| 69 | +```jsx |
| 70 | +<JSONTree |
| 71 | + labelRenderer={([key]) => <strong>{key}</strong>} |
| 72 | + valueRenderer={(raw) => <em>{raw}</em>} |
| 73 | +/> |
| 74 | +``` |
| 75 | +
|
| 76 | +In this example the label and value will be rendered with `<strong>` and `<em>` wrappers respectively. |
| 77 | +
|
| 78 | +For `labelRenderer`, you can provide a full path - [see this PR](https://github.com/chibicode/react-json-tree/pull/32). |
| 79 | +
|
| 80 | +Their full signatures are: |
| 81 | +
|
| 82 | +- `labelRenderer: function(keyPath, nodeType, expanded, expandable)` |
| 83 | +- `valueRenderer: function(valueAsString, value, ...keyPath)` |
| 84 | + |
| 85 | +#### More Options |
| 86 | + |
| 87 | +- `shouldExpandNodeInitially: function(keyPath, data, level)` - determines if node should be expanded when it first renders (root is expanded by default) |
| 88 | +- `hideRoot: boolean` - if `true`, the root node is hidden. |
| 89 | +- `sortObjectKeys: boolean | function(a, b)` - sorts object keys with compare function (optional). Isn't applied to iterable maps like `Immutable.Map`. |
| 90 | +- `postprocessValue: function(value)` - maps `value` to a new `value` |
| 91 | +- `isCustomNode: function(value)` - overrides the default object type detection and renders the value as a single value |
| 92 | +- `collectionLimit: number` - sets the number of nodes that will be rendered in a collection before rendering them in collapsed ranges |
| 93 | +- `keyPath: (string | number)[]` - overrides the initial key path for the root node (defaults to `[root]`) |
| 94 | + |
| 95 | +### Credits |
| 96 | + |
| 97 | +- 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/). |
| 98 | +- 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)) |
| 99 | +- [Iterable support](https://github.com/gaearon/redux-devtools/pull/79) thanks to [Daniel K](https://github.com/FredyC). |
| 100 | +- 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). |
| 101 | +- 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). |
| 102 | + |
| 103 | +### Similar Libraries |
| 104 | +- [original react-json-tree](https://github.com/reduxjs/redux-devtools/tree/main/packages/react-json-tree) |
| 105 | +- [react-treeview](https://github.com/chenglou/react-treeview) |
| 106 | +- [react-json-inspector](https://github.com/Lapple/react-json-inspector) |
| 107 | +- [react-object-inspector](https://github.com/xyc/react-object-inspector) |
| 108 | +- [react-json-view](https://github.com/mac-s-g/react-json-view) |
| 109 | + |
| 110 | +### License |
| 111 | + |
| 112 | +MIT |
0 commit comments