0.2.8 • Published 1 month ago

react18-json-view v0.2.8

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

React18 JSON View

React function component for displaying javascript arrays and JSON objects. Supports all JS types.

Website, Storybook,Online

JSON View

Installation

npm i react18-json-view
npm i react18-json-view@canary

Usage

import JsonView from 'react18-json-view'
import 'react18-json-view/src/style.css'
// If dark mode is needed, import `dark.css`.
// import 'react18-json-view/src/dark.css'

<JsonView src={my_json_object} />

// If needed, you can use the internal stringify function.
// import { stringify } from 'react18-json-view'

Props

NameTypeDefaultDescription
srcJSON ObjectNoneThis property contains your input JSON
classNamestringNoneThe CSS class name(s) to apply to the component.
styleJSON ObjectNoneAn object containing custom style rules to apply to the component.
darkbooleanfalseKeep in dark mode (Don't forget to import dark.css)
themedefault | a11y | github | vscode | atom|winter-is-coming'default'Color theme
enableClipboardbooleantrueWhether enable clipboard feature.
matchesURLbooleantrueShow the link icon if value is string and matches URL regex pattern.
urlRegExpRegExp/^(((ht\|f)tps?):\/\/)?([^!@#$%^&*?.\s-]([^!@#$%^&*?.\s]{0,63}[^!@#$%^&*?.\s])?\.)+[a-z]{2,6}\/?/URL RegExp pattern.
displaySizeboolean | integer | 'collapsed' | 'expanded'falseWhether display the size of Object, Array.
collapseStringsAfterLengthinteger99When an integer value is assigned, strings longer than that length will be truncated and indicated by an ellipsis. To expand or collapse the string content, simply click on the string value.
customizeCollapseStringUI(str_show: string, truncated: boolean) => (JSX.Element \| string) | string-Customize the collapse string UI.
ignoreLargeArraybooleanfalsePrevent collapsing large array(length > 100) behavior since v0.2.7
collapseStringMode'directly' | 'word' | 'address''directly'If the word is assigned, the collapsed length will be adjusted to fully display the last word.
collapsedboolean | integer | functionfalseWhen set to true(false), all nodes will be (not) collapsed by default. When using an integer value, it will collapse at a specific depth. The collapsed also can be a function.
collapseObjectsAfterLengthinteger99When an integer value is assigned, the object and array will initially collapse.
editableboolean | {add?: boolean, edit?: boolean, delete?: boolean}falseWhen set to true, you can add, edit, or delete the property, and the actions will trigger onAdd, onEdit, or onDelete. Options is available.
onAddfunction-(params: { indexOrName: string\| number, depth: number, src: any; parentType: 'object' \| 'array' }) => void
onDeletefunction-(params:{ value: any,indexOrName: string \| number,depth: number,src: any,parentType: 'object' \| 'array'}) => void
onEditfunction-(params: { newValue: any, oldValue: any, depth: number, src: any, indexOrName: string \| number, parentType: 'object' \| 'array'}) => void
customizeNodeReactElement|ReactComponent|Options-Highly customize every node.
customizeCopy(node: any) => anyinternal stringifyCustomize copy behavior, only the returned non-empty string will be written to clipboard.

Collapsed function

(params: {
    node: Record<string, any> | Array<any> // Object or array
    indexOrName: number | string | undefined
    depth: number
    size: number // Object's size or array's length
  }) => boolean

Editable options

{
  add?: boolean
  edit?: boolean
  delete?: boolean
}

CustomizeNode

(params: { node: any; indexOrName: number | string | undefined; depth: number }) =>
  | {
    add?: boolean
    edit?: boolean
    delete?: boolean
    enableClipboard?: boolean
    collapsed?: boolean
    className?: string
  }
  | React.FC
  | typeof React.Component
  | React.ReactElement<any, any>

Editable

How to generate object/array

The editor uses eval(<input-value>). While in edit mode, you can enter ({}) or ([]), which will cause the result of eval to become a new object or array.

{} and [] will be auto convert to ({}),([])

How the editor works

This component does not perform any cloning operations, so every step of the operation is carried out on the original object. If cloning is required, please handle it yourself.

Edit keyboard shortcuts

When element is editable:

  • Ctrl/Cmd+Click => Edit Mode
  • Enter => Submit
  • Esc => Cancel

Custom themes

Below are the default theme variables that you can easily customize to fit your needs.

.json-view {
  color: #4d4d4d;
  --json-property: #009033;
  --json-index: #676dff;
  --json-number: #676dff;
  --json-string: #b2762e;
  --json-boolean: #dc155e;
  --json-null: #dc155e;
}
.json-view .json-view--property { color: var(--json-property); }
.json-view .json-view--index { color: var(--json-index); }
.json-view .json-view--number { color: var(--json-number); }
.json-view .json-view--string { color: var(--json-string); }
.json-view .json-view--boolean { color: var(--json-boolean); }
.json-view .json-view--null { color: var(--json-null); }

Why

react-json-view does not support React 18.

Todo

  • copy (enableClipboard)
  • css
  • collapse at a particular depth (collapsed)
  • editable
    • add
    • edit
    • delete
    • onChange
    • onSelect
  • dark mode
  • custom icon
    • export default icons
  • more usability/scenarios
  • gif guide
  • more color themes(dark)
  • collapse objects callback
  • editable option
  • advance customization
    • access internal actions
  • map/set viewer
  • display data type
  • display object size
  • handle circle loop
  • redesign docs
  • truncate long strings
  • custom stringify
  • split large array
  • tree?
0.2.8

1 month ago

0.2.8-canary.6

1 month ago

0.2.8-canary.5

2 months ago

0.2.8-canary.2

2 months ago

0.2.8-canary.4

2 months ago

0.2.8-canary.3

2 months ago

0.2.8-canary.1

3 months ago

0.2.8-canary.0

3 months ago

0.2.7-canary.4

4 months ago

0.2.7

4 months ago

0.2.7-canary.3

4 months ago

0.2.7-canary.1

6 months ago

0.2.7-canary.0

7 months ago

0.2.7-canary.2

5 months ago

0.2.6

7 months ago

0.2.6-canary.1

8 months ago

0.2.6-canary.2

8 months ago

0.2.6-canary.3

7 months ago

0.2.6-canary.0

8 months ago

0.2.4-canary.2

8 months ago

0.2.4-canary.1

8 months ago

0.2.4-canary.0

8 months ago

0.2.1-canary.0

9 months ago

0.2.0-canary.4

9 months ago

0.2.0-canary.3

9 months ago

0.2.0-canary.6

9 months ago

0.2.0-canary.5

9 months ago

0.2.0-canary.0

9 months ago

0.2.0-canary.2

9 months ago

0.2.0-canary.1

9 months ago

0.2.5-canary.2

8 months ago

0.0.9-canary.1

10 months ago

0.2.5-canary.3

8 months ago

0.0.9-canary.0

10 months ago

0.2.5-canary.1

8 months ago

0.2.2-canary.0

9 months ago

0.1.0

10 months ago

0.2.2-canary.1

8 months ago

0.2.1

9 months ago

0.2.0

9 months ago

0.2.5-canary.4

8 months ago

0.2.3-canary.1

8 months ago

0.2.5-canary.5

8 months ago

0.2.3-canary.0

8 months ago

0.2.3

8 months ago

0.2.2

8 months ago

0.2.5

8 months ago

0.2.4

8 months ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago