Skip to content

Commit fa28ea4

Browse files
authored
#208 Call onEditEvent when starting and stopping "add key" UI (#210)
* Call `onEditEvent` when adding key * Update ButtonPanels.tsx * Update README.md * Update OnEditEventFunction type signature * Fix "path" when submitting key
1 parent ff9c178 commit fa28ea4

File tree

7 files changed

+22
-3
lines changed

7 files changed

+22
-3
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1121,11 +1121,14 @@ The `onEditEvent` callback is executed whenever the user starts or stops editing
11211121
11221122
```ts
11231123
type OnEditEventFunction =
1124-
(path: CollectionKey[] | null, isKey: boolean) => void
1124+
(path: (CollectionKey | null)[] | null, isKey: boolean) => void
11251125
```
11261126
11271127
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`.
11281128
1129+
> [!NOTE]
1130+
> After clicking the "Add key" button, the `path` in the `onEditEvent` callback will end with a `null` value, indicating that the final path where this key will end up is not yet known.
1131+
11291132
The `onCollapse` callback is executed when user opens or collapses a node, and has the following signature:
11301133
11311134
```ts

demo/src/App.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ function App() {
586586
: undefined
587587
}
588588
// collapseClickZones={['property', 'header']}
589+
// onEditEvent={(...args) => console.log('onEditEvent', ...args)}
589590
// onEditEvent={(path) => {
590591
// console.log(path)
591592
// setIsEditing(path ? true : false)

src/ButtonPanels.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
type CustomButtonDefinition,
1313
type KeyboardControlsFull,
1414
JsonData,
15+
OnEditEventFunction,
1516
} from './types'
1617
import { getModifier } from './helpers'
1718

@@ -36,6 +37,7 @@ interface EditButtonProps {
3637
// eslint-disable-next-line
3738
replacer?: (this: any, key: string, value: unknown) => string
3839
) => string
40+
onEditEvent?: OnEditEventFunction
3941
showIconTooltips: boolean
4042
}
4143

@@ -53,6 +55,7 @@ export const EditButtons: React.FC<EditButtonProps> = ({
5355
editConfirmRef,
5456
getNewKeyOptions,
5557
jsonStringify,
58+
onEditEvent,
5659
showIconTooltips,
5760
}) => {
5861
const { getStyles } = useTheme()
@@ -71,6 +74,12 @@ export const EditButtons: React.FC<EditButtonProps> = ({
7174
const hasKeyOptionsList = Array.isArray(addingKeyState)
7275

7376
const updateAddingState = (active: boolean) => {
77+
// Add 'null' to the path to indicate that the actual path of where the new
78+
// key will go is not yet known.
79+
// Also, "active" matches the second "isKey" parameter here, even though it
80+
// describes a different thing.
81+
if (onEditEvent) onEditEvent(active ? [...path, null] : null, active)
82+
7483
if (!active) {
7584
setAddingKeyState(false)
7685
return

src/CollectionNode.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export const CollectionNode: React.FC<CollectionNodeProps> = (props) => {
4747
collapseAnimationTime,
4848
onMove,
4949
enableClipboard,
50+
onEditEvent,
5051
showIconTooltips,
5152
searchFilter,
5253
searchText,
@@ -433,6 +434,7 @@ export const CollectionNode: React.FC<CollectionNodeProps> = (props) => {
433434
getNewKeyOptions={getNewKeyOptions}
434435
editConfirmRef={editConfirmRef}
435436
jsonStringify={jsonStringify}
437+
onEditEvent={onEditEvent}
436438
showIconTooltips={showIconTooltips}
437439
/>
438440
)

src/JsonEditor.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const Editor: React.FC<JsonEditorProps> = ({
4545
onAdd: srcAdd = onUpdate,
4646
onChange,
4747
onError,
48+
onEditEvent,
4849
showErrorMessages = true,
4950
enableClipboard = true,
5051
indent = 2,
@@ -349,6 +350,7 @@ const Editor: React.FC<JsonEditorProps> = ({
349350
onAdd,
350351
onChange,
351352
onError,
353+
onEditEvent,
352354
showErrorMessages,
353355
onMove,
354356
showCollectionCount,

src/contexts/TreeStateProvider.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ export const TreeStateProvider = ({ children, onEditEvent, onCollapse }: TreeSta
9090
cancelOp.current()
9191
}
9292
setCurrentlyEditingElement(pathString)
93-
if (onEditEvent) onEditEvent(path, newCancelOrKey === 'key')
93+
if (onEditEvent && (Array.isArray(path) || path === null))
94+
onEditEvent(path, newCancelOrKey === 'key')
9495
cancelOp.current = typeof newCancelOrKey === 'function' ? newCancelOrKey : null
9596
}
9697

src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ export type CompareFunction = (
178178

179179
export type SortFunction = <T>(arr: T[], nodeMap: (input: T) => [string | number, unknown]) => void
180180

181-
export type OnEditEventFunction = (path: CollectionKey[] | string | null, isKey: boolean) => void
181+
export type OnEditEventFunction = (path: (CollectionKey | null)[] | null, isKey: boolean) => void
182182

183183
// Definition to externally set Collapse state -- also passed to OnCollapse
184184
// function
@@ -261,6 +261,7 @@ interface BaseNodeProps {
261261
showIconTooltips: boolean
262262
onMove: InternalMoveFunction
263263
enableClipboard: boolean | CopyFunction
264+
onEditEvent?: OnEditEventFunction
264265
restrictEditFilter: FilterFunction
265266
restrictDeleteFilter: FilterFunction
266267
restrictAddFilter: FilterFunction

0 commit comments

Comments
 (0)