Skip to content

#208 Call onEditEvent when starting and stopping "add key" UI #210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1121,11 +1121,14 @@ The `onEditEvent` callback is executed whenever the user starts or stops editing

```ts
type OnEditEventFunction =
(path: CollectionKey[] | null, isKey: boolean) => void
(path: (CollectionKey | null)[] | null, isKey: boolean) => void
```

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`.

> [!NOTE]
> 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.

The `onCollapse` callback is executed when user opens or collapses a node, and has the following signature:

```ts
Expand Down
1 change: 1 addition & 0 deletions demo/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,7 @@ function App() {
: undefined
}
// collapseClickZones={['property', 'header']}
// onEditEvent={(...args) => console.log('onEditEvent', ...args)}
// onEditEvent={(path) => {
// console.log(path)
// setIsEditing(path ? true : false)
Expand Down
9 changes: 9 additions & 0 deletions src/ButtonPanels.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
type CustomButtonDefinition,
type KeyboardControlsFull,
JsonData,
OnEditEventFunction,
} from './types'
import { getModifier } from './helpers'

Expand All @@ -36,6 +37,7 @@ interface EditButtonProps {
// eslint-disable-next-line
replacer?: (this: any, key: string, value: unknown) => string
) => string
onEditEvent?: OnEditEventFunction
showIconTooltips: boolean
}

Expand All @@ -53,6 +55,7 @@ export const EditButtons: React.FC<EditButtonProps> = ({
editConfirmRef,
getNewKeyOptions,
jsonStringify,
onEditEvent,
showIconTooltips,
}) => {
const { getStyles } = useTheme()
Expand All @@ -71,6 +74,12 @@ export const EditButtons: React.FC<EditButtonProps> = ({
const hasKeyOptionsList = Array.isArray(addingKeyState)

const updateAddingState = (active: boolean) => {
// Add 'null' to the path to indicate that the actual path of where the new
// key will go is not yet known.
// Also, "active" matches the second "isKey" parameter here, even though it
// describes a different thing.
if (onEditEvent) onEditEvent(active ? [...path, null] : null, active)

if (!active) {
setAddingKeyState(false)
return
Expand Down
2 changes: 2 additions & 0 deletions src/CollectionNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const CollectionNode: React.FC<CollectionNodeProps> = (props) => {
collapseAnimationTime,
onMove,
enableClipboard,
onEditEvent,
showIconTooltips,
searchFilter,
searchText,
Expand Down Expand Up @@ -433,6 +434,7 @@ export const CollectionNode: React.FC<CollectionNodeProps> = (props) => {
getNewKeyOptions={getNewKeyOptions}
editConfirmRef={editConfirmRef}
jsonStringify={jsonStringify}
onEditEvent={onEditEvent}
showIconTooltips={showIconTooltips}
/>
)
Expand Down
2 changes: 2 additions & 0 deletions src/JsonEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const Editor: React.FC<JsonEditorProps> = ({
onAdd: srcAdd = onUpdate,
onChange,
onError,
onEditEvent,
showErrorMessages = true,
enableClipboard = true,
indent = 2,
Expand Down Expand Up @@ -349,6 +350,7 @@ const Editor: React.FC<JsonEditorProps> = ({
onAdd,
onChange,
onError,
onEditEvent,
showErrorMessages,
onMove,
showCollectionCount,
Expand Down
3 changes: 2 additions & 1 deletion src/contexts/TreeStateProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ export const TreeStateProvider = ({ children, onEditEvent, onCollapse }: TreeSta
cancelOp.current()
}
setCurrentlyEditingElement(pathString)
if (onEditEvent) onEditEvent(path, newCancelOrKey === 'key')
if (onEditEvent && (Array.isArray(path) || path === null))
onEditEvent(path, newCancelOrKey === 'key')
cancelOp.current = typeof newCancelOrKey === 'function' ? newCancelOrKey : null
}

Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export type CompareFunction = (

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

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

// Definition to externally set Collapse state -- also passed to OnCollapse
// function
Expand Down Expand Up @@ -261,6 +261,7 @@ interface BaseNodeProps {
showIconTooltips: boolean
onMove: InternalMoveFunction
enableClipboard: boolean | CopyFunction
onEditEvent?: OnEditEventFunction
restrictEditFilter: FilterFunction
restrictDeleteFilter: FilterFunction
restrictAddFilter: FilterFunction
Expand Down