Skip to content

Commit 1a1387c

Browse files
authored
Merge pull request #373 from eccenca/feature/badges-config-CMEM-7195
Fixed the baked state of the onChange function and disabled prop in <CodeMirror /> component (CMEM-7195)
2 parents 023f031 + d2f6644 commit 1a1387c

2 files changed

Lines changed: 13 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
4646
- use correct font sizes when `size` property is set
4747
- `Typography`
4848
- adjust displaying fallback symbols in different browsers
49+
- `<CodeMirror />`
50+
- use the latest provided `onChange` function
4951

5052
### Changed
5153

src/extensions/codemirror/CodeMirror.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,14 @@ export const CodeEditor = ({
227227
}: CodeEditorProps) => {
228228
const parent = useRef<any>(undefined);
229229
const [view, setView] = React.useState<EditorView | undefined>();
230-
const currentView = React.useRef<EditorView>();
231-
currentView.current = view;
232-
const currentReadOnly = React.useRef(readOnly);
233-
currentReadOnly.current = readOnly;
234-
//const currentDisabled = React.useRef(disabled);
230+
const currentView = React.useRef<EditorView>()
231+
currentView.current = view
232+
const currentReadOnly = React.useRef(readOnly)
233+
currentReadOnly.current = readOnly
234+
const currentOnChange = React.useRef(onChange)
235+
currentOnChange.current = onChange
236+
const currentDisabled = React.useRef(disabled)
237+
currentDisabled.current = disabled
235238
const [showPreview, setShowPreview] = React.useState<boolean>(false);
236239
// CodeMirror Compartments in order to allow for re-configuration after initialization
237240
const readOnlyCompartment = React.useRef<Compartment>(compartment())
@@ -320,11 +323,11 @@ export const CodeEditor = ({
320323
disabledCompartment.current.of(EditorView?.editable.of(!disabled)),
321324
AdaptedEditorViewDomEventHandlers(domEventHandlers) as Extension,
322325
EditorView?.updateListener.of((v: ViewUpdate) => {
323-
if (disabled) return;
326+
if (currentDisabled.current) return;
324327

325-
if (onChange && v.docChanged) {
328+
if (currentOnChange.current && v.docChanged) {
326329
// Only fire if the text has actually been changed
327-
onChange(v.state.doc.toString());
330+
currentOnChange.current(v.state.doc.toString());
328331
}
329332

330333
if (onSelection)

0 commit comments

Comments
 (0)