Skip to content
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

fix(abstractquickedit): get new state in closure #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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