From d2f6644423ce15ea36b22e18fa60e6e8f60166e2 Mon Sep 17 00:00:00 2001 From: Mariia Kovsharova Date: Fri, 27 Feb 2026 09:44:00 +0100 Subject: [PATCH] Fixed the baked state of the onChange function in component Fixed the baked state of the onChange function in component --- CHANGELOG.md | 2 ++ src/extensions/codemirror/CodeMirror.tsx | 19 +++++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce3c53c7..0f0b3030 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p - use correct font sizes when `size` property is set - `Typography` - adjust displaying fallback symbols in different browsers +- `` + - use the latest provided `onChange` function ### Changed diff --git a/src/extensions/codemirror/CodeMirror.tsx b/src/extensions/codemirror/CodeMirror.tsx index 8ef1a0bf..bcaec554 100644 --- a/src/extensions/codemirror/CodeMirror.tsx +++ b/src/extensions/codemirror/CodeMirror.tsx @@ -227,11 +227,14 @@ export const CodeEditor = ({ }: CodeEditorProps) => { const parent = useRef(undefined); const [view, setView] = React.useState(); - const currentView = React.useRef(); - currentView.current = view; - const currentReadOnly = React.useRef(readOnly); - currentReadOnly.current = readOnly; - //const currentDisabled = React.useRef(disabled); + const currentView = React.useRef() + currentView.current = view + const currentReadOnly = React.useRef(readOnly) + currentReadOnly.current = readOnly + const currentOnChange = React.useRef(onChange) + currentOnChange.current = onChange + const currentDisabled = React.useRef(disabled) + currentDisabled.current = disabled const [showPreview, setShowPreview] = React.useState(false); // CodeMirror Compartments in order to allow for re-configuration after initialization const readOnlyCompartment = React.useRef(compartment()) @@ -320,11 +323,11 @@ export const CodeEditor = ({ disabledCompartment.current.of(EditorView?.editable.of(!disabled)), AdaptedEditorViewDomEventHandlers(domEventHandlers) as Extension, EditorView?.updateListener.of((v: ViewUpdate) => { - if (disabled) return; + if (currentDisabled.current) return; - if (onChange && v.docChanged) { + if (currentOnChange.current && v.docChanged) { // Only fire if the text has actually been changed - onChange(v.state.doc.toString()); + currentOnChange.current(v.state.doc.toString()); } if (onSelection)