Fix body field mismatch when switching between items in edit dialog

The RichTextEditor was not updating its content when switching from one
item to another because initializedRef was only reset when value became
null. Now uses prevValueRef to properly detect value changes.
This commit is contained in:
Ilia Sharin
2026-08-20 13:01:55 -04:00
parent f9ae92f605
commit 95a628249d
4 changed files with 28 additions and 38 deletions
+6 -13
View File
@@ -10,27 +10,20 @@ function RichTextEditor({ value, onChange, placeholder = "Add details...", colla
const [copyMenuPos, setCopyMenuPos] = useStateD({ x: 0, y: 0 });
const toolbarRef = useRefD(null);
const isEditingRef = useRefD(false);
const initializedRef = useRefD(false);
const prevValueRef = useRefD(value);
// Initialize editor content only when value changes externally (not from our own edits)
useEffectD(() => {
if (editorRef.current && value !== undefined && !isEditingRef.current) {
const html = MarkdownUtils.toHtml(value || '');
// Only set innerHTML on first load or when value is reset externally
if (!initializedRef.current || editorRef.current.innerHTML === '') {
if (editorRef.current && !isEditingRef.current) {
// Update if value changed from previous or editor is empty
if (prevValueRef.current !== value || editorRef.current.innerHTML === '') {
const html = MarkdownUtils.toHtml(value || '');
editorRef.current.innerHTML = html;
initializedRef.current = true;
prevValueRef.current = value;
}
}
}, [value]);
// Reset initialized flag when dialog closes/opens (value becomes null/undefined)
useEffectD(() => {
if (value === null || value === undefined) {
initializedRef.current = false;
}
}, [value]);
const handleInput = useCallbackD(() => {
if (editorRef.current) {
isEditingRef.current = true;