Skip to content

Commit 64b97cd

Browse files
committed
Fixed the baked state of the onChange function in <CodeMirror /> component
1 parent e0b0e11 commit 64b97cd

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
2828
- header-menu items are vertically centered now
2929
- `Typography`
3030
- adjust displaying fallback symbols in different browsers
31+
- `<CodeMirror />`
32+
- use the latest provided `onChange` function
3133

3234
### Changed
3335

src/extensions/codemirror/CodeMirror.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ export const CodeEditor = ({
231231
currentView.current = view
232232
const currentReadOnly = React.useRef(readOnly)
233233
currentReadOnly.current = readOnly
234+
const currentOnChange = React.useRef(onChange)
235+
currentOnChange.current = onChange
236+
const currentDisabled = React.useRef(disabled)
237+
currentDisabled.current = disabled
234238
const [showPreview, setShowPreview] = React.useState<boolean>(false);
235239
// CodeMirror Compartments in order to allow for re-configuration after initialization
236240
const readOnlyCompartment = React.useRef<Compartment>(compartment())
@@ -319,11 +323,11 @@ export const CodeEditor = ({
319323
disabledCompartment.current.of(EditorView?.editable.of(!disabled)),
320324
AdaptedEditorViewDomEventHandlers(domEventHandlers) as Extension,
321325
EditorView?.updateListener.of((v: ViewUpdate) => {
322-
if (disabled) return;
326+
if (currentDisabled.current) return;
323327

324-
if (onChange && v.docChanged) {
328+
if (currentOnChange.current && v.docChanged) {
325329
// Only fire if the text has actually been changed
326-
onChange(v.state.doc.toString());
330+
currentOnChange.current(v.state.doc.toString());
327331
}
328332

329333
if (onSelection)

0 commit comments

Comments
 (0)