You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> -**Version 1.19.0** has a change to the `theme` input. Built-in themes must now
32
-
> be imported separately and passed in, rather than just naming the theme as a
33
-
> string. This is better for tree-shaking, so unused themes won't be bundled
34
-
> with your build. See [Themes & Styles](#themes--styles)
35
-
> -**Version 1.14.0** has a change which recommends you provide a `setData` prop
36
-
> and not use `onUpdate` for updating your data externally. See [Managing
37
-
> state](#managing-state).
32
+
> -**Version 1.19.0** has a change to the `theme` input. Built-in themes must now be imported separately and passed in, rather than just naming the theme as a string. This is better for tree-shaking, so unused themes won't be bundled with your build. See [Themes & Styles](#themes--styles).
33
+
> -**Version 1.14.0** has a change which recommends you provide a `setData` prop and not use `onUpdate` for updating your data externally. See [Managing state](#managing-state).
38
34
39
35
## Contents <!-- omit in toc -->
40
36
-[Installation](#installation)
@@ -46,6 +42,7 @@ A [React](https://github.com/facebook/react) component for editing or viewing JS
@@ -195,7 +195,16 @@ The only *required* property is `data` (although you will need to provide a `set
195
195
|`TextEditor`|`ReactComponent<TextEditorProps>`|| Pass a component to offer a custom text/code editor when editing full JSON object as text. [See details](#full-object-editing)|
196
196
|`jsonParse`|`(input: string) => JsonData`|`JSON.parse`| When editing a block of JSON directly, you may wish to allow some "looser" input -- e.g. 'single quotes', trailing commas, or unquoted field names. In this case, you can provide a third-party JSON parsing method. I recommend [JSON5](https://json5.org/), which is what is used in the [Demo](https://carlosnz.github.io/json-edit-react/)|
197
197
|`jsonStringify`|`(data: JsonData) => string`|`(data) => JSON.stringify(data, null, 2)`| Similarly, you can override the default presentation of the JSON string when starting editing JSON. You can supply different formatting parameters to the native `JSON.stringify()`, or provide a third-party option, like the aforementioned JSON5. |
198
-
|`keyboardControls`|`KeyboardControls`| As explained [above](#usage)| Override some or all of the keyboard controls. See [Keyboard customisation](#keyboard-customisation) for details. ||
198
+
|`keyboardControls`|`KeyboardControls`| As explained [above](#usage)| Override some or all of the keyboard controls. See [Keyboard customisation](#keyboard-customisation) for details. |
|`onEditEvent`|`OnEditEventFunction`| none | Callback to execute whenever the user starts or stops editing a node |
206
+
|`onCollapse`|`OnCollapseFunction`| none | Callback to execute whenever the user collapses or opens a node |
207
+
|`externalTriggers`|`ExternalTriggers`| none | Specify a node to collapse/open, or to start/stop editing ||
199
208
200
209
### Miscellaneous
201
210
@@ -798,7 +807,71 @@ If (for example), you just wish to change the general "confirmation" action to "
798
807
- If multiple modifiers are specified (in an array), *any* of them will be accepted (multi-modifier commands not currently supported)
799
808
- You only need to specify values for `stringConfirm`, `numberConfirm`, and `booleanConfirm` if they should *differ* from your `confirm` value.
800
809
- You won't be able to override system or browser behaviours: for example, on Mac "Ctrl-click" will perform a right-click, so using it as a click modifier won't work (hence we also accept "Meta"/"Cmd" as the default `clipboardModifier`).
801
-
810
+
811
+
## External control
812
+
813
+
You can interact with the component externally, with event callbacks and triggers to set/get the collapse or editing state of any node.
814
+
815
+
### Event callbacks
816
+
817
+
Pass in a function to the props `onEditEvent` and `onCollapse` if you want your app to be able to respond to these events.
818
+
819
+
The `onEditEvent` callback is executed whenever the user starts or stops editing a node, and has the following signature:
The `path` will be an array representing the path components when starting to edit, and `null` when ending the edit. The `isKey` indicates whether the edit is for the property `key` rather than `value`.
827
+
828
+
The `onCollapse` callback is executed when user opens or collapses a node, and has the following signature:
includeChildren: boolean // if was clicked with Modifier key to
836
+
// open/close all descendants as well
837
+
}
838
+
) => void
839
+
```
840
+
841
+
### Event triggers
842
+
843
+
You can *trigger* collapse and editing actions by changing the the `externalTriggers` prop.
844
+
845
+
The shape of the `externalTriggers` object is:
846
+
847
+
```ts
848
+
interface ExternalTriggers {
849
+
collapse?: CollapseState | CollapseState[]
850
+
edit?: EditState
851
+
}
852
+
853
+
// CollapseState same as `onCollapseFunction` (above) input
854
+
interface CollapseState {
855
+
path: CollectionKey[]
856
+
collapsed: boolean
857
+
includeChildren: boolean
858
+
}
859
+
860
+
interface EditState {
861
+
path?: CollectionKey[]
862
+
action?: 'accept' | 'cancel'
863
+
}
864
+
```
865
+
866
+
For the `edit` trigger, the `path` is only required when *starting* to edit, and
867
+
the `action` is only required when *stopping* the edit, to determine whether the
868
+
component should cancel or submit the current changes.
869
+
870
+
> [!WARNING]
871
+
> Ensure that your `externalTriggers` object is stable (i.e. doesn't create new instances on each render) so as to not cause unwanted triggering -- you may need to wrap it in `useMemo`.
872
+
> You should also be careful that your event callbacks and triggers don't cause an infinite loop!
873
+
874
+
802
875
## Undo functionality
803
876
804
877
Even though Undo/Redo functionality is probably desirable in most cases, this is not built in to the component, for two main reasons:
@@ -826,7 +899,7 @@ A few helper functions, components and types that might be useful in your own im
826
899
- `ThemeInput`: input type for the `theme` prop
827
900
- `JsonEditorProps`: all input props for the Json Editor component
828
901
- `JsonData`: main `data` object -- any valid JSON structure
0 commit comments