@@ -54,6 +54,8 @@ export type EditViewFn<T> = (props: {
5454 value : T ;
5555 onChange : ( value : T ) => void ;
5656 onChangeEnd : ( ) => void ;
57+ onCommit ?: ( value : T ) => void ;
58+ onCancel ?: ( ) => void ;
5759 onImmediateSave ?: ( value : T ) => void ;
5860 otherProps ?: Record < string , any > ;
5961} ) => ReactNode ;
@@ -152,22 +154,38 @@ function EditableCellComp<T extends JSONValue>(props: EditableCellProps<T>) {
152154 [ ]
153155 ) ;
154156
155- const onChangeEnd = useCallback ( ( ) => {
157+ const commitValue = useCallback ( ( finalValue : T | null ) => {
156158 if ( ! mountedRef . current ) return ;
157-
159+
158160 setIsEditing ( false ) ;
159- const newValue = _ . isNil ( tmpValue ) || _ . isEqual ( tmpValue , baseValue ) ? null : tmpValue ;
161+ const newValue = _ . isNil ( finalValue ) || _ . isEqual ( finalValue , baseValue ) ? null : finalValue ;
160162 dispatch (
161163 changeChildAction (
162164 "changeValue" ,
163165 newValue ,
164166 false
165167 )
166168 ) ;
167- if ( ! _ . isEqual ( tmpValue , value ) ) {
169+ if ( ! _ . isEqual ( finalValue , value ) ) {
168170 onTableEvent ?.( 'columnEdited' ) ;
169171 }
170- } , [ dispatch , tmpValue , baseValue , value , onTableEvent , setIsEditing ] ) ;
172+ } , [ dispatch , baseValue , value , onTableEvent , setIsEditing ] ) ;
173+
174+ const onChangeEnd = useCallback ( ( ) => {
175+ commitValue ( tmpValue ) ;
176+ } , [ commitValue , tmpValue ] ) ;
177+
178+ const onCommit = useCallback ( ( nextValue : T ) => {
179+ if ( ! mountedRef . current ) return ;
180+ setTmpValue ( nextValue ) ;
181+ commitValue ( nextValue ) ;
182+ } , [ commitValue ] ) ;
183+
184+ const onCancel = useCallback ( ( ) => {
185+ if ( ! mountedRef . current ) return ;
186+ setIsEditing ( false ) ;
187+ setTmpValue ( value ) ;
188+ } , [ setIsEditing , value ] ) ;
171189
172190 const onImmediateSave = useCallback ( ( newValue : T ) => {
173191 if ( ! mountedRef . current ) return ;
@@ -187,8 +205,8 @@ function EditableCellComp<T extends JSONValue>(props: EditableCellProps<T>) {
187205 } , [ dispatch , baseValue , value , onTableEvent ] ) ;
188206
189207 const editView = useMemo (
190- ( ) => editViewFn ?.( { value, onChange, onChangeEnd, onImmediateSave, otherProps } ) ?? < > </ > ,
191- [ editViewFn , value , onChange , onChangeEnd , onImmediateSave , otherProps ]
208+ ( ) => editViewFn ?.( { value, onChange, onChangeEnd, onCommit , onCancel , onImmediateSave, otherProps } ) ?? < > </ > ,
209+ [ editViewFn , value , onChange , onChangeEnd , onCommit , onCancel , onImmediateSave , otherProps ]
192210 ) ;
193211
194212 const enterEditFn = useCallback ( ( ) => {
@@ -243,4 +261,4 @@ function EditableCellComp<T extends JSONValue>(props: EditableCellProps<T>) {
243261 ) ;
244262}
245263
246- export const EditableCell = React . memo ( EditableCellComp ) as typeof EditableCellComp ;
264+ export const EditableCell = React . memo ( EditableCellComp ) as typeof EditableCellComp ;
0 commit comments