From 9fbc1def9678b00d8e819f3023e821e8e64505da Mon Sep 17 00:00:00 2001 From: Rozstone Date: Thu, 12 Dec 2024 16:58:05 +0800 Subject: [PATCH 1/2] fix: correct setvalue in controlled input behavior --- .../math/src/react/hooks/useEquationInput.ts | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/packages/math/src/react/hooks/useEquationInput.ts b/packages/math/src/react/hooks/useEquationInput.ts index 26cdd96f31..eb74bbb670 100644 --- a/packages/math/src/react/hooks/useEquationInput.ts +++ b/packages/math/src/react/hooks/useEquationInput.ts @@ -23,9 +23,11 @@ export const useEquationInput = ({ const element = useElement(); const inputRef = useRef(null); - const [initialExpression, setInitialExpression] = React.useState< - string | null - >(null); + const [expressionInput, setExpressionInput] = React.useState( + element.texExpression + ); + + const initialExpressionRef = useRef(element.texExpression); useEffect(() => { if (open) { @@ -35,7 +37,7 @@ export const useEquationInput = ({ inputRef.current.select(); if (isInline) { - setInitialExpression(element.texExpression); + initialExpressionRef.current = element.texExpression; } } }, 0); @@ -43,6 +45,13 @@ export const useEquationInput = ({ // eslint-disable-next-line react-hooks/exhaustive-deps }, [open]); + useEffect(() => { + setNode(editor, element, { + texExpression: expressionInput || '', + }); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [expressionInput]); + const onSubmit = () => { onClose?.(); }; @@ -50,7 +59,7 @@ export const useEquationInput = ({ const onDismiss = () => { if (isInline) { setNode(editor, element, { - texExpression: initialExpression ?? '', + texExpression: initialExpressionRef.current, }); } @@ -59,11 +68,9 @@ export const useEquationInput = ({ return { props: { - value: element.texExpression, + value: expressionInput, onChange: (e: React.ChangeEvent) => { - setNode(editor, element, { - texExpression: e.target.value, - }); + setExpressionInput(e.target.value); }, onKeyDown: (e: React.KeyboardEvent) => { if (isHotkey('enter')(e)) { From df35d66cc8f87c1971bc8f380c4f929009080271 Mon Sep 17 00:00:00 2001 From: Ziad Beyens Date: Thu, 12 Dec 2024 15:04:58 +0100 Subject: [PATCH 2/2] Create pink-pillows-end.md --- .changeset/pink-pillows-end.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/pink-pillows-end.md diff --git a/.changeset/pink-pillows-end.md b/.changeset/pink-pillows-end.md new file mode 100644 index 0000000000..b5508134ae --- /dev/null +++ b/.changeset/pink-pillows-end.md @@ -0,0 +1,5 @@ +--- +"@udecode/plate-math": patch +--- + +Fixes #3851