Skip to content

Commit 3945b0d

Browse files
committed
Allow onUpdate functions to return true to represent success
1 parent 3eeeedc commit 3945b0d

File tree

4 files changed

+7
-8
lines changed

4 files changed

+7
-8
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,8 @@ The function will receive the following object as a parameter:
165165
// (e.g. [ "user", "friends", 1, "name" ] ) (equivalent to "user.friends[1].name")
166166
}
167167
```
168-
169-
The function needn't return anything, in which case the data is updated normally. However, you can return either an Error value, or a modified data value, which will be used instead of the input data. The return value can be one of the following:
170-
- `void` / `undefined`: data continues update as normal
168+
The function can return nothing (in which case the data is updated normally), or a value to represent success/failure, error value, or modified data. The return value can be one of the following, and handled accordingly:
169+
- `true` / `void` / `undefined`: data continues update as normal
171170
- `false`: considers the update to be an error, so data is not updated (reverts to previous value), and a generic error message is displayed in the UI
172171
- `string`: also considered an error, so no data update, but the UI error message will be your provided string
173172
- `[ "value", <value> ]`: tells the component to use the returned `<value>` instead of the input data. You might use this to automatically modify user input -- for example, sorting an array, or inserting a timestamp field into an object.

demo/src/demoData/dataDefinitions.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export interface DemoData {
4545
onUpdate?: (
4646
props: UpdateFunctionProps,
4747
toast: (options: unknown) => void
48-
) => void | ErrorString | false | Promise<false | ErrorString | void>
48+
) => void | ErrorString | boolean | Promise<boolean | ErrorString | void>
4949
onAdd?: UpdateFunction
5050
onEdit?: UpdateFunction
5151
onChange?: OnChangeFunction

src/JsonEditor.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ const Editor: React.FC<JsonEditorProps> = ({
103103
const handleEdit = async (updateMethod: UpdateFunction, input: UpdateFunctionProps) => {
104104
const result = await updateMethod(input)
105105

106-
if (result === undefined) {
106+
if (result === true || result === undefined) {
107107
setData(input.newData)
108108
return
109109
}
@@ -373,7 +373,7 @@ const getSearchFilter = (
373373
}
374374

375375
const isUpdateReturnTuple = (
376-
input: UpdateFunctionReturn | string | false | undefined
376+
input: UpdateFunctionReturn | string | boolean | undefined
377377
): input is UpdateFunctionReturn => {
378378
return Array.isArray(input) && input.length === 2 && ['error', 'value'].includes(input[0])
379379
}

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ export type UpdateFunction = (
9090
) =>
9191
| void
9292
| ErrorString
93-
| false
93+
| boolean
9494
| UpdateFunctionReturn
95-
| Promise<false | ErrorString | void | UpdateFunctionReturn>
95+
| Promise<boolean | ErrorString | void | UpdateFunctionReturn>
9696

9797
export type OnChangeFunction = (props: {
9898
currentData: JsonData

0 commit comments

Comments
 (0)