Skip to content

Commit

Permalink
fix(abstractquickedit): get new state in closure
Browse files Browse the repository at this point in the history
  • Loading branch information
leotian committed Aug 1, 2022
1 parent 275207e commit c3a59e9
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/AbstractQuickEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import React, {
useMemo,
createContext,
useContext,
useEffect,
} from 'react';
import c from 'classnames';
import {useDerivedState} from '@huse/derived-state';
Expand Down Expand Up @@ -78,6 +79,8 @@ const register = <P extends {onChange?: (...args: any[]) => any, value?: unknown
}) => {
const [editValue, setEditValue] = useDerivedState(value);
const containerRef = useRef(null);
const intervalRef = useRef<any>(null);

const [shouldRenderEditComponent, switchShouldRenderEditComponent] = useDerivedState(editing);

const {defaultEditComponentProps, disableEnterKey} = options;
Expand Down Expand Up @@ -177,7 +180,7 @@ const register = <P extends {onChange?: (...args: any[]) => any, value?: unknown
handleOnChangeWhenValueChanged(updator(editValue));
}
else {
handleOnChangeWhenValueChanged(updator);
handleOnChangeWhenValueChanged(editValue);
}
switchEditStatus(false);
},
Expand All @@ -189,11 +192,18 @@ const register = <P extends {onChange?: (...args: any[]) => any, value?: unknown
[onChangeValueByEffect]
);

useEffect(
() => {
intervalRef.current = onChangeValueByEffect;
},
[onChangeValueByEffect]
);

useClickOutside(containerRef, () => {
setTimeout(
() => {
if (shouldRenderEditComponent) {
onChangeValueByEffect(editValue);
intervalRef.current();
}
},
0
Expand Down Expand Up @@ -225,7 +235,7 @@ const register = <P extends {onChange?: (...args: any[]) => any, value?: unknown
{...defaultEditComponentProps}
disabled={disabled}
onChange={handleChange}
value={editValue}
value={value}
/>
) : (
displayComponent
Expand Down

0 comments on commit c3a59e9

Please sign in to comment.