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
Copy file name to clipboardExpand all lines: README.md
+30-2
Original file line number
Diff line number
Diff line change
@@ -22,9 +22,10 @@ Features include:
22
22
-[Usage](#usage)
23
23
-[Props overview](#props-overview)
24
24
-[Update functions](#update-functions)
25
+
-[OnChange function](#onchange-function)
25
26
-[Copy function](#copy-function)
26
27
-[Filter functions](#filter-functions)
27
-
-[Examples](#examples)
28
+
-[Examples](#examples-1)
28
29
-[Search/Filtering](#searchfiltering)
29
30
-[Themes \& Styles](#themes--styles)
30
31
-[Fragments](#fragments)
@@ -94,6 +95,7 @@ The only *required* value is `data`.
94
95
|`onEdit`|`UpdateFunction`|| A function to run whenever a value is **edited**. |
95
96
|`onDelete`|`UpdateFunction`|| A function to run whenever a value is **deleted**. |
96
97
|`onAdd`|`UpdateFunction`|| A function to run whenever a new property is **added**. |
98
+
|`onChange`|`OnChangeFunction`|| A function to modify/constrain user input as they type -- see [OnChange functions](#onchange-function). |
97
99
|`enableClipboard`|`boolean\|CopyFunction`|`true`| Whether or not to enable the "Copy to clipboard" button in the UI. If a function is provided, `true` is assumed and this function will be run whenever an item is copied. |
98
100
|`indent`|`number`|`3`| Specify the amount of indentation for each level of nesting in the displayed data. |
99
101
|`collapse`|`boolean\|number\|FilterFunction`|`false`| Defines which nodes of the JSON tree will be displayed "opened" in the UI on load. If `boolean`, it'll be either all or none. A `number` specifies a nesting depth after which nodes will be closed. For more fine control a function can be provided — see [Filter functions](#filter-functions). |
@@ -140,6 +142,32 @@ The function will receive the following object as a parameter:
140
142
141
143
The function needn't return anything, but if it returns `false`, it will be considered an error, in which case an error message will displayed in the UI and the internal data state won't actually be updated. If the return value is a `string`, this will be the error message displayed (i.e. you can define your own error messages for updates). On error, the displayed data will revert to its previous value.
142
144
145
+
### OnChange function
146
+
147
+
Similar to the Update functions, the `onChange` function is executed as the user input changes. You can use this to restrict or constrain user input -- e.g. limiting numbers to positive values, or preventing line breaks in strings. The function *must* return a value in order to update the user input field, so if no changes are to made, just return it unmodified.
148
+
149
+
The input object is similar to the Update function input, but with no `newData` field (since this operation occurs before the data is updated).
150
+
151
+
#### Examples
152
+
153
+
- Restrict "age" inputs to positive values up to 100:
154
+
```js
155
+
// in <JsonEditor /> props
156
+
onChange= ({ newValue, name }) => {
157
+
if (name ==="age"&& newValue <0) return0;
158
+
if (name ==="age"&& newValue >100) return100;
159
+
return newValue
160
+
}
161
+
```
162
+
- Only allow alphabetical or whitespace input for "name" field (including no line breaks):
A similar callback is executed whenever an item is copied to the clipboard (if passed to the `enableClipboard` prop), but with a different input parameter:
@@ -531,7 +559,7 @@ A few helper functions, components and types that might be useful in your own im
531
559
- `Theme`: a full [Theme](#themes--styles) object
532
560
- `ThemeInput`: input type for the `theme` prop
533
561
- `JsonEditorProps`: all input props for the Json Editor component
0 commit comments