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 5, 2022
1 parent 275207e commit bad6753
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/AbstractQuickEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ const register = <P extends {onChange?: (...args: any[]) => any, value?: unknown
}) => {
const [editValue, setEditValue] = useDerivedState(value);
const containerRef = useRef(null);
const editValueRef = useRef<any>(editValue);
editValueRef.current = editValue;

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

const {defaultEditComponentProps, disableEnterKey} = options;
Expand Down Expand Up @@ -172,16 +175,18 @@ const register = <P extends {onChange?: (...args: any[]) => any, value?: unknown
);

const onChangeValueByEffect = useCallback(
updator => {
(updator?: any) => {
// ensure the value is new value
const editValue = editValueRef.current;
if (typeof updator === 'function') {
handleOnChangeWhenValueChanged(updator(editValue));
}
else {
handleOnChangeWhenValueChanged(updator);
handleOnChangeWhenValueChanged(editValue);
}
switchEditStatus(false);
},
[editValue, handleOnChangeWhenValueChanged, switchEditStatus]
[handleOnChangeWhenValueChanged, switchEditStatus]
);

const contextValue = useMemo(
Expand All @@ -193,7 +198,7 @@ const register = <P extends {onChange?: (...args: any[]) => any, value?: unknown
setTimeout(
() => {
if (shouldRenderEditComponent) {
onChangeValueByEffect(editValue);
onChangeValueByEffect();
}
},
0
Expand Down Expand Up @@ -225,7 +230,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 bad6753

Please sign in to comment.