7
7
* dropped on can act on it)
8
8
*/
9
9
10
- import React , { createContext , useContext , useState } from 'react'
10
+ import React , { createContext , useContext , useRef , useState } from 'react'
11
11
import { type CollectionKey } from '../types'
12
12
13
13
interface CollapseAllState {
@@ -25,7 +25,7 @@ interface TreeStateContext {
25
25
setCollapseState : ( collapseState : CollapseAllState | null ) => void
26
26
doesPathMatch : ( path : CollectionKey [ ] ) => boolean
27
27
currentlyEditingElement : string | null
28
- setCurrentlyEditingElement : React . Dispatch < React . SetStateAction < string | null > >
28
+ setCurrentlyEditingElement : ( newElement : string | null , cancelOp ?: ( ) => void ) => void
29
29
areChildrenBeingEdited : ( pathString : string ) => boolean
30
30
dragSource : DragSource
31
31
setDragSource : ( newState : DragSource ) => void
@@ -50,6 +50,17 @@ export const TreeStateProvider = ({ children }: { children: React.ReactNode }) =
50
50
path : null ,
51
51
pathString : null ,
52
52
} )
53
+ const cancelOp = useRef < ( ( ) => void ) | null > ( null )
54
+
55
+ const updateCurrentlyEditingElement = ( newElement : string | null , newCancel ?: ( ) => void ) => {
56
+ // The "Cancel" allows the UI to reset the element that was previously being
57
+ // edited if the user clicks another "Edit" button elsewhere
58
+ if ( currentlyEditingElement !== null && newElement !== null && cancelOp . current !== null ) {
59
+ cancelOp . current ( )
60
+ }
61
+ setCurrentlyEditingElement ( newElement )
62
+ cancelOp . current = newCancel ?? null
63
+ }
53
64
54
65
const doesPathMatch = ( path : CollectionKey [ ] ) => {
55
66
if ( collapseState === null ) return false
@@ -79,7 +90,7 @@ export const TreeStateProvider = ({ children }: { children: React.ReactNode }) =
79
90
doesPathMatch,
80
91
// Editing
81
92
currentlyEditingElement,
82
- setCurrentlyEditingElement,
93
+ setCurrentlyEditingElement : updateCurrentlyEditingElement ,
83
94
areChildrenBeingEdited,
84
95
// Drag-n-drop
85
96
dragSource,
0 commit comments