@uiw/react-json-view 1.6.0 → 1.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -3,7 +3,7 @@ react-json-view
3
3
 
4
4
  [![CI](https://github.com/uiwjs/react-json-view/actions/workflows/ci.yml/badge.svg)](https://github.com/uiwjs/react-json-view/actions/workflows/ci.yml)
5
5
  [![npm version](https://img.shields.io/npm/v/@uiw/react-json-view.svg)](https://www.npmjs.com/package/@uiw/react-json-view)
6
- [![NPM Downloads](https://img.shields.io/npm/dm/@uiw/react-json-view.svg?style=flat&label=)](https://www.npmjs.com/package/@uiw/react-json-view) |
6
+ [![NPM Downloads](https://img.shields.io/npm/dm/@uiw/react-json-view.svg?style=flat&label=)](https://www.npmjs.com/package/@uiw/react-json-view)
7
7
  [![react@^18](https://shields.io/badge/react-^18-green?style=flat&logo=react)](https://github.com/facebook/react/releases)
8
8
  [![Coverage Status](https://uiwjs.github.io/react-json-view/badges.svg)](https://uiwjs.github.io/react-json-view/lcov-report/)
9
9
 
@@ -123,6 +123,7 @@ const customTheme = {
123
123
  '--w-rjv-line-color': '#323232',
124
124
  '--w-rjv-arrow-color': 'var(--w-rjv-color)',
125
125
  '--w-rjv-edit-color': 'var(--w-rjv-color)',
126
+ '--w-rjv-add-color': 'var(--w-rjv-color)',
126
127
  '--w-rjv-info-color': '#656565',
127
128
  '--w-rjv-update-color': '#ebcb8b',
128
129
  '--w-rjv-copied-color': '#9cdcfe',
@@ -187,7 +188,8 @@ const customTheme = {
187
188
  '--w-rjv-background-color': '#1e1e1e',
188
189
  '--w-rjv-line-color': '#323232',
189
190
  '--w-rjv-arrow-color': '#9cdcfe',
190
- '--w-rjv-edit-color': '#9cdcfe',
191
+ '--w-rjv-edit-color': '#0184a6',
192
+ '--w-rjv-add-color': '#0184a6',
191
193
  '--w-rjv-info-color': '#656565',
192
194
  '--w-rjv-update-color': '#ebcb8b',
193
195
  '--w-rjv-copied-color': '#0184a6',
@@ -237,7 +239,18 @@ export default function Demo() {
237
239
  <input type="checkbox" checked={editable} onChange={changeEditable} /> Editable
238
240
  </label>
239
241
  <div style={{ display: 'flex', gap: '1rem', alignItems: 'flex-start' }}>
240
- <JsonView editable={editable} value={src} keyName="root" style={{ flex: 1, overflow: 'auto', ...theme }} />
242
+ <JsonView
243
+ editable={editable}
244
+ value={src}
245
+ keyName="root"
246
+ onAdd={(keyOrValue, newValue, value, isAdd) => {
247
+ return isAdd;
248
+ }}
249
+ onEdit={(opts) => {
250
+ return true;
251
+ }}
252
+ style={{ flex: 1, overflow: 'auto', ...theme }}
253
+ />
241
254
  <div>
242
255
  <Colorful color={hex} onChange={onChange} />
243
256
  <div style={{ display: 'flex', gap: '0.4rem', flexDirection: 'column' }}>
@@ -247,7 +260,7 @@ export default function Demo() {
247
260
  setHex(customTheme[varname]);
248
261
  };
249
262
  const active = cssvar === varname ? '#a8a8a8' : '';
250
- return <button key={idx} style={{ background: active }} onClick={click}>{varname}</button>
263
+ return <button key={idx} style={{ background: active, border: 0,boxShadow: 'inset 0px 0px 1px #000' }} onClick={click}>{varname}</button>
251
264
  })}
252
265
  </div>
253
266
  </div>
@@ -316,11 +329,11 @@ const object = {
316
329
  integer: 42,
317
330
  }
318
331
 
319
- function value({ type, children, keyName, visible, ...props }) {
320
- if (type === 'string' && /\.(jpg)$/.test(children)) {
332
+ function value({ type, children, value, keyName, visible, ...props }) {
333
+ if (type === 'string' && /\.(jpg)$/.test(value)) {
321
334
  return (
322
335
  <span {...props}>
323
- <img src={children} height="36" />
336
+ <img src={value} height="36" />
324
337
  </span>
325
338
  );
326
339
  }
@@ -672,6 +685,7 @@ export default function Demo() {
672
685
  ```ts
673
686
  import React from 'react';
674
687
  import { MetaProps, SemicolonProps, EllipsisProps, ValueViewProps } from '@uiw/react-json-view';
688
+ import type { CountInfoExtraProps } from '@uiw/react-json-view/cjs/editor/countInfoExtra';
675
689
  export interface JsonViewProps<T> extends React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement> {
676
690
  /** This property contains your input JSON */
677
691
  value?: T;
@@ -710,25 +724,41 @@ export interface JsonViewProps<T> extends React.DetailedHTMLProps<React.HTMLAttr
710
724
  objectKey?: SemicolonProps['render'];
711
725
  value?: ValueViewProps<T>['renderValue'];
712
726
  copied?: CopiedProps<T>['render'];
727
+ countInfo?: (props: CountInfoProps) => JSX.Element;
728
+ countInfoExtra?: (props: Omit<CountInfoExtraProps<T>, 'editable'>) => JSX.Element;
713
729
  };
714
730
  }
715
731
  declare const JsonView: React.ForwardRefExoticComponent<Omit<JsonViewProps<object>, "ref"> & React.RefAttributes<HTMLDivElement>>;
716
732
  export default JsonView;
733
+ export interface CountInfoProps {
734
+ count: number;
735
+ level: number;
736
+ visible: boolean;
737
+ }
717
738
  ```
718
739
 
719
740
  ### JsonView Editor Props
720
741
 
721
742
  ```ts
722
743
  import { JsonViewProps } from '@uiw/react-json-view';
744
+ import type { CountInfoExtraProps } from '@uiw/react-json-view/cjs/editor/countInfoExtra';
723
745
  export interface JsonViewEditorProps<T extends object> extends JsonViewProps<T> {
724
- /** Callback when value edit functionality */
746
+ /**
747
+ * When a callback function is passed in, edit functionality is enabled. The callback is invoked before edits are completed.
748
+ * @returns {boolean} Returning false from onEdit will prevent the change from being made.
749
+ */
725
750
  onEdit?: (option: {
726
751
  value: unknown;
727
752
  oldValue: unknown;
728
753
  keyName?: string | number;
729
754
  parentName?: string | number;
730
755
  type?: 'value' | 'key';
731
- }) => void;
756
+ }) => boolean;
757
+ /**
758
+ * When a callback function is passed in, add functionality is enabled. The callback is invoked before additions are completed.
759
+ * @returns {boolean} Returning false from onAdd will prevent the change from being made.
760
+ */
761
+ onAdd?: CountInfoExtraProps<T>['onAdd'];
732
762
  /** Whether enable edit feature. @default true */
733
763
  editable?: boolean;
734
764
  }
@@ -740,19 +770,19 @@ export default JsonViewEditor;
740
770
 
741
771
  Here is the size benchmark (using [bundlephobia.com](https://bundlephobia.com)) against similar React libraries (found by [`npmjs.com/search`](https://www.npmjs.com/search?q=react%20json&ranking=popularity)):
742
772
 
743
- | Library | Bundle size | Bundle size (gzip) | Deps | Last commit | Download |
773
+ | Library | Bundle size (gzip) | Deps | Last commit | Download | Editable |
744
774
  | ------- | ------- | ------- | ------- | ------- | ------- |
745
- | **@uiw/react-json-view** | [![](https://img.shields.io/bundlephobia/min/@uiw/react-json-view?color=6ead0a&label=)](https://bundlephobia.com/result?p=@uiw/react-json-view) | [![](https://img.shields.io/bundlephobia/minzip/@uiw/react-json-view?color=6ead0a&label=)](https://bundlephobia.com/result?p=@uiw/react-json-view) | [![](https://badgen.net/bundlephobia/dependency-count/%40uiw%2Freact-json-view?color=6ead0a&label=)](https://bundlephobia.com/result?p=@uiw/react-json-view) | [![GitHub last commit](https://img.shields.io/github/last-commit/uiwjs/react-json-view?style=flat&label=)](https://github.com/uiwjs/react-json-view/commits) | [![NPM Downloads](https://img.shields.io/npm/dm/@uiw/react-json-view.svg?style=flat&label=)](https://www.npmjs.com/package/@uiw/react-json-view) |
746
- | react-json-view-lite | [![](https://img.shields.io/bundlephobia/min/react-json-view-lite?color=red&label=)](https://bundlephobia.com/result?p=react-json-view-lite) | [![](https://img.shields.io/bundlephobia/minzip/react-json-view-lite?color=red&label=)](https://bundlephobia.com/result?p=react-json-view-lite) | [![](https://badgen.net/bundlephobia/dependency-count/react-json-view-lite?color=red&label=)](https://bundlephobia.com/result?p=react-json-view-lite) | [![GitHub last commit](https://img.shields.io/github/last-commit/AnyRoad/react-json-view-lite?style=flat&label=)](https://github.com/AnyRoad/react-json-view-lite/commits) | [![NPM Downloads](https://img.shields.io/npm/dm/react-json-view-lite.svg?style=flat&color=747474&label=)](https://www.npmjs.com/package/react-json-view-lite) |
747
- | react-json-pretty | [![](https://img.shields.io/bundlephobia/min/react-json-pretty?color=red&label=)](https://bundlephobia.com/result?p=react-json-pretty) | [![](https://img.shields.io/bundlephobia/minzip/react-json-pretty?color=red&label=)](https://bundlephobia.com/result?p=react-json-pretty) | [![](https://badgen.net/bundlephobia/dependency-count/react-json-pretty?color=red&label=)](https://bundlephobia.com/result?p=react-json-pretty) | [![GitHub last commit](https://img.shields.io/github/last-commit/chenckang/react-json-pretty?style=flat&label=)](https://github.com/chenckang/react-json-pretty/commits) | [![NPM Downloads](https://img.shields.io/npm/dm/react-json-pretty.svg?style=flat&color=747474&label=)](https://www.npmjs.com/package/react-json-pretty) |
748
- | react-json-inspector | [![](https://img.shields.io/bundlephobia/min/react-json-inspector?color=red&label=)](https://bundlephobia.com/result?p=react-json-inspector) | [![](https://img.shields.io/bundlephobia/minzip/react-json-inspector?color=red&label=)](https://bundlephobia.com/result?p=react-json-inspector) | [![](https://badgen.net/bundlephobia/dependency-count/react-json-inspector?color=red&label=)](https://bundlephobia.com/result?p=react-json-inspector) | [![GitHub last commit](https://img.shields.io/github/last-commit/Lapple/react-json-inspector?style=flat&label=)](https://github.com/Lapple/react-json-inspector/commits) | [![NPM Downloads](https://img.shields.io/npm/dm/react-json-inspector.svg?style=flat&color=747474&label=)](https://www.npmjs.com/package/react-json-inspector) |
749
- | react-json-tree | [![](https://img.shields.io/bundlephobia/min/react-json-tree?color=red&label=)](https://bundlephobia.com/result?p=react-json-tree) | [![](https://img.shields.io/bundlephobia/minzip/react-json-tree?color=red&label=)](https://bundlephobia.com/result?p=react-json-tree) | [![](https://badgen.net/bundlephobia/dependency-count/react-json-tree?color=red&label=)](https://bundlephobia.com/result?p=react-json-tree) | [![GitHub last commit](https://img.shields.io/github/last-commit/reduxjs/redux-devtools?style=flat&label=)](https://github.com/reduxjs/redux-devtools/commits) | [![NPM Downloads](https://img.shields.io/npm/dm/react-json-tree.svg?style=flat&color=747474&label=)](https://www.npmjs.com/package/react-json-tree) |
750
- | react-json-view | [![](https://img.shields.io/bundlephobia/min/react-json-view?color=red&label=)](https://bundlephobia.com/result?p=react-json-view) | [![](https://img.shields.io/bundlephobia/minzip/react-json-view?color=red&label=)](https://bundlephobia.com/result?p=react-json-view) | [![](https://badgen.net/bundlephobia/dependency-count/react-json-view?color=red&label=&t=123)](https://bundlephobia.com/result?p=react-json-view) | [![GitHub last commit](https://img.shields.io/github/last-commit/mac-s-g/react-json-view?style=flat&label=)](https://github.com/mac-s-g/react-json-view/commits) | [![NPM Downloads](https://img.shields.io/npm/dm/react-json-view.svg?style=flat&color=747474&label=)](https://www.npmjs.com/package/react-json-view) |
751
- | react-json-tree-viewer | [![](https://img.shields.io/bundlephobia/min/react-json-tree-viewer?color=red&label=)](https://bundlephobia.com/result?p=react-json-tree-viewer) | [![](https://img.shields.io/bundlephobia/minzip/react-json-tree-viewer?color=red&label=)](https://bundlephobia.com/result?p=react-json-tree-viewer) | [![](https://badgen.net/bundlephobia/dependency-count/react-json-tree-viewer?color=red&label=&t=123)](https://bundlephobia.com/result?p=react-json-tree-viewer) | [![GitHub last commit](https://img.shields.io/github/last-commit/nsisodiya/react-json-viewer?style=flat&label=)](https://github.com/nsisodiya/react-json-viewer/commits) | [![NPM Downloads](https://img.shields.io/npm/dm/react-json-tree-viewer.svg?style=flat&color=747474&label=)](https://www.npmjs.com/package/react-json-tree-viewer) |
752
- | react-inspector | [![](https://img.shields.io/bundlephobia/min/react-inspector?color=red&label=)](https://bundlephobia.com/result?p=react-inspector) | [![](https://img.shields.io/bundlephobia/minzip/react-inspector?color=red&label=)](https://bundlephobia.com/result?p=react-inspector) | [![](https://badgen.net/bundlephobia/dependency-count/react-inspector?color=red&label=)](https://bundlephobia.com/result?p=react-inspector) | [![GitHub last commit](https://img.shields.io/github/last-commit/storybookjs/react-inspector?style=flat&label=)](https://github.com/storybookjs/react-inspector/commits) | [![NPM Downloads](https://img.shields.io/npm/dm/react-inspector.svg?style=flat&color=747474&label=)](https://www.npmjs.com/package/react-inspector) |
753
- | react-domify | [![](https://img.shields.io/bundlephobia/min/react-domify?color=red&label=)](https://bundlephobia.com/result?p=react-domify) | [![](https://img.shields.io/bundlephobia/minzip/react-domify?color=red&label=)](https://bundlephobia.com/result?p=react-domify) | [![](https://badgen.net/bundlephobia/dependency-count/react-domify?color=red&label=)](https://bundlephobia.com/result?p=react-domify) | [![GitHub last commit](https://img.shields.io/github/last-commit/JedWatson/react-domify?style=flat&label=)](https://github.com/JedWatson/react-domify/commits) | [![NPM Downloads](https://img.shields.io/npm/dm/react-domify.svg?style=flat&color=747474&label=)](https://www.npmjs.com/package/react-domify) |
754
- | react18-json-view | [![](https://img.shields.io/bundlephobia/min/react18-json-view?color=red&label=)](https://bundlephobia.com/result?p=react18-json-view) | [![](https://img.shields.io/bundlephobia/minzip/react18-json-view?color=red&label=)](https://bundlephobia.com/result?p=react18-json-view) | [![](https://badgen.net/bundlephobia/dependency-count/react18-json-view?color=red&label=)](https://bundlephobia.com/result?p=react18-json-view) | [![GitHub last commit](https://img.shields.io/github/last-commit/YYsuni/react18-json-view?style=flat&label=)](https://github.com/YYsuni/react18-json-view/commits) | [![NPM Downloads](https://img.shields.io/npm/dm/react18-json-view.svg?style=flat&color=747474&label=)](https://www.npmjs.com/package/react18-json-view) |
755
- | @textea/json-viewer | [![](https://img.shields.io/bundlephobia/min/@textea/json-viewer?color=red&label=)](https://bundlephobia.com/result?p=@textea/json-viewer) | [![](https://img.shields.io/bundlephobia/minzip/@textea/json-viewer?color=red&label=)](https://bundlephobia.com/result?p=@textea/json-viewer) | [![](https://badgen.net/bundlephobia/dependency-count/%40textea%2Fjson-viewer?color=red&label=&t=vvv12213)](https://bundlephobia.com/result?p=@textea/json-viewer) | [![GitHub last commit](https://img.shields.io/github/last-commit/TexteaInc/json-viewer?style=flat&label=)](https://github.com/TexteaInc/json-viewer/commits) | [![NPM Downloads](https://img.shields.io/npm/dm/@textea/json-viewer.svg?style=flat&color=747474&label=)](https://www.npmjs.com/package/@textea/json-viewer) |
775
+ | **@uiw/react-json-view** | [![](https://img.shields.io/bundlephobia/min/@uiw/react-json-view?color=6ead0a&label=)](https://bundlephobia.com/result?p=@uiw/react-json-view) [![](https://img.shields.io/bundlephobia/minzip/@uiw/react-json-view?color=6ead0a&label=gzip)](https://bundlephobia.com/result?p=@uiw/react-json-view) | [![](https://badgen.net/bundlephobia/dependency-count/%40uiw%2Freact-json-view?color=6ead0a&label=)](https://bundlephobia.com/result?p=@uiw/react-json-view) | [![GitHub last commit](https://img.shields.io/github/last-commit/uiwjs/react-json-view?style=flat&label=)](https://github.com/uiwjs/react-json-view/commits) | [![NPM Downloads](https://img.shields.io/npm/dm/@uiw/react-json-view.svg?style=flat&label=)](https://www.npmjs.com/package/@uiw/react-json-view) | ✅ |
776
+ | react-json-view-lite | [![](https://img.shields.io/bundlephobia/min/react-json-view-lite?color=red&label=)](https://bundlephobia.com/result?p=react-json-view-lite) [![](https://img.shields.io/bundlephobia/minzip/react-json-view-lite?color=red&label=gzip)](https://bundlephobia.com/result?p=react-json-view-lite) | [![](https://badgen.net/bundlephobia/dependency-count/react-json-view-lite?color=red&label=)](https://bundlephobia.com/result?p=react-json-view-lite) | [![GitHub last commit](https://img.shields.io/github/last-commit/AnyRoad/react-json-view-lite?style=flat&label=)](https://github.com/AnyRoad/react-json-view-lite/commits) | [![NPM Downloads](https://img.shields.io/npm/dm/react-json-view-lite.svg?style=flat&color=747474&label=)](https://www.npmjs.com/package/react-json-view-lite) | ❌ |
777
+ | react-json-pretty | [![](https://img.shields.io/bundlephobia/min/react-json-pretty?color=red&label=)](https://bundlephobia.com/result?p=react-json-pretty) [![](https://img.shields.io/bundlephobia/minzip/react-json-pretty?color=red&label=gzip)](https://bundlephobia.com/result?p=react-json-pretty) | [![](https://badgen.net/bundlephobia/dependency-count/react-json-pretty?color=red&label=)](https://bundlephobia.com/result?p=react-json-pretty) | [![GitHub last commit](https://img.shields.io/github/last-commit/chenckang/react-json-pretty?style=flat&label=)](https://github.com/chenckang/react-json-pretty/commits) | [![NPM Downloads](https://img.shields.io/npm/dm/react-json-pretty.svg?style=flat&color=747474&label=)](https://www.npmjs.com/package/react-json-pretty) | ❌ |
778
+ | react-json-inspector | [![](https://img.shields.io/bundlephobia/min/react-json-inspector?color=red&label=)](https://bundlephobia.com/result?p=react-json-inspector) [![](https://img.shields.io/bundlephobia/minzip/react-json-inspector?color=red&label=gzip)](https://bundlephobia.com/result?p=react-json-inspector) | [![](https://badgen.net/bundlephobia/dependency-count/react-json-inspector?color=red&label=)](https://bundlephobia.com/result?p=react-json-inspector) | [![GitHub last commit](https://img.shields.io/github/last-commit/Lapple/react-json-inspector?style=flat&label=)](https://github.com/Lapple/react-json-inspector/commits) | [![NPM Downloads](https://img.shields.io/npm/dm/react-json-inspector.svg?style=flat&color=747474&label=)](https://www.npmjs.com/package/react-json-inspector) | ❌ |
779
+ | react-json-tree | [![](https://img.shields.io/bundlephobia/min/react-json-tree?color=red&label=)](https://bundlephobia.com/result?p=react-json-tree) [![](https://img.shields.io/bundlephobia/minzip/react-json-tree?color=red&label=gzip)](https://bundlephobia.com/result?p=react-json-tree) | [![](https://badgen.net/bundlephobia/dependency-count/react-json-tree?color=red&label=)](https://bundlephobia.com/result?p=react-json-tree) | [![GitHub last commit](https://img.shields.io/github/last-commit/reduxjs/redux-devtools?style=flat&label=)](https://github.com/reduxjs/redux-devtools/commits) | [![NPM Downloads](https://img.shields.io/npm/dm/react-json-tree.svg?style=flat&color=747474&label=)](https://www.npmjs.com/package/react-json-tree) | ❌ |
780
+ | ~~react-json-view~~ | [![](https://img.shields.io/bundlephobia/min/react-json-view?color=red&label=)](https://bundlephobia.com/result?p=react-json-view) [![](https://img.shields.io/bundlephobia/minzip/react-json-view?color=red&label=gzip)](https://bundlephobia.com/result?p=react-json-view) | [![](https://badgen.net/bundlephobia/dependency-count/react-json-view?color=red&label=&t=123)](https://bundlephobia.com/result?p=react-json-view) | [![GitHub last commit](https://img.shields.io/github/last-commit/mac-s-g/react-json-view?style=flat&label=)](https://github.com/mac-s-g/react-json-view/commits) | [![NPM Downloads](https://img.shields.io/npm/dm/react-json-view.svg?style=flat&color=747474&label=)](https://www.npmjs.com/package/react-json-view) | ✅ |
781
+ | ~~react-json-tree-viewer~~ | [![](https://img.shields.io/bundlephobia/min/react-json-tree-viewer?color=red&label=)](https://bundlephobia.com/result?p=react-json-tree-viewer) [![](https://img.shields.io/bundlephobia/minzip/react-json-tree-viewer?color=red&label=gzip)](https://bundlephobia.com/result?p=react-json-tree-viewer) | [![](https://badgen.net/bundlephobia/dependency-count/react-json-tree-viewer?color=red&label=&t=123)](https://bundlephobia.com/result?p=react-json-tree-viewer) | [![GitHub last commit](https://img.shields.io/github/last-commit/nsisodiya/react-json-viewer?style=flat&label=)](https://github.com/nsisodiya/react-json-viewer/commits) | [![NPM Downloads](https://img.shields.io/npm/dm/react-json-tree-viewer.svg?style=flat&color=747474&label=)](https://www.npmjs.com/package/react-json-tree-viewer) | ❌ |
782
+ | react-inspector | [![](https://img.shields.io/bundlephobia/min/react-inspector?color=red&label=)](https://bundlephobia.com/result?p=react-inspector) [![](https://img.shields.io/bundlephobia/minzip/react-inspector?color=red&label=gzip)](https://bundlephobia.com/result?p=react-inspector) | [![](https://badgen.net/bundlephobia/dependency-count/react-inspector?color=red&label=)](https://bundlephobia.com/result?p=react-inspector) | [![GitHub last commit](https://img.shields.io/github/last-commit/storybookjs/react-inspector?style=flat&label=)](https://github.com/storybookjs/react-inspector/commits) | [![NPM Downloads](https://img.shields.io/npm/dm/react-inspector.svg?style=flat&color=747474&label=)](https://www.npmjs.com/package/react-inspector) | ❌ |
783
+ | react-domify | [![](https://img.shields.io/bundlephobia/min/react-domify?color=red&label=)](https://bundlephobia.com/result?p=react-domify) [![](https://img.shields.io/bundlephobia/minzip/react-domify?color=red&label=gzip)](https://bundlephobia.com/result?p=react-domify) | [![](https://badgen.net/bundlephobia/dependency-count/react-domify?color=red&label=)](https://bundlephobia.com/result?p=react-domify) | [![GitHub last commit](https://img.shields.io/github/last-commit/JedWatson/react-domify?style=flat&label=)](https://github.com/JedWatson/react-domify/commits) | [![NPM Downloads](https://img.shields.io/npm/dm/react-domify.svg?style=flat&color=747474&label=)](https://www.npmjs.com/package/react-domify) | ❌ |
784
+ | react18-json-view | [![](https://img.shields.io/bundlephobia/min/react18-json-view?color=red&label=)](https://bundlephobia.com/result?p=react18-json-view) [![](https://img.shields.io/bundlephobia/minzip/react18-json-view?color=red&label=gzip)](https://bundlephobia.com/result?p=react18-json-view) | [![](https://badgen.net/bundlephobia/dependency-count/react18-json-view?color=red&label=)](https://bundlephobia.com/result?p=react18-json-view) | [![GitHub last commit](https://img.shields.io/github/last-commit/YYsuni/react18-json-view?style=flat&label=)](https://github.com/YYsuni/react18-json-view/commits) | [![NPM Downloads](https://img.shields.io/npm/dm/react18-json-view.svg?style=flat&color=747474&label=)](https://www.npmjs.com/package/react18-json-view) | ❌ |
785
+ | @textea/json-viewer | [![](https://img.shields.io/bundlephobia/min/@textea/json-viewer?color=red&label=)](https://bundlephobia.com/result?p=@textea/json-viewer) [![](https://img.shields.io/bundlephobia/minzip/@textea/json-viewer?color=red&label=gzip)](https://bundlephobia.com/result?p=@textea/json-viewer) | [![](https://badgen.net/bundlephobia/dependency-count/%40textea%2Fjson-viewer?color=red&label=&t=vvv12213)](https://bundlephobia.com/result?p=@textea/json-viewer) | [![GitHub last commit](https://img.shields.io/github/last-commit/TexteaInc/json-viewer?style=flat&label=)](https://github.com/TexteaInc/json-viewer/commits) | [![NPM Downloads](https://img.shields.io/npm/dm/@textea/json-viewer.svg?style=flat&color=747474&label=)](https://www.npmjs.com/package/@textea/json-viewer) | ✅ |
756
786
 
757
787
  ## Development
758
788
 
@@ -0,0 +1,14 @@
1
+ /// <reference types="react" />
2
+ import type { CountInfoProps } from '../';
3
+ export interface CountInfoExtraProps<T> extends Partial<CountInfoProps> {
4
+ editable: boolean;
5
+ showTools: boolean;
6
+ value: T;
7
+ setValue?: React.Dispatch<React.SetStateAction<T>>;
8
+ /**
9
+ * When a callback function is passed in, add functionality is enabled. The callback is invoked before additions are completed.
10
+ * @returns {boolean} Returning false from onAdd will prevent the change from being made.
11
+ */
12
+ onAdd?: (keyOrValue: string, newValue: T, value: T, isAdd: boolean) => boolean;
13
+ }
14
+ export declare function CountInfoExtra<T extends object>(props: CountInfoExtraProps<T>): import("react/jsx-runtime").JSX.Element | null | undefined;
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.CountInfoExtra = CountInfoExtra;
8
+ var _regeneratorRuntime2 = _interopRequireDefault(require("@babel/runtime/helpers/regeneratorRuntime"));
9
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
10
+ var _objectSpread3 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
11
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
12
+ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
13
+ var _add = require("./icon/add");
14
+ var _jsxRuntime = require("react/jsx-runtime");
15
+ function CountInfoExtra(props) {
16
+ var visible = props.visible,
17
+ showTools = props.showTools,
18
+ editable = props.editable,
19
+ value = props.value,
20
+ setValue = props.setValue,
21
+ onAdd = props.onAdd;
22
+ if (!visible || !showTools) return null;
23
+ var click = /*#__PURE__*/function () {
24
+ var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/(0, _regeneratorRuntime2["default"])().mark(function _callee(event) {
25
+ var keyOrValue, isArray, isAdd, result, maybeAdd;
26
+ return (0, _regeneratorRuntime2["default"])().wrap(function _callee$(_context) {
27
+ while (1) switch (_context.prev = _context.next) {
28
+ case 0:
29
+ event.stopPropagation();
30
+ keyOrValue = 'AddKeyOrValue';
31
+ isArray = Array.isArray(value);
32
+ isAdd = isArray ? true : !(keyOrValue in value);
33
+ result = isArray ? [].concat((0, _toConsumableArray2["default"])(value), [keyOrValue]) : (0, _objectSpread3["default"])((0, _objectSpread3["default"])({}, value), {}, (0, _defineProperty2["default"])({}, keyOrValue, undefined));
34
+ if (!onAdd) {
35
+ _context.next = 10;
36
+ break;
37
+ }
38
+ _context.next = 8;
39
+ return onAdd(keyOrValue, result, props.value, isAdd);
40
+ case 8:
41
+ maybeAdd = _context.sent;
42
+ if (maybeAdd) {
43
+ setValue(result);
44
+ }
45
+ case 10:
46
+ case "end":
47
+ return _context.stop();
48
+ }
49
+ }, _callee);
50
+ }));
51
+ return function click(_x) {
52
+ return _ref.apply(this, arguments);
53
+ };
54
+ }();
55
+ var svgProps = {
56
+ onClick: click
57
+ };
58
+ if (!editable || !onAdd) return;
59
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(_add.AddIcon, (0, _objectSpread3["default"])({}, svgProps));
60
+ }
@@ -0,0 +1,4 @@
1
+ /// <reference types="react" />
2
+ export interface AddIconProps extends React.SVGAttributes<SVGElement> {
3
+ }
4
+ export declare const AddIcon: (props: AddIconProps) => import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.AddIcon = void 0;
8
+ var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
9
+ var _jsxRuntime = require("react/jsx-runtime");
10
+ var AddIcon = function AddIcon(props) {
11
+ var style = props.style;
12
+ var defaultStyle = {
13
+ verticalAlign: 'middle',
14
+ display: 'inline-block',
15
+ cursor: 'pointer',
16
+ marginLeft: 5,
17
+ height: '1em',
18
+ width: '1em'
19
+ };
20
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)("svg", (0, _objectSpread2["default"])((0, _objectSpread2["default"])({
21
+ viewBox: "0 0 20 20",
22
+ fill: "var(--w-rjv-add-color, currentColor)"
23
+ }, props), {}, {
24
+ style: (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, style), defaultStyle),
25
+ children: /*#__PURE__*/(0, _jsxRuntime.jsx)("path", {
26
+ d: "M14.1970498,0 L5.81288651,0 C2.17107809,0 0,2.17 0,5.81 L0,14.18 C0,17.83 2.17107809,20 5.81288651,20 L14.1870449,20 C17.8288533,20 19.9999658,17.83 19.9999658,14.19 L19.9999658,5.81 C20.0099363,2.17 17.8388583,0 14.1970498,0 Z M16.0079491,10.75 L10.7553408,10.75 L10.7553408,16 C10.7553408,16.41 10.4151719,16.75 10.0049682,16.75 C9.59476448,16.75 9.25459556,16.41 9.25459556,16 L9.25459556,10.75 L4.00198727,10.75 C3.59178357,10.75 3.25161466,10.41 3.25161466,10 C3.25161466,9.59 3.59178357,9.25 4.00198727,9.25 L9.25459556,9.25 L9.25459556,4 C9.25459556,3.59 9.59476448,3.25 10.0049682,3.25 C10.4151719,3.25 10.7553408,3.59 10.7553408,4 L10.7553408,9.25 L16.0079491,9.25 C16.4181528,9.25 16.7583217,9.59 16.7583217,10 C16.7583217,10.41 16.4181528,10.75 16.0079491,10.75 Z"
27
+ })
28
+ }));
29
+ };
30
+ exports.AddIcon = AddIcon;
@@ -1,14 +1,23 @@
1
1
  /// <reference types="react" />
2
- import { JsonViewProps } from '../';
2
+ import type { JsonViewProps } from '../';
3
+ import type { CountInfoExtraProps } from './countInfoExtra';
3
4
  export interface JsonViewEditorProps<T extends object> extends JsonViewProps<T> {
4
- /** Callback when value edit functionality */
5
+ /**
6
+ * When a callback function is passed in, edit functionality is enabled. The callback is invoked before edits are completed.
7
+ * @returns {boolean} Returning false from onEdit will prevent the change from being made.
8
+ */
5
9
  onEdit?: (option: {
6
10
  value: unknown;
7
11
  oldValue: unknown;
8
12
  keyName?: string | number;
9
13
  parentName?: string | number;
10
14
  type?: 'value' | 'key';
11
- }) => void;
15
+ }) => boolean;
16
+ /**
17
+ * When a callback function is passed in, add functionality is enabled. The callback is invoked before additions are completed.
18
+ * @returns {boolean} Returning false from onAdd will prevent the change from being made.
19
+ */
20
+ onAdd?: CountInfoExtraProps<T>['onAdd'];
12
21
  /** Whether enable edit feature. @default true */
13
22
  editable?: boolean;
14
23
  }
@@ -11,8 +11,9 @@ var _react = require("react");
11
11
  var _ = _interopRequireDefault(require("../"));
12
12
  var _objectKey = require("./objectKey");
13
13
  var _value = require("./value");
14
+ var _countInfoExtra = require("./countInfoExtra");
14
15
  var _jsxRuntime = require("react/jsx-runtime");
15
- var _excluded = ["onEdit", "components", "editable", "displayDataTypes"];
16
+ var _excluded = ["onEdit", "components", "editable", "displayDataTypes", "onAdd"];
16
17
  var JsonViewEditor = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
17
18
  var onEdit = props.onEdit,
18
19
  components = props.components,
@@ -20,8 +21,15 @@ var JsonViewEditor = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
20
21
  editable = _props$editable === void 0 ? true : _props$editable,
21
22
  _props$displayDataTyp = props.displayDataTypes,
22
23
  displayDataTypes = _props$displayDataTyp === void 0 ? true : _props$displayDataTyp,
24
+ onAdd = props.onAdd,
23
25
  reset = (0, _objectWithoutProperties2["default"])(props, _excluded);
24
26
  var comps = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, components), {}, {
27
+ countInfoExtra: function countInfoExtra(reprops) {
28
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(_countInfoExtra.CountInfoExtra, (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, reprops), {}, {
29
+ editable: editable,
30
+ onAdd: onAdd
31
+ }));
32
+ },
25
33
  objectKey: function objectKey(reprops) {
26
34
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_objectKey.ObjectKey, (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, reprops), {}, {
27
35
  editableValue: editable,
@@ -1,6 +1,6 @@
1
1
  import { FC } from 'react';
2
- import { SemicolonProps } from '../semicolon';
3
- import { JsonViewEditorProps } from './';
2
+ import type { SemicolonProps } from '../semicolon';
3
+ import type { JsonViewEditorProps } from './';
4
4
  export interface ObjectKeyProps<T extends object> extends SemicolonProps {
5
5
  onEdit?: JsonViewEditorProps<T>['onEdit'];
6
6
  render?: (props: SemicolonProps & {
@@ -1,6 +1,6 @@
1
1
  /// <reference types="react" />
2
2
  import type { TypeProps } from '../value';
3
- import { JsonViewEditorProps } from './';
3
+ import type { JsonViewEditorProps } from './';
4
4
  export interface ReValueProps<T extends object> extends React.HTMLAttributes<HTMLSpanElement> {
5
5
  onEdit?: JsonViewEditorProps<T>['onEdit'];
6
6
  quotes?: JsonViewEditorProps<T>['quotes'];
@@ -6,6 +6,8 @@ Object.defineProperty(exports, "__esModule", {
6
6
  });
7
7
  exports.ReValue = ReValue;
8
8
  var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
9
+ var _regeneratorRuntime2 = _interopRequireDefault(require("@babel/runtime/helpers/regeneratorRuntime"));
10
+ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
9
11
  var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
10
12
  var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
11
13
  var _react = require("react");
@@ -71,60 +73,96 @@ function ReValue(props) {
71
73
  $edit.current.contentEditable = 'false';
72
74
  }
73
75
  };
74
- var blur = function blur() {
75
- if (!editableValue) return;
76
- setEditable(false);
77
- if ($edit.current) {
78
- $edit.current.contentEditable = 'false';
79
- var text = $edit.current.innerText;
80
- if (curentType === 'number' || curentType === 'float') {
81
- text = Number(text);
82
- setCurentType((0, _value.isFloat)(text) ? 'float' : 'number');
83
- }
84
- if (curentType === 'url' && typeof text === 'string') {
85
- text = new URL(text);
86
- }
87
- if (Number.isNaN(text)) {
88
- setCurentType('number');
89
- }
90
- if (typeof text === 'string' && /^(true|false)$/ig.test(text)) {
91
- text = /^(true)$/ig.test(text) ? true : false;
92
- setCurentType('boolean');
93
- } else if (typeof text === 'string' && /^[\d]+n$/ig.test(text)) {
94
- text = BigInt(text.replace(/n$/ig, ''));
95
- setCurentType('bigint');
96
- } else if (typeof text === 'string' && /^(null)$/ig.test(text)) {
97
- text = null;
98
- setCurentType('null');
99
- } else if (typeof text === 'string' && /^(undefined)$/ig.test(text)) {
100
- text = undefined;
101
- setCurentType('undefined');
102
- } else if (typeof text === 'string') {
103
- try {
104
- var dt = new Date(text);
105
- if (dt.toString() !== 'Invalid Date') {
106
- text = dt;
107
- setCurentType('date');
108
- }
109
- } catch (error) {}
110
- }
111
- setCurentChild(text);
112
- onEdit && onEdit({
113
- type: 'value',
114
- value: text,
115
- oldValue: curentChild
116
- });
117
- }
118
- };
76
+ var blur = /*#__PURE__*/function () {
77
+ var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/(0, _regeneratorRuntime2["default"])().mark(function _callee() {
78
+ var text, _typeStr, dt, result, _getValueString, oldChildStr;
79
+ return (0, _regeneratorRuntime2["default"])().wrap(function _callee$(_context) {
80
+ while (1) switch (_context.prev = _context.next) {
81
+ case 0:
82
+ if (editableValue) {
83
+ _context.next = 2;
84
+ break;
85
+ }
86
+ return _context.abrupt("return");
87
+ case 2:
88
+ setEditable(false);
89
+ if (!$edit.current) {
90
+ _context.next = 16;
91
+ break;
92
+ }
93
+ $edit.current.contentEditable = 'false';
94
+ text = $edit.current.innerText;
95
+ _typeStr = curentType;
96
+ if (curentType === 'number' || curentType === 'float') {
97
+ text = Number(text);
98
+ _typeStr = (0, _value.isFloat)(text) ? 'float' : 'number';
99
+ }
100
+ if (curentType === 'url' && typeof text === 'string') {
101
+ text = new URL(text);
102
+ _typeStr = 'url';
103
+ }
104
+ if (Number.isNaN(text)) {
105
+ _typeStr = 'number';
106
+ }
107
+ if (typeof text === 'string' && /^(true|false)$/ig.test(text)) {
108
+ text = /^(true)$/ig.test(text) ? true : false;
109
+ _typeStr = 'boolean';
110
+ } else if (typeof text === 'string' && /^[\d]+n$/ig.test(text)) {
111
+ text = BigInt(text.replace(/n$/ig, ''));
112
+ _typeStr = 'bigint';
113
+ } else if (typeof text === 'string' && /^(null)$/ig.test(text)) {
114
+ text = null;
115
+ _typeStr = 'null';
116
+ } else if (typeof text === 'string' && /^(undefined)$/ig.test(text)) {
117
+ text = undefined;
118
+ _typeStr = 'undefined';
119
+ } else if (typeof text === 'string') {
120
+ try {
121
+ if (text && text.length > 10 && !isNaN(Date.parse(text))) {
122
+ dt = new Date(text);
123
+ text = dt;
124
+ _typeStr = 'date';
125
+ }
126
+ } catch (error) {}
127
+ }
128
+ if (!onEdit) {
129
+ _context.next = 16;
130
+ break;
131
+ }
132
+ _context.next = 14;
133
+ return onEdit({
134
+ type: 'value',
135
+ value: text,
136
+ oldValue: curentChild
137
+ });
138
+ case 14:
139
+ result = _context.sent;
140
+ if (result) {
141
+ setCurentType(_typeStr);
142
+ setCurentChild(text);
143
+ } else {
144
+ _getValueString = (0, _value.getValueString)(curentChild), oldChildStr = _getValueString.content;
145
+ $edit.current.innerHTML = String(oldChildStr);
146
+ }
147
+ case 16:
148
+ case "end":
149
+ return _context.stop();
150
+ }
151
+ }, _callee);
152
+ }));
153
+ return function blur() {
154
+ return _ref2.apply(this, arguments);
155
+ };
156
+ }();
119
157
  var defaultStyle = {
120
158
  minWidth: 34,
121
159
  minHeight: 18,
122
160
  paddingInline: 3,
123
161
  display: 'inline-block'
124
162
  };
125
- var _getValueString = (0, _value.getValueString)(curentChild),
126
- typeStr = _getValueString.type,
127
- childStr = _getValueString.content;
163
+ var _getValueString2 = (0, _value.getValueString)(curentChild),
164
+ typeStr = _getValueString2.type,
165
+ childStr = _getValueString2.content;
128
166
  var color = ((_typeMap$typeStr = _value.typeMap[typeStr]) === null || _typeMap$typeStr === void 0 ? void 0 : _typeMap$typeStr.color) || '';
129
167
  var spanProps = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, reset), {}, {
130
168
  onBlur: blur,
@@ -143,21 +181,19 @@ function ReValue(props) {
143
181
  typeView = /*#__PURE__*/(0, _jsxRuntime.jsx)(_react.Fragment, {});
144
182
  }
145
183
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_react.Fragment, {
146
- children: [displayDataTypes && typeView, /*#__PURE__*/(0, _jsxRuntime.jsxs)(_react.Fragment, {
147
- children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(Quotes, {
148
- style: style,
149
- quotes: quotes,
150
- show: typeStr === 'string'
151
- }), /*#__PURE__*/(0, _jsxRuntime.jsx)("span", (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, spanProps), {}, {
152
- ref: $edit,
153
- "data-value": childStr,
154
- children: typeof curentChild === 'string' ? curentChild : childStr
155
- })), /*#__PURE__*/(0, _jsxRuntime.jsx)(Quotes, {
156
- style: style,
157
- quotes: quotes,
158
- show: typeStr === 'string'
159
- })]
160
- }), visible && editableValue && /*#__PURE__*/(0, _jsxRuntime.jsx)(_edit.EditIcon, {
184
+ children: [displayDataTypes && typeView, /*#__PURE__*/(0, _jsxRuntime.jsx)(Quotes, {
185
+ style: style,
186
+ quotes: quotes,
187
+ show: typeStr === 'string'
188
+ }), /*#__PURE__*/(0, _jsxRuntime.jsx)("span", (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, spanProps), {}, {
189
+ ref: $edit,
190
+ "data-value": childStr,
191
+ children: typeof curentChild === 'string' ? curentChild : childStr
192
+ })), /*#__PURE__*/(0, _jsxRuntime.jsx)(Quotes, {
193
+ style: style,
194
+ quotes: quotes,
195
+ show: typeStr === 'string'
196
+ }), visible && editableValue && onEdit && /*#__PURE__*/(0, _jsxRuntime.jsx)(_edit.EditIcon, {
161
197
  onClick: click
162
198
  })]
163
199
  });
package/cjs/index.d.ts CHANGED
@@ -1,11 +1,17 @@
1
1
  import React from 'react';
2
- import { SemicolonProps } from './semicolon';
3
- import { ValueViewProps } from './value';
4
- import { CopiedProps } from './copied';
5
- import { EllipsisProps } from './comps/ellipsis';
6
- import { MetaProps } from './comps/meta';
2
+ import type { SemicolonProps } from './semicolon';
3
+ import type { ValueViewProps } from './value';
4
+ import type { CopiedProps } from './copied';
5
+ import type { EllipsisProps } from './comps/ellipsis';
6
+ import type { MetaProps } from './comps/meta';
7
+ import type { CountInfoExtraProps } from './editor/countInfoExtra';
7
8
  export * from './node';
8
9
  export * from './value';
10
+ export interface CountInfoProps {
11
+ count: number;
12
+ level: number;
13
+ visible: boolean;
14
+ }
9
15
  export interface JsonViewProps<T extends object> extends React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement> {
10
16
  /** This property contains your input JSON */
11
17
  value?: T;
@@ -44,11 +50,8 @@ export interface JsonViewProps<T extends object> extends React.DetailedHTMLProps
44
50
  objectKey?: SemicolonProps['render'];
45
51
  value?: ValueViewProps<T>['renderValue'];
46
52
  copied?: CopiedProps<T>['render'];
47
- countInfo?: (props: {
48
- count: number;
49
- level: number;
50
- visible: boolean;
51
- }) => JSX.Element;
53
+ countInfo?: (props: CountInfoProps) => JSX.Element;
54
+ countInfoExtra?: (props: Omit<CountInfoExtraProps<T>, 'editable'>) => JSX.Element;
52
55
  };
53
56
  }
54
57
  declare const JsonView: React.ForwardRefExoticComponent<Omit<JsonViewProps<object>, "ref"> & React.RefAttributes<HTMLDivElement>>;